lab42_diggy_methods 0.1.2 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +53 -4
- data/lib/lab42/diggy_methods/version.rb +1 -1
- data/lib/lab42/diggy_methods.rb +22 -5
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd7c9c8c8932a3b06dd8d5cbaae76cc8983d90ffef39df1baca0bfed35eb2020
|
4
|
+
data.tar.gz: de17d940d1e5adffd415efc59ff3aba510da695db0f372135db2fa675f625215
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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!`
|
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
|
```
|
@@ -60,7 +60,13 @@ And that works for leave nodes too of course
|
|
60
60
|
|
61
61
|
And in case of missing keys
|
62
62
|
```ruby
|
63
|
-
expect{ diggy.b.d.f }.to raise_error(KeyError, "key not found:
|
63
|
+
expect{ diggy.b.d.f }.to raise_error(KeyError, "key not found: b.d.f")
|
64
|
+
```
|
65
|
+
|
66
|
+
And this works for arrays too:
|
67
|
+
```ruby
|
68
|
+
with_array = Diggy(a: [b: {}])
|
69
|
+
expect { with_array.a.first.b.c }.to raise_error(KeyError, "key not found: a.b.c")
|
64
70
|
```
|
65
71
|
|
66
72
|
If we access unknown keys we get the usual `KeyError` error, however we must not pass, nonhashable data
|
@@ -70,7 +76,7 @@ But if we pass an array
|
|
70
76
|
expect{ Diggy([:a]) }.to raise_error(ArgumentError)
|
71
77
|
```
|
72
78
|
|
73
|
-
### Context: Using in ERB
|
79
|
+
### Context: Using in ERB with `__binding__`
|
74
80
|
|
75
81
|
In order to take advantage of this syntax we want to pass the binding of a `Diggy` object to `ERB`
|
76
82
|
|
@@ -90,6 +96,49 @@ Then we can pass the binding to the template
|
|
90
96
|
expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
|
91
97
|
```
|
92
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
|
+
|
123
|
+
### Context: Iteration
|
124
|
+
|
125
|
+
If an element in a diggy object is an array we descend
|
126
|
+
|
127
|
+
Given yet another diggy™
|
128
|
+
```ruby
|
129
|
+
let(:diggy) { Diggy(a: [b: 1, c: 2]) }
|
130
|
+
```
|
131
|
+
|
132
|
+
Then we get an array
|
133
|
+
```ruby
|
134
|
+
expect(diggy.a).to be_an(Array)
|
135
|
+
```
|
136
|
+
|
137
|
+
And it contains diggy instances
|
138
|
+
```ruby
|
139
|
+
expect(diggy.a.first.b).to eq(1)
|
140
|
+
```
|
141
|
+
|
93
142
|
# LICENSE
|
94
143
|
|
95
144
|
Copyright 2022 Robert Dober robert.dober@gmail.com
|
data/lib/lab42/diggy_methods.rb
CHANGED
@@ -12,8 +12,9 @@ module Lab42
|
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
-
def initialize(a=nil, **data_)
|
15
|
+
def initialize(a=nil, __key_chain__: [], **data_)
|
16
16
|
@data = _make_data_from_args(a, data_)
|
17
|
+
@key_chain = __key_chain__
|
17
18
|
end
|
18
19
|
|
19
20
|
def _make_data_from_args(a, data_)
|
@@ -37,7 +38,7 @@ module Lab42
|
|
37
38
|
raise ArgumentError, "positional argument must be a hash or respond to to_h if present"
|
38
39
|
end
|
39
40
|
|
40
|
-
def method_missing(name)
|
41
|
+
def method_missing(name, *)
|
41
42
|
if name.to_s.end_with?"!"
|
42
43
|
@data.fetch(name.to_s.sub(/!\z/, "").to_sym)
|
43
44
|
else
|
@@ -45,14 +46,30 @@ module Lab42
|
|
45
46
|
end
|
46
47
|
end
|
47
48
|
|
48
|
-
def
|
49
|
-
found
|
49
|
+
def _make_diggy_array(found, key)
|
50
|
+
found.map { self.class.new(_1, __key_chain__: _new_key_chain(key)) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def _maybe_make_diggy(found, key)
|
50
54
|
if found.respond_to?(:to_h)
|
51
|
-
self.class.new(**found)
|
55
|
+
self.class.new(**found, __key_chain__: _new_key_chain(key))
|
52
56
|
else
|
53
57
|
found
|
54
58
|
end
|
55
59
|
end
|
60
|
+
|
61
|
+
def _method_missing_try_descend(name)
|
62
|
+
found = @data.fetch(name) { raise KeyError, "key not found: #{_new_key_chain(name).join(".")}" }
|
63
|
+
if Array === found
|
64
|
+
_make_diggy_array(found, name)
|
65
|
+
else
|
66
|
+
_maybe_make_diggy(found, name)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def _new_key_chain(key)
|
71
|
+
@key_chain + [key]
|
72
|
+
end
|
56
73
|
end
|
57
74
|
end
|
58
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.
|
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-
|
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
|
@@ -25,7 +25,7 @@ files:
|
|
25
25
|
- lib/lab42/diggy_methods.rb
|
26
26
|
- lib/lab42/diggy_methods/kernel.rb
|
27
27
|
- lib/lab42/diggy_methods/version.rb
|
28
|
-
homepage: https://github.com/robertdober/
|
28
|
+
homepage: https://github.com/robertdober/diggy_methods
|
29
29
|
licenses:
|
30
30
|
- Apache-2.0
|
31
31
|
metadata: {}
|