hash_map 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/lib/hash_map/mapper.rb +1 -8
- data/lib/hash_map/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c4012172f5ca69e9686260220452ccdc5f7fd4d2
|
4
|
+
data.tar.gz: 0a8a23e6add10541e7fb5b0cf81f20c595897d33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2537edf596a989fd26c3f9bbd99f45fbc2a952187fd6bf80aebc47fdb97f4af2ed086012ec9bd4f7faa134b46b3f25ec5a217a93c9e52a1ae974def8cdb2d50
|
7
|
+
data.tar.gz: f3982bbf5f523cc049d0e7213f3bda29da840f18176acc56ad6bf1f4253853c1cb35ec3af79764808d6bd62ac165893f1d47c8669c88d6ca6dc9c45c982db0ea
|
data/README.md
CHANGED
@@ -124,6 +124,22 @@ class Collections < HashMap::Base
|
|
124
124
|
end
|
125
125
|
```
|
126
126
|
|
127
|
+
The collection method always treats the value as an `Array` and it always returns an `Array`. If the value is not an `Array` it will be wrapped in a new one. If the value is `nil` it always returns `[]`.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
Collections.map({ things: nil})
|
131
|
+
=> {
|
132
|
+
things: []
|
133
|
+
numbers: []
|
134
|
+
}
|
135
|
+
|
136
|
+
Collections.map({ numbers: '1'})
|
137
|
+
=> {
|
138
|
+
things: []
|
139
|
+
numbers: [1]
|
140
|
+
}
|
141
|
+
```
|
142
|
+
|
127
143
|
**Methods:**
|
128
144
|
|
129
145
|
You can create your helpers in the mapper and call them inside the block
|
data/lib/hash_map/mapper.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module HashMap
|
2
2
|
class Mapper
|
3
|
-
class CanNotMapCollection < StandardError; end
|
4
3
|
attr_reader :original, :hash_map
|
5
4
|
def initialize(original, hash_map)
|
6
5
|
@original = HashWithIndifferentAccess.new(original)
|
@@ -31,16 +30,10 @@ module HashMap
|
|
31
30
|
|
32
31
|
def map_collection(struct)
|
33
32
|
value = get_value_from_key(struct)
|
34
|
-
|
33
|
+
value = Array.wrap(value)
|
35
34
|
value.map { |elem| struct[:mapper].call(elem) }
|
36
35
|
end
|
37
36
|
|
38
|
-
def errors_for_collection(value, struct)
|
39
|
-
unless value.respond_to?(:map)
|
40
|
-
fail CanNotMapCollection, "'#{value.class}' does not respond to '#map'"
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
37
|
def get_value_from_key(struct, from = :from)
|
45
38
|
struct[from].inject(original) do |output, k|
|
46
39
|
break unless output.respond_to?(:[])
|
data/lib/hash_map/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Pañach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|