xf 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +51 -1
- data/lib/xf/identity.rb +1 -1
- data/lib/xf/public_api.rb +13 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca75c5042852e2391ad547df17f005f23987bfd780512d5d4e3d3f9d963bf972
|
4
|
+
data.tar.gz: 28cf81f1673af0b96ba1d2e469a181c68539105a45cdaf99970716290c62f22d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 851f5270c4cd9815bfdf96b0c56ef338b21f0e176990a8e67f3a63c1f50f63d71571c1bb2270bddb3c989a4f48985f968f102e80819b25a7de5d31844afc34ed
|
7
|
+
data.tar.gz: d00cab86f2bd53031e2d7e19cdb0b7eac9185519430a0e600abeb6e021b9a721cf7a777c868122716ebe6397caf2f8d374bb19c0770b593bd02f00f4cee57ebc
|
data/README.md
CHANGED
@@ -26,7 +26,7 @@
|
|
26
26
|
|
27
27
|
## Features
|
28
28
|
|
29
|
-
|
29
|
+
**Compose**
|
30
30
|
|
31
31
|
```ruby
|
32
32
|
%w(1 2 3 4).map(&Xf.compose(:to_i, :succ))
|
@@ -34,6 +34,56 @@ For the moment, compose:
|
|
34
34
|
|
35
35
|
If it looks like a Proc, or can be convinced to become one, it will work there.
|
36
36
|
|
37
|
+
**Scopes**
|
38
|
+
|
39
|
+
A Scope is a play on a [concept from Haskell called a Lense](http://hackage.haskell.org/package/lens-tutorial-1.0.3/docs/Control-Lens-Tutorial.html).
|
40
|
+
|
41
|
+
The idea is to be able to define a path to a transformation and either get or
|
42
|
+
set against it as a first class function. Well, easier shown than explained.
|
43
|
+
|
44
|
+
To start with we can do a few gets:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
people = [{name: "Robert", age: 22}, {name: "Roberta", age: 22}, {name: "Foo", age: 42}, {name: "Bar", age: 18}]
|
48
|
+
|
49
|
+
people.map(&Xf.scope(:age).get)
|
50
|
+
# => [22, 22, 42, 18]
|
51
|
+
```
|
52
|
+
|
53
|
+
Let's try setting a value:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
people = [{name: "Robert", age: 22}, {name: "Roberta", age: 22}, {name: "Foo", age: 42}, {name: "Bar", age: 18}]
|
57
|
+
|
58
|
+
older_people = people.map(&Xf.scope(:age) { |age| age + 1 })
|
59
|
+
|
60
|
+
older_people = people.map(&Xf.scope(:age).set { |age| age + 1 })
|
61
|
+
# => [{:name=>"Robert", :age=>23}, {:name=>"Roberta", :age=>23}, {:name=>"Foo", :age=>43}, {:name=>"Bar", :age=>19}]
|
62
|
+
|
63
|
+
people
|
64
|
+
# => [{:name=>"Robert", :age=>22}, {:name=>"Roberta", :age=>22}, {:name=>"Foo", :age=>42}, {:name=>"Bar", :age=>18}]
|
65
|
+
|
66
|
+
# set! will mutate, for those tough ground in issues:
|
67
|
+
|
68
|
+
older_people = people.map(&Xf.scope(:age).set! { |age| age + 1 })
|
69
|
+
# => [{:name=>"Robert", :age=>23}, {:name=>"Roberta", :age=>23}, {:name=>"Foo", :age=>43}, {:name=>"Bar", :age=>19}]
|
70
|
+
|
71
|
+
people
|
72
|
+
# => [{:name=>"Robert", :age=>23}, {:name=>"Roberta", :age=>23}, {:name=>"Foo", :age=>43}, {:name=>"Bar", :age=>19}]
|
73
|
+
```
|
74
|
+
|
75
|
+
It works much the same as `Hash#dig` in that you can pass multiple comma-seperated values as a deeper path:
|
76
|
+
|
77
|
+
```
|
78
|
+
Xf.scope(:children, 0, :name).get.call({name: 'Foo', children: [{name: 'Bar'}]})
|
79
|
+
# => "Bar"
|
80
|
+
|
81
|
+
Xf.scope(:children, 0, :name).set('Baz').call({name: 'Foo', children: [{name: 'Bar'}]})
|
82
|
+
# => {:name=>"Foo", :children=>[{:name=>"Baz"}]}
|
83
|
+
```
|
84
|
+
|
85
|
+
That means array indexes work too, and on both `get` and `set` methods!
|
86
|
+
|
37
87
|
## Screencasts
|
38
88
|
|
39
89
|
## Requirements
|
data/lib/xf/identity.rb
CHANGED
data/lib/xf/public_api.rb
CHANGED
@@ -20,8 +20,21 @@ module Xf
|
|
20
20
|
|
21
21
|
alias_method :c, :compose
|
22
22
|
|
23
|
+
# Creates a Scope.
|
24
|
+
#
|
25
|
+
# @see Xf::Scope
|
26
|
+
#
|
27
|
+
# @note
|
28
|
+
# See the README for more instructions on usage. Will likely propogate
|
29
|
+
# more examples here and into the specs later.
|
30
|
+
#
|
31
|
+
# @param *path [Array[Any]] Hash#dig accessible segments
|
32
|
+
#
|
33
|
+
# @return [Xf::Scope]
|
23
34
|
def scope(*path)
|
24
35
|
Scope.new(path)
|
25
36
|
end
|
37
|
+
|
38
|
+
alias_method :s, :scope
|
26
39
|
end
|
27
40
|
end
|