lab42_diggy_methods 0.1.1 → 0.1.6

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: 7c7c506c170e95600888b44e102b565f4c6fa3fe52bf7dcd34c935d204e9980a
4
- data.tar.gz: df9c49145cd0780be53b8e1fbc4c568d53064cb35200839d3f80baace035b0b3
3
+ metadata.gz: 3052880cc5f39b51c1ee8264afb99d24ddf2dc6625a8a50131c22a3511a3f6f4
4
+ data.tar.gz: 466ec786a0722d71c35d7bda70525c0f277c008e4b6d86d23fa1e666ec3953a3
5
5
  SHA512:
6
- metadata.gz: f316245eed051450bd831d7b378376ba4e9fbd515b4efad4b670aeed2949a98de239d2ec2d81db5067272c446b067a86ab805b90779cd73afc412ec9539b93a1
7
- data.tar.gz: c7965c692a9ce203a079482a939b6f3f6ff16a60fcc2307fd4fcfeb40224a5a20c42c4c687f64c6e1f6375e5bff5ebe07ae995111e660e08fd255095c3d2fd3d
6
+ metadata.gz: caef67120413bd91d84630ab0414e0c598d2d00908579b4050a6f15b4833baaf5ca81bdec5c11c6f6ecdb4f70a7ce1b97619262e1b26028315130499b0e33534
7
+ data.tar.gz: e795c99a9d373a9effe7a41554d0c0e796f2c823a94bad7c97d436f5ee5c3c8144fcb011b8c1d22dec1488a4b9299a6f9f3226440f24ae9d0dccf5c2f74cbabd
data/README.md CHANGED
@@ -48,9 +48,25 @@ 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: :f")
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")
54
70
  ```
55
71
 
56
72
  If we access unknown keys we get the usual `KeyError` error, however we must not pass, nonhashable data
@@ -79,6 +95,26 @@ Then we can pass the binding to the template
79
95
  ```ruby
80
96
  expect(ERB.new(template_text).result(data.__binding__)).to eq("YHS")
81
97
  ```
98
+
99
+ ### Context: Iteration
100
+
101
+ If an element in a diggy object is an array we descend
102
+
103
+ Given yet another diggy™
104
+ ```ruby
105
+ let(:diggy) { Diggy(a: [b: 1, c: 2]) }
106
+ ```
107
+
108
+ Then we get an enumerator
109
+ ```ruby
110
+ expect(diggy.a).to be_an(Array)
111
+ ```
112
+
113
+ And we can use it to iterate
114
+ ```ruby
115
+ expect(diggy.a.first.b).to eq(1)
116
+ ```
117
+
82
118
  # LICENSE
83
119
 
84
120
  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.1"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
8
8
  # SPDX-License-Identifier: Apache-2.0
@@ -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,14 +38,35 @@ 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
- found = @data.fetch(name)
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, __key_chain__: @key_chain) }
51
+ end
52
+
53
+ def _maybe_make_diggy(found)
42
54
  if found.respond_to?(:to_h)
43
- self.class.new(**found)
55
+ self.class.new(**found, __key_chain__: @key_chain)
44
56
  else
45
57
  found
46
58
  end
47
59
  end
60
+
61
+ def _method_missing_try_descend(name)
62
+ @key_chain << name
63
+ found = @data.fetch(name) { raise KeyError, "key not found: #{@key_chain.join(".")}" }
64
+ if Array === found
65
+ _make_diggy_array(found)
66
+ else
67
+ _maybe_make_diggy(found)
68
+ end
69
+ end
48
70
  end
49
71
  end
50
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.1
4
+ version: 0.1.6
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-01-31 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
@@ -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/lab42_diggy_methods
28
+ homepage: https://github.com/robertdober/diggy_methods
29
29
  licenses:
30
30
  - Apache-2.0
31
31
  metadata: {}