lab42_diggy_methods 0.1.6 → 0.1.7

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: 3052880cc5f39b51c1ee8264afb99d24ddf2dc6625a8a50131c22a3511a3f6f4
4
- data.tar.gz: 466ec786a0722d71c35d7bda70525c0f277c008e4b6d86d23fa1e666ec3953a3
3
+ metadata.gz: cd7c9c8c8932a3b06dd8d5cbaae76cc8983d90ffef39df1baca0bfed35eb2020
4
+ data.tar.gz: de17d940d1e5adffd415efc59ff3aba510da695db0f372135db2fa675f625215
5
5
  SHA512:
6
- metadata.gz: caef67120413bd91d84630ab0414e0c598d2d00908579b4050a6f15b4833baaf5ca81bdec5c11c6f6ecdb4f70a7ce1b97619262e1b26028315130499b0e33534
7
- data.tar.gz: e795c99a9d373a9effe7a41554d0c0e796f2c823a94bad7c97d436f5ee5c3c8144fcb011b8c1d22dec1488a4b9299a6f9f3226440f24ae9d0dccf5c2f74cbabd
6
+ metadata.gz: 9e049dd334f90743481c9e3ce898586171149e4de1f8733654fc63e9ee85555ceb8d09e49f64b656096534e042bc0ca5b8e64851d856c80cd290eeb478a8119c
7
+ data.tar.gz: 834efe604015307a109e8ac2d0790c5c44187144aae1c5740819952c3c8aa671d1edea0a9e5348b7c1d4324801df4b88956f044e19d0a8afa6afbbe8096d7253
data/README.md CHANGED
@@ -33,7 +33,7 @@ Well let us [speculate about](https://github.com/RobertDober/speculate_about) it
33
33
 
34
34
  ### Context: `Diggy` function
35
35
 
36
- An extension to the Kernel as an alias to `Lab42::DiggyMethods.new`
36
+ An extension to the Kernel as an alias for `Lab42::DiggyMethods.new`
37
37
 
38
38
  Given
39
39
  ```ruby
@@ -48,7 +48,7 @@ Then we can access its fields as follows
48
48
  expect(diggy.b.d.e).to eq(3)
49
49
  ```
50
50
 
51
- And we can use a shortcut for `key.__data__` by using `key!`A
51
+ And we can use a shortcut for `key.__data__` by using `key!`
52
52
  ```ruby
53
53
  expect(diggy.b.d!).to eq(e: 3)
54
54
  ```
@@ -76,7 +76,7 @@ But if we pass an array
76
76
  expect{ Diggy([:a]) }.to raise_error(ArgumentError)
77
77
  ```
78
78
 
79
- ### Context: Using in ERB
79
+ ### Context: Using in ERB with `__binding__`
80
80
 
81
81
  In order to take advantage of this syntax we want to pass the binding of a `Diggy` object to `ERB`
82
82
 
@@ -96,6 +96,30 @@ Then we can pass the binding to the template
96
96
  expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
97
97
  ```
98
98
 
99
+ #### Context: Merging bindings
100
+
101
+ In some cases `Diggy` will not be the only useful _binding_ in an `ERB` Template
102
+ we can work around this as follows
103
+
104
+ Given a diggy and a module
105
+ ```ruby
106
+ let(:diggy) { Diggy(a: 1) }
107
+ let :mod do
108
+ Module.new do
109
+ def b; 42 end
110
+ end
111
+ end
112
+ ```
113
+ And we extend the `diggy` with the module
114
+ ```ruby
115
+ before { diggy.extend(mod) }
116
+ ```
117
+
118
+ Then the module's methods become accessible in the binding
119
+ ```ruby
120
+ expect(diggy.__binding__.eval("b")).to eq(42)
121
+ ```
122
+
99
123
  ### Context: Iteration
100
124
 
101
125
  If an element in a diggy object is an array we descend
@@ -105,12 +129,12 @@ Given yet another diggy™
105
129
  let(:diggy) { Diggy(a: [b: 1, c: 2]) }
106
130
  ```
107
131
 
108
- Then we get an enumerator
132
+ Then we get an array
109
133
  ```ruby
110
134
  expect(diggy.a).to be_an(Array)
111
135
  ```
112
136
 
113
- And we can use it to iterate
137
+ And it contains diggy instances
114
138
  ```ruby
115
139
  expect(diggy.a.first.b).to eq(1)
116
140
  ```
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Lab42
4
4
  class DiggyMethods
5
- VERSION = "0.1.6"
5
+ VERSION = "0.1.7"
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: Apache-2.0
@@ -46,27 +46,30 @@ module Lab42
46
46
  end
47
47
  end
48
48
 
49
- def _make_diggy_array(found)
50
- found.map { self.class.new(_1, __key_chain__: @key_chain) }
49
+ def _make_diggy_array(found, key)
50
+ found.map { self.class.new(_1, __key_chain__: _new_key_chain(key)) }
51
51
  end
52
52
 
53
- def _maybe_make_diggy(found)
53
+ def _maybe_make_diggy(found, key)
54
54
  if found.respond_to?(:to_h)
55
- self.class.new(**found, __key_chain__: @key_chain)
55
+ self.class.new(**found, __key_chain__: _new_key_chain(key))
56
56
  else
57
57
  found
58
58
  end
59
59
  end
60
60
 
61
61
  def _method_missing_try_descend(name)
62
- @key_chain << name
63
- found = @data.fetch(name) { raise KeyError, "key not found: #{@key_chain.join(".")}" }
62
+ found = @data.fetch(name) { raise KeyError, "key not found: #{_new_key_chain(name).join(".")}" }
64
63
  if Array === found
65
- _make_diggy_array(found)
64
+ _make_diggy_array(found, name)
66
65
  else
67
- _maybe_make_diggy(found)
66
+ _maybe_make_diggy(found, name)
68
67
  end
69
68
  end
69
+
70
+ def _new_key_chain(key)
71
+ @key_chain + [key]
72
+ end
70
73
  end
71
74
  end
72
75
  # SPDX-License-Identifier: Apache-2.0
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lab42_diggy_methods
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Dober
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-02 00:00:00.000000000 Z
11
+ date: 2022-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |
14
14
  Create an instance of me with a Hash and access the (nested) values