lab42_diggy_methods 0.1.2 → 0.1.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: 7f9642e13d0932c3f95de27b981501ef89e4f73f02cb2befaee5ef583294b5eb
4
- data.tar.gz: 6677dde97b01b2a652a06ceb755371ddc13eb6ce6c935eb38a123518c7da37ad
3
+ metadata.gz: ee77b7b8979ba7c58c691afab738e48a3543e58c9f388fb4d3d7c9f3de97675a
4
+ data.tar.gz: df9dc0977027d9a7644e32ffa515930cd25d3fb841f3fd16146bc09bfa5bbd02
5
5
  SHA512:
6
- metadata.gz: 93bf1546ea98a27bf88a16180be58b32616af3c70f8418a1a9978f76b2cdd14083acf144c8f215441a1fa81fdb1a7ed7484f941103a62a25f37748161255c61f
7
- data.tar.gz: 649f3fb72dea384c11a2f86d17c09836dd4b88cfc8762876aa180f0897466f9a01219942e0c99228a36eae5a31b2c24c5d4f9428c217bd2bfb301c695f93ecfe
6
+ metadata.gz: 2ec4ea5bc54cc96aeeeb32c4f576b39bf5eae8563773cba5bfabc0150426e4f49f872e9ebaa1934046b44d036ea1f565a41d99337f6f86962d6390371b073602
7
+ data.tar.gz: b632307a64a67e96144c51adabaf8edcbd2cd0cf72fcb602148e27e9b96bc2999aaf03a7c597b5b9e7af045ec75366851b851ed4f7ed7a892828e2a0d7b9e3e4
data/README.md CHANGED
@@ -90,6 +90,25 @@ Then we can pass the binding to the template
90
90
  expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
91
91
  ```
92
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
+
93
112
  # LICENSE
94
113
 
95
114
  Copyright 2022 Robert Dober robert.dober@gmail.com
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Lab42
4
4
  class DiggyMethods
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: Apache-2.0
@@ -37,7 +37,7 @@ module Lab42
37
37
  raise ArgumentError, "positional argument must be a hash or respond to to_h if present"
38
38
  end
39
39
 
40
- def method_missing(name)
40
+ def method_missing(name, *)
41
41
  if name.to_s.end_with?"!"
42
42
  @data.fetch(name.to_s.sub(/!\z/, "").to_sym)
43
43
  else
@@ -45,14 +45,26 @@ module Lab42
45
45
  end
46
46
  end
47
47
 
48
- def _method_missing_try_descend(name)
49
- found = @data.fetch(name)
48
+ def _make_diggy_array(found)
49
+ found.map { self.class.new(_1) }
50
+ end
51
+
52
+ def _maybe_make_diggy(found)
50
53
  if found.respond_to?(:to_h)
51
54
  self.class.new(**found)
52
55
  else
53
56
  found
54
57
  end
55
58
  end
59
+
60
+ def _method_missing_try_descend(name)
61
+ found = @data.fetch(name)
62
+ if Array === found
63
+ _make_diggy_array(found)
64
+ else
65
+ _maybe_make_diggy(found)
66
+ end
67
+ end
56
68
  end
57
69
  end
58
70
  # 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.2
4
+ version: 0.1.3
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-01 00:00:00.000000000 Z
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