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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eecd42833f732acc06ac6bb480e914a2e4410f79
4
- data.tar.gz: 6a52704178d7f42c5dce703ded69da6dd1e8be7e
3
+ metadata.gz: 009df32bde8c5f75cdf065ff8feb8c56a34e613e
4
+ data.tar.gz: 83ec663c7783abec75fd9bb19369075b2c46976c
5
5
  SHA512:
6
- metadata.gz: 49a7ed40a89d7d78064eefe4d1e91d8efa4ce9b69e18ad0e012f1794922edbcb0da005f1e7580c8a61ea91bc384d9cb3717efa0b48e2fdbaf2568faa03598610
7
- data.tar.gz: fe30f661e0fe6b52bb0e7bfa8930d4f70509e72a45287d83f89a59d4a5fb1949b7a3c08996122823135d869ff7f87ede894f34219220a456868db60fdd2761a0
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
@@ -6,6 +6,7 @@ module HashMap
6
6
  def self.map(input)
7
7
  new(input).output
8
8
  end
9
+ singleton_class.send(:alias_method, :call, :map)
9
10
 
10
11
  def mapper
11
12
  @mapper ||= Mapper.new(original, self)
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 "[Depercation Warning] using: #{__callee__} use from_child instead"
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 "[Depercation Warning] using: #{__callee__} use to_child instead"
68
+ puts "[HashMap Deprecation Warning] using: #{__callee__} use to_child instead"
60
69
  to_child(key, opts, &block)
61
70
  end
62
71
 
@@ -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[:proc]
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?(:[])
@@ -1,3 +1,3 @@
1
1
  module HashMap
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_map
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artur Pañach