Commit graph

52 commits

Author SHA1 Message Date
Luis Lavena
b81ac70758 Prepare for release: v0.3.7 2017-02-04 13:00:14 -03:00
Luis Lavena
c04be1b4bd Display latest release badge in README
Use shields.io and display GitHub latest release version.

Update Travis CI badge to use shields.io too.

[skip ci]
2017-02-04 12:56:52 -03:00
Luis Lavena
68e21bcff1 Merge pull request #19 from luislavena/improve-node-sorting
Makes node prioritization insertion-independent
2017-02-04 17:47:17 +02:00
Luis Lavena
1764332123 Makes node prioritization insertion-independent
The order in which nodes were inserted into a tree might produce
failures in lookup mechanism.

    tree = Radix::Tree(Symbol).new
    tree.add "/one/:id", :one
    tree.add "/one-longer/:id", :two

    result = tree.find "/one-longer/10"

    # expected `true`
    result.found? # => false

In above example, reversing the order of insertion solved the issue,
but exposed the naive sorting/prioritization mechanism used.

This change improves that by properly identifying the kind of node
being evaluated and compared against others of the same kind.

It is now possible to know in advance if a node contains an special
condition (named parameter or globbing) or is a normal one:

    node = Radix::Node(Nil).new("a")
    node.normal? # => true

    node = Radix::Node(Nil).new(":query")
    node.named? # => true

    node = Radix::Node(Nil).new("*filepath")
    node.glob? # => true

Which helps out with prioritization of nodes:

- A normal node ranks higher than a named one
- A named node ranks higher than a glob one
- On two of same kind, ranks are based on priority

With this change in place, above example works as expected:

    tree = Radix::Tree(Symbol).new
    tree.add "/one/:id", :one
    tree.add "/one-longer/:id", :two

    result = tree.find "/one-longer/10"

    result.found? # => true

Fixes #18
2017-02-04 12:36:38 -03:00
Luis Lavena
0dc6e174d2 Prepare for release: v0.3.6 2017-01-18 12:29:43 -03:00
Luis Lavena
1eabf1a23f Merge pull request #17 from luislavena/fix-sorting-named-parameters
Lower named and glob node priority to fix lookup issue
2017-01-18 12:24:52 -03:00
Luis Lavena
9163860e4d Lowers named and glob node priority to fix lookup issue
Given two similar keys, one short and one with named parameter, lookup
was incorrectly picking up the named parameter version instead of the
specific one:

    tree = Radix::Tree(Symbol).new
    tree.add "/tag-edit/:id", :edit_tag
    tree.add "/tag-edit2", :alternate_edit_tag

    result = tree.find("/tag-edit2")
    result.found? # => false

The order of insertion (named before specific) was causing the
lookup mechanism to validate it before checking the other options.

With the changes present here, short or long keys will be affected
anymore by named or globbed keys present when sharing part of the
key.

Fixes kemalcr/kemal#293
2017-01-17 14:40:16 -03:00
Luis Lavena
40b8e26ba5 spec(Tree): Replace deprecated MemoryIO
Crystal 0.20 introduced a change so `MemoryIO` is know known as
`IO::Memory`.

Since no current spec was using the deprecation redirect, there was
warning raised.

Fixes #16
2016-12-03 10:55:48 -03:00
Luis Lavena
bed664a936 misc(git): update ignores for latest version of Crystal
[skip ci]
2016-11-24 21:29:13 -03:00
Luis Lavena
a0fcdb896e misc(Travis): silence annoying success emails
Got it, it worked, nice, no need to email me on every push about it.

[skip ci]
2016-11-24 21:27:29 -03:00
Luis Lavena
6c8981af91 Prepare for release: v0.3.5 2016-11-24 21:04:15 -03:00
Luis Lavena
c81694fbec Merge pull request #15 from luislavena/14-fix-catch-all-lookup
Corrects lookup issue with catch all and shared key
2016-11-24 21:00:50 -03:00
Luis Lavena
95b6638f1a fix(Tree): corrects lookup issue with catch all and shared key
Lookup was failing when part of the given path matched a key at the
first character, even when the rest of the key didn't match.

It was not possible place a catch all an another key at the same
level that started with same character.

This change ensures that all shared part between path and key is
compared prior assuming usage of that node as part of the lookup.

Closes #14
2016-11-24 20:52:07 -03:00
Luis Lavena
6880b341a6 Prepare for release: v0.3.4
Yank v0.3.3 due mismatched `Radix::VERSION` version.
2016-11-12 15:49:32 -03:00
Luis Lavena
82b9c915e1 Prepare for release: v0.3.3 2016-11-12 13:58:42 -03:00
Luis Lavena
61f58207fd Merge pull request #13 from luislavena/inclusive-catch-all
Allows catch all to be used as optional globbing
2016-11-12 13:45:26 -03:00
Luis Lavena
c8d20afc54 fix(Tree): allows catch all to be used as optional globbing
Catch all parameter was limited to be used *after* a slash (`/`) on
a path (ie. `/members/*trailing`).

Attempts to use it immediately after the path (ie. `/members*trailing`)
was not properly identified or captured.

This change ensures catch all can be used as globbing, allowing capture
of anything after the matching path.

Closes #12
2016-11-12 13:10:44 -03:00
Luis Lavena
e4c53ba7b9 Prepare for release: v0.3.2 2016-11-05 15:32:44 -03:00
Luis Lavena
961a98dd02 Ensure code samples in docs can be executed 2016-11-05 15:24:27 -03:00
Luis Lavena
a6984844bd spec(VERSION): format naming for better verbose output
Avoid `Radix::VERSION` being evaluated and use the string version
for presentation purposes.
2016-11-05 15:02:45 -03:00
Luis Lavena
07f7576895 Merge pull request #10 from luislavena/fix-shared-key-branching
Allow insert paths with shared keys unordered
2016-11-05 14:56:29 -03:00
Luis Lavena
d7c0779ac9 fix(Tree): allows insert paths with shared keys unordered
When adding new nodes into the tree, the lookup mechanism failed
to detect same named parameter was already added to it, raising
an exception.

Example:

    tree = Radix::Tree(Symbol).new
    tree.add "/members/:id/edit", :member_edit
    tree.add "/members/export",   :members_export
    tree.add "/members/:id/videos", :member_videos # Exception

This fix ensures that comparison is done key by key, even if they
are inserted into the tree in a different order.

Closes #9
2016-11-05 14:16:09 -03:00
Luis Lavena
e55d144413 doc(Tree): adjust documentation for correct usage
Code samples embedded into `Tree` documentation weren't accurate,
so this corrects them to be up-to-date on usage.

[skip ci]
2016-08-14 11:15:01 +01:00
Luis Lavena
95d2d0aedb Prepare for release: v0.3.1 2016-07-29 18:27:51 +02:00
Luis Lavena
77789be285 Add VERSION constant for runtime usage
Introduce `Radix::VERSION` to be used at runtime by users of the
library for any purpose this will help them (expose them, use
conditional blocks or similar).

Also add spec that checks that defined versions in both `shard.yml`
and `Radix::VERSION` matches to avoid messing up releases.
2016-05-16 20:37:41 -03:00
Luis Lavena
54d3b627a2 Prepare for release: v0.3.0 2016-04-16 17:19:29 -03:00
Luis Lavena
08db441c9a Merge pull request #8 from luislavena/define-payload-type
Introduce types for forward compiler compatibility

Closes #7
2016-04-16 17:06:16 -03:00
Luis Lavena
9003075ec7 Introduce types for forward compiler compatiblity
After Crystal 0.15, compiler will require declare the types used
by instance variables on classes.

This require changes to the usage of `Radix::Tree` by introducing
the type of payload elements it will handle:

    # Will only support symbols as payload
    tree = Radix::Tree(Symbol).new
    tree.add "/", :root

    # Error: cannot add node with anything other than Symbol
    tree.add "/meaning-of-life", 42

The changes ensure future compatibility with Crystal and also
enforces a more declarative usage of `Radix::Tree`.

If necessary, you can combine multiple types to ensure a tree
can contain all the wide range of payloads you need:

    tree = Radix::Tree.new(Foo | Bar | Symbol).new
    tree.add "/", :root
    tree.add "/foo", foo_instance

This change includes:

- Tree, Node and Result has been updated to require types.
- Node is capable of have optional payload (from defined type).
- Documentation has been updated to reflect this change.
2016-04-16 16:53:20 -03:00
Luis Lavena
18bf9b132e Prepare for release: v0.2.1 2016-03-15 20:57:55 -03:00
Luis Lavena
be420ec875 Fix incorrect return type
`Result#key` is expected to be `String`, yet Crystal considered
that by branching is possible result to be `nil`.

This was caused by *return-early* approach:

    def foo
      return @foo if @foo

      @foo = "awesome"
    end

    pp typeof(foo) # => String?

Changed to instead use memoization technique.
2016-03-15 20:49:36 -03:00
Luis Lavena
2c9279e51e Prepare for release: v0.2.0 2016-03-15 20:39:35 -03:00
Luis Lavena
303a70dbfe Add Type annotations and remove Symbol usage
Remove Ruby's *symbols everywhere* approach to define getter/setters
or properties. Crystal's parser and macros do not require symbols
for these options.

Also add type annotations to some elements aiming to improve quality
of the documentation generated (more accurate expected types).
2016-03-15 20:32:48 -03:00
Luis Lavena
9e3316c93f Use Crystal's shortcut to assign variables 2016-03-10 20:55:19 -03:00
Luis Lavena
ca41d21294 Remove deprecation on shared named parameters
Deal with named parameters under same level (shared) and raise
proper `SharedKeyError` exception.

This is a non-backward compatible change aims to solve result
mapping issues and tree lookup.

Now README covers this under *Caveats* section and offers an
alternative organization of the paths used on the tree.

To avoid potential issues when using `master` instead of a locked
release, bump the version.
2016-03-10 20:54:34 -03:00
Luis Lavena
ae814c608c Prepare for release: v0.1.2 2016-03-10 20:18:59 -03:00
Luis Lavena
85d565c321 Merge pull request #6 from luislavena/correctly-split-named-parameters
Correctly split named parameters
2016-03-10 20:16:12 -03:00
Luis Lavena
e0ef8d83da Correctly split named parameters on tree insert
Our Radix implementation was literally considering every single
character as candidate for splitting, which caused keys that
contained named parameters markers (`:foo`) to be broken across
nodes:

    tree = Radix::Tree.new
    tree.add "/", :root
    tree.add "/:post", :post
    tree.add "/:category/:post", :category_post
    # /
    #  :
    #   post
    #   category/:post

This caused incorrect behavior when performing lookup (`Tree#find`)
and failing to detect and map the key name, even when the value
was properly captured:

    result = tree.find "/example"
    pp result.found? # => false

This change corrects the issue by identifying named parameter marker
and once detected, consider everything until a separator or the end
of the supplied string is reached to be a unique key:

    tree = Radix::Tree.new
    tree.add "/", :root
    tree.add "/:post", :post
    tree.add "/:category/:post", :category_post
    # /
    #  :category/:post
    #  :post

However, due how Radix tree is structured, two named parameters at the
same level might result in problems during lookup phase:

    tree = Radix::Tree.new
    tree.add "/", :root
    tree.add "/:post", :post
    tree.add "/:category/:post", :category_post
    # /
    #  :category/:post
    #  :post

    tree.root.sort!
    # /
    #  :post
    #  :category/:post

    result = tree.find "/example"
    pp result.found? # => false
    pp result.params # => {"post" => "example"}

    result = tree.find "/news/first-post"
    pp result.found? # => false
    pp result.params # => {"post" => "news"}

Causing lookup to fail and values be stored under incorrect keys
for the parameters.

Because of this, a deprecation warning will be shown to allow
users adjust and correct their code prior fully removing it and
raise error (you know, semantic versioning and all that jazz).

This fixes #5 and closes #4
2016-03-10 17:50:08 -03:00
Luis Lavena
689625acfe Correct typos in documentation 2016-03-10 17:50:08 -03:00
Luis Lavena
8e8cd530ea Add more details to Change Log
- Correct project name.
- Add links for Unreleased section.
- Indicate the versioning schema the project aims to comply with.
2016-03-10 16:49:49 -03:00
Luis Lavena
c54767a9b1 Prepare for release: v0.1.1 2016-02-29 12:11:12 -03:00
Luis Lavena
60acd49794 doc: Add CHANGELOG.md file with list of changes
[skip ci]
2016-02-29 12:09:34 -03:00
Luis Lavena
88863daab2 Merge pull request #3 from luislavena/detect-multiple-params
Correctly build key from detected parameters
2016-02-29 11:49:43 -03:00
Luis Lavena
fe1ebb7d76 Correctly build key from detected parameters
Given a key `/:foo/:bar`, the find mechanism was incorrectly picking
the separator character as part of the key name (`foo/`).

This caused incorrect match between expected name (`foo`) and the
one obtained.

When the key name was extracted from the named parameter, the size
of the returned key was not compensated, given that we move +1 positions
to avoid having ':' as part of the key.

This is now corrected by reducing the key size one shorter to
compensate.

Fixes Issue #2
2016-02-29 11:40:18 -03:00
Luis Lavena
2a7a3b77bc Merge pull request #1 from askn/master
Update README.md

[skip ci]
2016-01-24 20:04:41 -03:00
Luis Lavena
63bdc343f4 doc: Correct usage sample styles
Ensure Crystal is used as usage examples blocks covered in README.

[skip ci]
2016-01-24 20:01:31 -03:00
Aşkın Gedik
b33839e5f1 Update README.md 2016-01-25 01:00:30 +02:00
Luis Lavena
cc0f5ca225 doc: Correct typo in documentation link
Build link to documentation incorrectly, which resulted in 404 when
visited.

This commit corrects the issue.

[skip ci]
2016-01-24 19:58:29 -03:00
Luis Lavena
4543af0ae3 doc: Link to documentation (docrystal.org)
Add documentation reference badge to README

[skip ci]
2016-01-24 19:42:34 -03:00
Luis Lavena
f320d9fb1a ci: Add Travis build badge to README
[skip ci]
2016-01-24 19:35:39 -03:00
Luis Lavena
c9f474b927 ci: adjust Travis build options
Ensure both stable and nightly builds of Crystal are being used
to test this project.
2016-01-24 19:23:48 -03:00