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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 834016d61fb0ca6d052c441bd9ca2a864e859ae3573fd94a85a6b35d90741169
4
- data.tar.gz: 364f4a15f6cfb7495a201a4093105f0dc3796fbe0f5f3faa403b541ce56eade0
3
+ metadata.gz: ca75c5042852e2391ad547df17f005f23987bfd780512d5d4e3d3f9d963bf972
4
+ data.tar.gz: 28cf81f1673af0b96ba1d2e469a181c68539105a45cdaf99970716290c62f22d
5
5
  SHA512:
6
- metadata.gz: 199dd67f1f1bfc4f607c11e9f5c4c1cf25ec69844d265320ea610932055e3d859c2a52d294262581de26e3613657e8de5034fede1dd57bf053423513268061d4
7
- data.tar.gz: b929b55e0789431a0362238c6405dc06372fabe29a2e53ae5f61f649e76dc77197f0c0a73282e07b7e519e8e3185642980792f9e903f66e2e9fc22726ec8d753
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
- For the moment, compose:
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
@@ -12,7 +12,7 @@ module Xf
12
12
  end
13
13
 
14
14
  def self.version
15
- "0.0.2"
15
+ "0.0.3"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Weaver