hamstar 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -7
- data/hamstar.gemspec +1 -1
- data/lib/hamstar.rb +4 -7
- data/lib/hamstar/version.rb +1 -1
- data/spec/hamstar_spec.rb +8 -6
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 10081af52f734deabb468d54970637ad5f591c73
|
4
|
+
data.tar.gz: f17292d002caceba185a55a8b814c42b506f92ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38185b986447b91ecfc13ac4cc5f90531b37dd3d8db99830464053a9cc6cd751471090a0dce0b324f7927e1d2e840fdea2855962374cc12a1d5f9ddef6b8b8e8
|
7
|
+
data.tar.gz: c0627954be2535db2d4a15f4d18f426449262938421e9ec469ebc49c95571f4a7e7df38430c5503bb8164f82c4c1e075641dd567e9d8e9ef57bec51355bd3f09
|
data/README.md
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# Hamstar
|
2
2
|
|
3
|
+
`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:
|
3
4
|
|
5
|
+
1. the associative selector denoted by an array containing a key and a value e.g. `[:name,'Chris']`
|
6
|
+
2. the Kleene star operator denoted by '*'
|
7
|
+
3. generalized `Proc`-based matching e.g. you can supply a lambda directly in the path specification
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
@@ -18,11 +22,6 @@ Or install it yourself as:
|
|
18
22
|
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
`Hamstar.update_having` is a `module_function` that works just like [Hamster update_in()](https://github.com/hamstergem/hamster#transformations) but with two additional ways to select container elements:
|
22
|
-
|
23
|
-
1. the associative selector denoted by an array containing a key and a value e.g. `[:name,'Chris']`
|
24
|
-
2. the Kleen star operator denoted by '*'
|
25
|
-
|
26
25
|
With plain old `update_in()` you can:
|
27
26
|
|
28
27
|
```ruby
|
@@ -54,10 +53,10 @@ Hamstar.update_having( x, [:name,'Pat'],:name){|name| 'Patsy'}
|
|
54
53
|
=> Hamster::Vector[Hamster::Hash[:name => "Chris", :hobbies => Hamster::Vector["clarinet"]], Hamster::Hash[:name => "Patsy", :hobbies => Hamster::Vector["bird watching", "rugby"]]]
|
55
54
|
```
|
56
55
|
|
57
|
-
Finally, you can use a `Proc` as a matcher
|
56
|
+
Finally, you can use a `Proc` as a matcher. Here's an example that supplies a lambda inline:
|
58
57
|
|
59
58
|
```ruby
|
60
|
-
Hamstar.update_having( x, ->(k,v
|
59
|
+
Hamstar.update_having( x, ->(k,v){v[:name] == 'Pat'},:name){|name| 'Patsy'}
|
61
60
|
=> Hamster::Vector[Hamster::Hash[:name => "Chris", :hobbies => Hamster::Vector["clarinet"]], Hamster::Hash[:name => "Patsy", :hobbies => Hamster::Vector["bird watching", "rugby"]]]
|
62
61
|
```
|
63
62
|
|
data/hamstar.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Bill Burcham"]
|
10
10
|
spec.email = ["bill.burcham@gmail.com"]
|
11
11
|
spec.summary = %q{Hamstar Transforms Immutable Ruby Collections Better}
|
12
|
-
spec.description = %q{Hamstar.update_having() lets you transform deep amalgamations of Hamster (immutable) Hash and Vector with all the features of update_in() plus associative selection [key,val]
|
12
|
+
spec.description = %q{Hamstar.update_having() lets you transform deep amalgamations of Hamster (immutable) Hash and Vector with all the features of update_in() plus: associative selection [key,val], Kleene star '*', and generalized Proc-based matching}
|
13
13
|
spec.homepage = "https://github.com/Bill/hamstar"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/hamstar.rb
CHANGED
@@ -5,8 +5,6 @@ require 'hamster'
|
|
5
5
|
module Hamstar
|
6
6
|
|
7
7
|
KLEENE_STAR = '*'
|
8
|
-
MATCH_KLEENE_STAR = ->(k,v,expr){true}
|
9
|
-
MATCH_ASSOCIATION = ->(k,v,expr){key,value=expr; v[key] == value}
|
10
8
|
|
11
9
|
module_function
|
12
10
|
|
@@ -16,8 +14,8 @@ module Hamstar
|
|
16
14
|
end
|
17
15
|
matcher = match_path[0]
|
18
16
|
case matcher
|
19
|
-
when KLEENE_STAR; match
|
20
|
-
when Array, Hamster::Vector; match
|
17
|
+
when KLEENE_STAR; match ->(k,v){true}, c, *match_path, &block
|
18
|
+
when Array, Hamster::Vector; match ->(k,v){key,value=matcher; v[key] == value}, c, *match_path, &block
|
21
19
|
when Proc; match matcher, c, *match_path, &block
|
22
20
|
else
|
23
21
|
if match_path.size == 1
|
@@ -31,10 +29,9 @@ module Hamstar
|
|
31
29
|
end
|
32
30
|
|
33
31
|
def match(matcher, c, *match_path, &block)
|
34
|
-
|
35
|
-
mp_rest = Hamster.from(match_path)[1..-1] # drop expr
|
32
|
+
mp_rest = Hamster.from(match_path)[1..-1] # drop first expr
|
36
33
|
c.each_pair do |key,value|
|
37
|
-
if matcher.call key, value
|
34
|
+
if matcher.call key, value
|
38
35
|
mp = mp_rest.unshift key # put key where assoc was
|
39
36
|
c = update_having c, *mp, &block
|
40
37
|
end
|
data/lib/hamstar/version.rb
CHANGED
data/spec/hamstar_spec.rb
CHANGED
@@ -13,18 +13,20 @@ RSpec.describe Hamstar do
|
|
13
13
|
examples_kleene_star = [
|
14
14
|
[ {a:1,b:2}, ['*'], ->(v){v+1}, {a:2,b:3}, 'top level Hash'],
|
15
15
|
[ [1,2], ['*'], ->(v){v+1}, [2,3], 'top level Vector'],
|
16
|
-
[ {a:[1],b:[2]},[:b,
|
17
|
-
[ [{a:1},{a:2}],[0,
|
18
|
-
[ {a:[1],b:[2]},['*',
|
19
|
-
[ [{a:1},{a:2}],['*'
|
16
|
+
[ {a:[1],b:[2]},[:b,'*'], ->(v){v+1}, {a:[1],b:[3]}, 'Hash containing Vector'],
|
17
|
+
[ [{a:1},{a:2}],[0,'*'], ->(v){v+1}, [{a:2},{a:2}], 'Vector containing Hash'],
|
18
|
+
[ {a:[1],b:[2]},['*',0], ->(v){v+1}, {a:[2],b:[3]}, 'Vector inside Hash'],
|
19
|
+
[ [{a:1},{a:2}],['*',:a], ->(v){v+1}, [{a:2},{a:3}], 'Hash inside Vector']
|
20
20
|
]
|
21
21
|
|
22
22
|
examples_associative = [
|
23
|
-
[ [{name:'Chris'},{name:'Pat'}],
|
23
|
+
[ [{name:'Chris'},{name:'Pat'}], [[:name,'Pat'],:name],->(name){name+'sy'}, [{name:'Chris'},{name:'Patsy'}], 'match a Hash'],
|
24
|
+
[ [[:name,'Chris'],[:name,'Pat']],[[1,'Pat'],1], ->(name){name+'sy'}, [[:name,'Chris'],[:name,'Patsy']],'match a Vector']
|
24
25
|
]
|
25
26
|
|
26
27
|
examples_function_match = [
|
27
|
-
[ [1,2], [
|
28
|
+
[ [{a:1},{b:1},{b:2}], ['*',->(k,v){k==:b && v==2}], ->(v){5}, [{a:1},{b:1},{b:5}],'match a Hash'],
|
29
|
+
[ [[1,2],[3,4]], ['*',->(k,v){k==0&&v==3}],->(v){7}, [[1,2],[7,4]], 'match a Vector']
|
28
30
|
]
|
29
31
|
|
30
32
|
def self.write_examples(examples)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hamstar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bill Burcham
|
@@ -80,9 +80,9 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description: Hamstar.update_having() lets you transform deep amalgamations of Hamster
|
84
|
-
(immutable) Hash and Vector with all the features of update_in() plus associative
|
85
|
-
selection [key,val]
|
83
|
+
description: 'Hamstar.update_having() lets you transform deep amalgamations of Hamster
|
84
|
+
(immutable) Hash and Vector with all the features of update_in() plus: associative
|
85
|
+
selection [key,val], Kleene star ''*'', and generalized Proc-based matching'
|
86
86
|
email:
|
87
87
|
- bill.burcham@gmail.com
|
88
88
|
executables: []
|