From 85f82e78b7118ceded5492dc538ae346ba6976d1 Mon Sep 17 00:00:00 2001 From: Bill Date: Tue, 3 Nov 2015 17:59:29 -0800 Subject: [PATCH] associative matcher uses case comparison now: match regexps and ranges and stuff --- README.md | 15 +++++++++++---- lib/hamstar.rb | 2 +- lib/hamstar/version.rb | 2 +- spec/hamstar_spec.rb | 7 +++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3015de5..b82d07c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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"]]] ``` diff --git a/lib/hamstar.rb b/lib/hamstar.rb index 400d742..32453ad 100644 --- a/lib/hamstar.rb +++ b/lib/hamstar.rb @@ -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 diff --git a/lib/hamstar/version.rb b/lib/hamstar/version.rb index 7d637ac..bfd6f27 100644 --- a/lib/hamstar/version.rb +++ b/lib/hamstar/version.rb @@ -1,3 +1,3 @@ module Hamstar - VERSION = "0.0.7" + VERSION = "0.0.8" end diff --git a/spec/hamstar_spec.rb b/spec/hamstar_spec.rb index 91c78a6..6df3b71 100644 --- a/spec/hamstar_spec.rb +++ b/spec/hamstar_spec.rb @@ -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 = [