Skip to content

Commit

Permalink
associative matcher uses case comparison now: match regexps and range…
Browse files Browse the repository at this point in the history
…s and stuff
  • Loading branch information
Bill committed Nov 4, 2015
1 parent 43d67e4 commit 85f82e7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

`Hamstar.update_having` is a `module_function` that works just like [Hamster update_in()](https://github.com/hamstergem/hamster#transformations) but with three additional ways to select container elements:

1. the associative selector denoted by an array containing a key and a value e.g. `[:name,'Chris']`
2. the Kleene star operator denoted by `'*'`
1. the Kleene star operator denoted by `'*'`
2. the associative selector denoted by an array containing a key and a value e.g. `[:name,'Chris']` your values will be compared to this one using the case comparison operator `===` so you can use Strings or regexps or other things that define `===` (such as classes and Ranges)
3. generalized `Proc`-based matching e.g. you can supply a lambda directly in the path specification

## Installation
Expand Down Expand Up @@ -53,10 +53,17 @@ Hamstar.update_having( x, [:name,'Pat'],:name){|name| 'Patsy'}
=> Hamster::Vector[Hamster::Hash[:name => "Chris", :hobbies => Hamster::Vector["clarinet"]], Hamster::Hash[:name => "Patsy", :hobbies => Hamster::Vector["bird watching", "rugby"]]]
```

Finally, you can use a `Proc` as a matcher. Here's an example that supplies a lambda inline:
Note that your values will be compared to the second element of the pair using the case comparison operator `===`. That means you can use a Regexp there (or any other object that defines `===`) e.g.:

```ruby
Hamstar.update_having( x, ->(k,v){v[:name] == 'Pat'},:name){|name| 'Patsy'}
Hamstar.update_having( x, [:name,/P/],:name){|name| name+'sy'}
=> (same result as before)
```

Finally, if none of the options given above work for you, you can use an arbitrary `Proc` as a matcher. Here's an example that supplies a lambda inline:

```ruby
Hamstar.update_having( x, ->(k,v){k.odd?},:name){|name| 'Patsy'}
=> Hamster::Vector[Hamster::Hash[:name => "Chris", :hobbies => Hamster::Vector["clarinet"]], Hamster::Hash[:name => "Patsy", :hobbies => Hamster::Vector["bird watching", "rugby"]]]
```

Expand Down
2 changes: 1 addition & 1 deletion lib/hamstar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def update_having(c, *match_path, &block)
matcher = match_path[0]
case matcher
when KLEENE_STAR; match ->(k,v){true}, c, *match_path, &block
when Array, Hamster::Vector; match ->(k,v){key,value=matcher; v[key] == value}, c, *match_path, &block
when Array, Hamster::Vector; match ->(k,v){key,value=matcher; value === v[key]}, c, *match_path, &block
when Proc; match matcher, c, *match_path, &block
else
if match_path.size == 1
Expand Down
2 changes: 1 addition & 1 deletion lib/hamstar/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Hamstar
VERSION = "0.0.7"
VERSION = "0.0.8"
end
7 changes: 5 additions & 2 deletions spec/hamstar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
[ [{a:1},{a:2}],['*',:a], ->(v){v+1}, [{a:2},{a:3}], 'Hash inside Vector']
]

cph = [{name:'Chris'},{name:'Pat'}]
addsy = ->(name){name+'sy'}
examples_associative = [
[ [{name:'Chris'},{name:'Pat'}], [[:name,'Pat'],:name],->(name){name+'sy'}, [{name:'Chris'},{name:'Patsy'}], 'match a Hash'],
[ [[:name,'Chris'],[:name,'Pat']],[[1,'Pat'],1], ->(name){name+'sy'}, [[:name,'Chris'],[:name,'Patsy']],'match a Vector']
[ cph, [[:name,'Pat'],:name], addsy, [{name:'Chris'},{name:'Patsy'}], 'match a Hash'],
[ [[:name,'Chris'],[:name,'Pat']],[[1,'Pat'],1], addsy, [[:name,'Chris'],[:name,'Patsy']],'match a Vector'],
[ cph, [[:name,/P/],:name], addsy, [{name:'Chris'},{name:'Patsy'}], 'match a Hash w/ a regexp']
]

examples_function_match = [
Expand Down

0 comments on commit 85f82e7

Please sign in to comment.