lab42_open_map 0.1.1 → 0.1.2
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 +38 -0
- data/lib/lab42/open_map.rb +1 -1
- data/lib/lab42/open_map/implementation.rb +5 -0
- data/lib/lab42/open_map/version.rb +1 -1
- 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: e37dfce43b61fdd85eed4129ebdbf66072bb71e06c09ea802fb59b5a931502e0
|
4
|
+
data.tar.gz: 4a3cddf4dedd4f5028df0183c050d4e3b6f69ae2859a3f92c263442dd1aa3616
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f11782f900564870141e1116418906e6f23628c0f99a5143c72664a77d838f992faec3747119d162c51a8a95461b745eb57dd7ed1ccf14a07a54ecfb5322c666
|
7
|
+
data.tar.gz: 4de418bd3a871bce756aa574f6700d868b0a866d4b7272cb1734436681fdf6a0b9ca94e175cb0b149e8bc0188064198394b33a0a6632d9ba6b49cf7388b5130f
|
data/README.md
CHANGED
@@ -142,6 +142,44 @@ And with `sans` we get this
|
|
142
142
|
expect( garfield.values ).to eq(["Garfield", 1976, "Jim Davis"])
|
143
143
|
```
|
144
144
|
|
145
|
+
## Context Hash like Protocol
|
146
|
+
|
147
|
+
We have already seen that `[]`, `slice`, `size` and friends act like on hashes, let us document
|
148
|
+
the other _Hashlike_ methods here
|
149
|
+
|
150
|
+
Given a nice little `OpenMap`
|
151
|
+
```ruby
|
152
|
+
let(:my_map) { OpenMap.new(street: "Champs Elysée", city: "Paris", country: "France") }
|
153
|
+
```
|
154
|
+
|
155
|
+
### `fetch`
|
156
|
+
|
157
|
+
Then we can fetch existing values
|
158
|
+
```ruby
|
159
|
+
expect( my_map.fetch(:city) ).to eq("Paris")
|
160
|
+
```
|
161
|
+
And we have to be a little bit more careful with non existing values
|
162
|
+
```ruby
|
163
|
+
expect( my_map.fetch(:zip, 75008) ).to eq(75008)
|
164
|
+
expect( my_map.fetch(:number) { 42 } ).to eq(42)
|
165
|
+
expect{ my_map.fetch(:continent) }.to raise_error(KeyError, "key not found: :continent" )
|
166
|
+
```
|
167
|
+
|
168
|
+
### Pattern Matching with `deconstruct_keys`
|
169
|
+
|
170
|
+
And we can pattern match
|
171
|
+
```ruby
|
172
|
+
my_map in {city: city, street: street}
|
173
|
+
expect( [street, city] ).to eq(["Champs Elysée", "Paris"])
|
174
|
+
expect{ my_map in {city: "Bordeaux"} }.to raise_error(NoMatchingPatternError)
|
175
|
+
```
|
176
|
+
|
177
|
+
### `entries`
|
178
|
+
|
179
|
+
And with not much to say about
|
180
|
+
```ruby
|
181
|
+
expect( my_map.entries ).to eq([[:street, "Champs Elysée"], [:city, "Paris"], [:country, "France"]])
|
182
|
+
```
|
145
183
|
|
146
184
|
# LICENSE
|
147
185
|
|
data/lib/lab42/open_map.rb
CHANGED
@@ -4,7 +4,7 @@ module Lab42
|
|
4
4
|
class OpenMap
|
5
5
|
extend Forwarder
|
6
6
|
include Enumerable
|
7
|
-
forward_all :[], :each, :each_pair, :empty?, :has_key?, :inject, :keys, :size, :slice, :values, to: :@hash
|
7
|
+
forward_all :[], :each, :each_pair, :empty?, :fetch, :has_key?, :inject, :keys, :size, :slice, :values, to: :@hash
|
8
8
|
|
9
9
|
include Implementation
|
10
10
|
|
@@ -1,3 +1,4 @@
|
|
1
|
+
Warning[:experimental] = false
|
1
2
|
|
2
3
|
module Lab42
|
3
4
|
class OpenMap
|
@@ -7,6 +8,10 @@ module Lab42
|
|
7
8
|
@hash[key] = value
|
8
9
|
end
|
9
10
|
|
11
|
+
def deconstruct_keys(keys)
|
12
|
+
@hash.slice(*keys)
|
13
|
+
end
|
14
|
+
|
10
15
|
def merge(**kwds)
|
11
16
|
_check_symbolic_keys!(kwds)
|
12
17
|
self.class.new(**@hash.merge(kwds))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lab42_open_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Dober
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-12-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lab42_result
|