hash_mapper 0.1.3 → 0.2.0
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 +22 -2
- data/lib/hash_mapper.rb +21 -7
- data/lib/hash_mapper/version.rb +1 -1
- data/spec/hash_mapper_spec.rb +21 -0
- 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: 4ccb1b38f4a738cf5b7f1a147cc998463f14246e
|
4
|
+
data.tar.gz: bb40000c4e4f03cc7cda222bea3f9182189e7faf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71dc27f23fae5c544caa5389fd7a31c1ae0bd04cfa84b007dfc18197f4dbee1349cbd7d677d59e8420505dcd2fa4c981f4c792429580fe05b656f9fad2046dc5
|
7
|
+
data.tar.gz: 68946b78bc3dc7dd2d0b7d287649125b63bb0ffb5f357fddac4cbd48fd1c944b1b7f3880813d0cce8d73b05605e49c8e19e47cec72acb6c9513a8cdf2b5ee596
|
data/README.md
CHANGED
@@ -114,6 +114,26 @@ You guessed it. That means that you can actually pass custom blocks to each to()
|
|
114
114
|
map from('/one'), to('/one'){|value| value.to_i}
|
115
115
|
```
|
116
116
|
|
117
|
+
#### Default values
|
118
|
+
|
119
|
+
You want to make sure that a value is present in the output (even if it's not in the input) so that:
|
120
|
+
|
121
|
+
```ruby
|
122
|
+
{'one' => '1'}
|
123
|
+
```
|
124
|
+
|
125
|
+
gets translated to
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
{:one => 1, :two => 2}
|
129
|
+
```
|
130
|
+
|
131
|
+
Do this:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
map from('/two'), to('/two'), default: 2
|
135
|
+
```
|
136
|
+
|
117
137
|
#### Custom value filtering
|
118
138
|
|
119
139
|
You want to pass the final value of a key through a custom filter:
|
@@ -204,7 +224,7 @@ class ProjectMapper
|
|
204
224
|
extend HashMapper
|
205
225
|
map from('/project'), to('/project_name')
|
206
226
|
map from('/url'), to('/url')
|
207
|
-
map from('/author_names'), to('/author'), using
|
227
|
+
map from('/author_names'), to('/author'), using: UserMapper
|
208
228
|
end
|
209
229
|
```
|
210
230
|
|
@@ -231,7 +251,7 @@ end
|
|
231
251
|
But HashMapper's nested mappers will actually do that for you if a value is an array, so:
|
232
252
|
|
233
253
|
```ruby
|
234
|
-
map from('/employees'), to('employees'), using
|
254
|
+
map from('/employees'), to('employees'), using: UserMapper
|
235
255
|
```
|
236
256
|
... Will map each employee using UserMapper.
|
237
257
|
|
data/lib/hash_mapper.rb
CHANGED
@@ -47,9 +47,9 @@ module HashMapper
|
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
def map(from, to,
|
50
|
+
def map(from, to, options={}, &filter)
|
51
51
|
self.maps = self.maps.dup
|
52
|
-
self.maps << Map.new(from, to,
|
52
|
+
self.maps << Map.new(from, to, options)
|
53
53
|
to.filter = filter if block_given? # Useful if just one block given
|
54
54
|
end
|
55
55
|
|
@@ -62,7 +62,8 @@ module HashMapper
|
|
62
62
|
alias :to :from
|
63
63
|
|
64
64
|
def using(mapper_class)
|
65
|
-
mapper_class
|
65
|
+
warn "[DEPRECATION] `using` is deprecated, instead of `using(#{mapper_class.name})` you should specify `{ using: #{mapper_class.name} }`"
|
66
|
+
{ using: mapper_class }
|
66
67
|
end
|
67
68
|
|
68
69
|
def normalize(a_hash)
|
@@ -113,16 +114,19 @@ module HashMapper
|
|
113
114
|
#
|
114
115
|
class Map
|
115
116
|
|
116
|
-
attr_reader :path_from, :path_to, :delegated_mapper
|
117
|
+
attr_reader :path_from, :path_to, :delegated_mapper, :default_value
|
117
118
|
|
118
|
-
def initialize(path_from, path_to,
|
119
|
-
@path_from
|
119
|
+
def initialize(path_from, path_to, options = {})
|
120
|
+
@path_from = path_from
|
121
|
+
@path_to = path_to
|
122
|
+
@delegated_mapper = options.fetch(:using, nil)
|
123
|
+
@default_value = options.fetch(:default, :hash_mapper_no_default)
|
120
124
|
end
|
121
125
|
|
122
126
|
def process_into(output, input, meth = :normalize)
|
123
127
|
path_1, path_2 = (meth == :normalize ? [path_from, path_to] : [path_to, path_from])
|
124
128
|
value = get_value_from_input(output, input, path_1, meth)
|
125
|
-
|
129
|
+
set_value_in_output(output, path_2, value)
|
126
130
|
end
|
127
131
|
protected
|
128
132
|
|
@@ -139,6 +143,16 @@ module HashMapper
|
|
139
143
|
delegated_mapper ? delegate_to_nested_mapper(value, meth) : value
|
140
144
|
end
|
141
145
|
|
146
|
+
def set_value_in_output(output, path, value)
|
147
|
+
if value == :hash_mapper_no_value
|
148
|
+
if default_value == :hash_mapper_no_default
|
149
|
+
return
|
150
|
+
else
|
151
|
+
value = default_value
|
152
|
+
end
|
153
|
+
end
|
154
|
+
add_value_to_hash!(output, path, value)
|
155
|
+
end
|
142
156
|
|
143
157
|
def delegate_to_nested_mapper(value, meth)
|
144
158
|
case value
|
data/lib/hash_mapper/version.rb
CHANGED
data/spec/hash_mapper_spec.rb
CHANGED
@@ -426,3 +426,24 @@ describe "dealing with strings and symbols" do
|
|
426
426
|
|
427
427
|
end
|
428
428
|
|
429
|
+
class DefaultValues
|
430
|
+
extend HashMapper
|
431
|
+
|
432
|
+
map from('/without_default'), to('not_defaulted')
|
433
|
+
map from('/with_default'), to('defaulted'), default: 'the_default_value'
|
434
|
+
end
|
435
|
+
|
436
|
+
describe "default values" do
|
437
|
+
it "should use a default value whenever a key is not set" do
|
438
|
+
DefaultValues.normalize(
|
439
|
+
'without_default' => 'some_value'
|
440
|
+
).should == { not_defaulted: 'some_value', defaulted: 'the_default_value' }
|
441
|
+
end
|
442
|
+
|
443
|
+
it "should not use a default if a key is set (even if the value is falsy)" do
|
444
|
+
DefaultValues.normalize({
|
445
|
+
'without_default' => 'some_value',
|
446
|
+
'with_default' => false
|
447
|
+
}).should == { not_defaulted: 'some_value', defaulted: false }
|
448
|
+
end
|
449
|
+
end
|