lab42_diggy_methods 0.1.0 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +50 -1
- data/lib/lab42/diggy_methods/version.rb +2 -1
- data/lib/lab42/diggy_methods.rb +29 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8cbaf0bd7f78a40b8b5a1583c0bf075877e7158c04c7f7295661e497cfbc42d
|
4
|
+
data.tar.gz: 425adc58abfa6e92a3d1e4a599db1b5f39df7da1aa4f40b025e25c71b48a86ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 331c2a205746618e0e868b110da0128296ab63b5f8047a1935f0fee27cf22a0b25d8da7760324f8e350f984a8044378ff194f7853d7152a2c3b7d76c6f29da3b
|
7
|
+
data.tar.gz: 577292e722386233c97d435cf6bfbac4fbcb3111179facf7dda176c45638dae6d962d8141d1af9f8363b38e2d64c971a9f50a1e9fcf2d1b7ed5191eeabd544a1
|
data/README.md
CHANGED
@@ -48,9 +48,19 @@ 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
|
52
|
+
```ruby
|
53
|
+
expect(diggy.b.d!).to eq(e: 3)
|
54
|
+
```
|
55
|
+
|
56
|
+
And that works for leave nodes too of course
|
57
|
+
```ruby
|
58
|
+
expect(diggy.a!).to eq(1)
|
59
|
+
```
|
60
|
+
|
51
61
|
And in case of missing keys
|
52
62
|
```ruby
|
53
|
-
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")
|
54
64
|
```
|
55
65
|
|
56
66
|
If we access unknown keys we get the usual `KeyError` error, however we must not pass, nonhashable data
|
@@ -60,6 +70,45 @@ But if we pass an array
|
|
60
70
|
expect{ Diggy([:a]) }.to raise_error(ArgumentError)
|
61
71
|
```
|
62
72
|
|
73
|
+
### Context: Using in ERB
|
74
|
+
|
75
|
+
In order to take advantage of this syntax we want to pass the binding of a `Diggy` object to `ERB`
|
76
|
+
|
77
|
+
Given an `ERB` template
|
78
|
+
```ruby
|
79
|
+
require 'erb'
|
80
|
+
let(:template_text) { "<%= data.person.name %>" }
|
81
|
+
```
|
82
|
+
|
83
|
+
And a `Lab42::DiggyMethod` instance
|
84
|
+
```ruby
|
85
|
+
let(:data) { Diggy(data: {person: {name: "YHS"}}) }
|
86
|
+
```
|
87
|
+
|
88
|
+
Then we can pass the binding to the template
|
89
|
+
```ruby
|
90
|
+
expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
|
91
|
+
```
|
92
|
+
|
93
|
+
### Context: Iteration
|
94
|
+
|
95
|
+
If an element in a diggy object is an array we descend
|
96
|
+
|
97
|
+
Given yet another diggy™
|
98
|
+
```ruby
|
99
|
+
let(:diggy) { Diggy(a: [b: 1, c: 2]) }
|
100
|
+
```
|
101
|
+
|
102
|
+
Then we get an enumerator
|
103
|
+
```ruby
|
104
|
+
expect(diggy.a).to be_an(Array)
|
105
|
+
```
|
106
|
+
|
107
|
+
And we can use it to iterate
|
108
|
+
```ruby
|
109
|
+
expect(diggy.a.first.b).to eq(1)
|
110
|
+
```
|
111
|
+
|
63
112
|
# LICENSE
|
64
113
|
|
65
114
|
Copyright 2022 Robert Dober robert.dober@gmail.com
|
data/lib/lab42/diggy_methods.rb
CHANGED
@@ -3,15 +3,18 @@
|
|
3
3
|
require_relative 'diggy_methods/kernel'
|
4
4
|
module Lab42
|
5
5
|
class DiggyMethods
|
6
|
-
|
6
|
+
|
7
|
+
# Expose for ERB
|
8
|
+
def __binding__; binding end
|
7
9
|
|
8
10
|
# In case data has key :data (often the case)
|
9
11
|
def __data__; @data end
|
10
12
|
|
11
13
|
private
|
12
14
|
|
13
|
-
def initialize(a=nil, **data_)
|
15
|
+
def initialize(a=nil, __key_chain__: [], **data_)
|
14
16
|
@data = _make_data_from_args(a, data_)
|
17
|
+
@keychain = __key_chain__
|
15
18
|
end
|
16
19
|
|
17
20
|
def _make_data_from_args(a, data_)
|
@@ -35,14 +38,35 @@ module Lab42
|
|
35
38
|
raise ArgumentError, "positional argument must be a hash or respond to to_h if present"
|
36
39
|
end
|
37
40
|
|
38
|
-
def method_missing(name)
|
39
|
-
|
41
|
+
def method_missing(name, *)
|
42
|
+
if name.to_s.end_with?"!"
|
43
|
+
@data.fetch(name.to_s.sub(/!\z/, "").to_sym)
|
44
|
+
else
|
45
|
+
_method_missing_try_descend(name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def _make_diggy_array(found)
|
50
|
+
found.map { self.class.new(_1) }
|
51
|
+
end
|
52
|
+
|
53
|
+
def _maybe_make_diggy(found)
|
40
54
|
if found.respond_to?(:to_h)
|
41
|
-
self.class.new(**found)
|
55
|
+
self.class.new(**found, __key_chain__: @keychain)
|
42
56
|
else
|
43
57
|
found
|
44
58
|
end
|
45
59
|
end
|
60
|
+
|
61
|
+
def _method_missing_try_descend(name)
|
62
|
+
@keychain << name
|
63
|
+
found = @data.fetch(name) { raise KeyError, "key not found: #{@keychain.join(".")}" }
|
64
|
+
if Array === found
|
65
|
+
_make_diggy_array(found)
|
66
|
+
else
|
67
|
+
_maybe_make_diggy(found)
|
68
|
+
end
|
69
|
+
end
|
46
70
|
end
|
47
71
|
end
|
48
72
|
# 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.4
|
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-
|
11
|
+
date: 2022-02-02 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
|