hash_map 0.3.1 → 0.3.2
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/base.rb +1 -0
- data/lib/hash_map/dsl.rb +11 -2
- data/lib/hash_map/mapper.rb +16 -1
- data/lib/hash_map/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 009df32bde8c5f75cdf065ff8feb8c56a34e613e
|
4
|
+
data.tar.gz: 83ec663c7783abec75fd9bb19369075b2c46976c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d7dfcef7e69885a271fef3bcf86fa62a5aaab21640de5c23828ffd65e6b49871a0ee8dff2bb96b6a35711221281bda91c4a1d77a408272fc976357e759b0c8a
|
7
|
+
data.tar.gz: dea4676ba01aa6e0300b491d3a747746549cc3eb48f5ac78166a3d2f6ffa64b5af11a55ba9b0c9fb3ddb2b8440bfebf168931749bbb20b197c6e2061abe85255
|
data/README.md
CHANGED
@@ -108,6 +108,22 @@ class Properties < HashMap::Base
|
|
108
108
|
properties :name, :address, :house
|
109
109
|
end
|
110
110
|
```
|
111
|
+
|
112
|
+
**Collections:**
|
113
|
+
|
114
|
+
You can map collections passing the mapper option, can be another mapper a proc or anything responding to `.call` with one argument.
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class Thing < HashMap::Base
|
118
|
+
properties :name, :age
|
119
|
+
end
|
120
|
+
|
121
|
+
class Collections < HashMap::Base
|
122
|
+
collection :things, mapper: Thing
|
123
|
+
collection :numbers, mapper: proc { |n| n.to_i }
|
124
|
+
end
|
125
|
+
```
|
126
|
+
|
111
127
|
**Methods:**
|
112
128
|
|
113
129
|
You can create your helpers in the mapper and call them inside the block
|
data/lib/hash_map/base.rb
CHANGED
data/lib/hash_map/dsl.rb
CHANGED
@@ -21,6 +21,7 @@ module HashMap
|
|
21
21
|
end
|
22
22
|
|
23
23
|
class DSL
|
24
|
+
class NoMapperForCollection < StandardError; end
|
24
25
|
attr_reader :attributes
|
25
26
|
|
26
27
|
def initialize
|
@@ -35,6 +36,14 @@ module HashMap
|
|
35
36
|
new_hash
|
36
37
|
end
|
37
38
|
|
39
|
+
def collection(key, opts = {}, &block)
|
40
|
+
unless opts[:mapper]
|
41
|
+
fail NoMapperForCollection, "[HashMap Error] Called 'collection' without the ':mapper' option"
|
42
|
+
end
|
43
|
+
opts.merge!(is_collection: true)
|
44
|
+
property(key, opts, &block)
|
45
|
+
end
|
46
|
+
|
38
47
|
def properties(*args)
|
39
48
|
args.each do |arg|
|
40
49
|
property(*arg)
|
@@ -42,7 +51,7 @@ module HashMap
|
|
42
51
|
end
|
43
52
|
|
44
53
|
def from_children(key, opts = {}, &block)
|
45
|
-
puts "[
|
54
|
+
puts "[HashMap Deprecation Warning] using: #{__callee__} use from_child instead"
|
46
55
|
from_child(key, opts, &block)
|
47
56
|
end
|
48
57
|
|
@@ -56,7 +65,7 @@ module HashMap
|
|
56
65
|
end
|
57
66
|
|
58
67
|
def to_children(key, opts = {}, &block)
|
59
|
-
puts "[
|
68
|
+
puts "[HashMap Deprecation Warning] using: #{__callee__} use to_child instead"
|
60
69
|
to_child(key, opts, &block)
|
61
70
|
end
|
62
71
|
|
data/lib/hash_map/mapper.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module HashMap
|
2
2
|
class Mapper
|
3
|
+
class CanNotMapCollection < StandardError; end
|
3
4
|
attr_reader :original, :hash_map
|
4
5
|
def initialize(original, hash_map)
|
5
6
|
@original = HashWithIndifferentAccess.new(original)
|
@@ -18,7 +19,9 @@ module HashMap
|
|
18
19
|
private
|
19
20
|
|
20
21
|
def get_value(struct)
|
21
|
-
value = if struct[:
|
22
|
+
value = if struct[:is_collection]
|
23
|
+
map_collection(struct)
|
24
|
+
elsif struct[:proc]
|
22
25
|
execute_block(struct)
|
23
26
|
elsif struct[:from]
|
24
27
|
get_value_from_key(struct)
|
@@ -26,6 +29,18 @@ module HashMap
|
|
26
29
|
nil_to_default(value, struct)
|
27
30
|
end
|
28
31
|
|
32
|
+
def map_collection(struct)
|
33
|
+
value = get_value_from_key(struct)
|
34
|
+
errors_for_collection(value, struct)
|
35
|
+
value.map { |elem| struct[:mapper].call(elem) }
|
36
|
+
end
|
37
|
+
|
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
|
+
|
29
44
|
def get_value_from_key(struct, from = :from)
|
30
45
|
struct[from].inject(original) do |output, k|
|
31
46
|
break unless output.respond_to?(:[])
|
data/lib/hash_map/version.rb
CHANGED