hash_mapper 0.2.5 → 0.2.6

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
  SHA256:
3
- metadata.gz: 3bddd2af1ce1793dad8c629df875b3896616603d26ca85d1373acf19bc366c8a
4
- data.tar.gz: c3c7d3ea740effd2faa8e870025e1bdff510e99fd6d03981be91facb23f014b1
3
+ metadata.gz: e24c6fe7171260dd214d3b5b1f5250afe1038a63702b1cc0db74dece9b648cd1
4
+ data.tar.gz: b27d3e3af82035e1bd5192906b4dad65a918a9a928513c854884ed2bed500709
5
5
  SHA512:
6
- metadata.gz: ae9aa35b6b73b898c1db69c1fe154578035286f1ed4bc77d8d413aa44b8225b8425c0b40f897236a07e9ba2b96520bc7c09b80d63d286cd4e2a0f8f1ecc05822
7
- data.tar.gz: 5c708bb349d7aa178a5461e3e6076cb838ee3aef005eebc7437f85b135dc79c268f7dc08436211959f637ceb94a34ce0e291cc3eb7990a76757f15b2b665e797
6
+ metadata.gz: 8fbc136b57c7e6bd5fcf435f202f6a6adf444a7f2c4982311bd9dd0ff3fd1ea99fb0b4f9cedbc71ba8a17cc08c7b0da9a4d55e9b7c22da5bb5b1d2e055914a43
7
+ data.tar.gz: 100954ccf11324b9b54ce471a3dafcb0ba7cc68e69b2cedd37e610c325ad7d12e3c2c61fdb3686721383f7a4dc0e80b765b21e3c1ba1a804ef586851e8106373
data/lib/hash_mapper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'hash_mapper/version'
2
4
 
3
5
  $:.unshift(File.dirname(__FILE__)) unless
@@ -138,7 +140,7 @@ module HashMapper
138
140
  def process_into(output, input, method_name: :normalize, context: nil)
139
141
  path_1, path_2 = (method_name == :normalize ? [path_from, path_to] : [path_to, path_from])
140
142
  value = get_value_from_input(output, input, path_1, method_name: method_name, context: context)
141
- set_value_in_output(output, path_2, value)
143
+ set_value_in_output(output, path_2, value, context: context)
142
144
  end
143
145
  protected
144
146
 
@@ -155,7 +157,7 @@ module HashMapper
155
157
  delegated_mapper ? delegate_to_nested_mapper(value, method_name, context: context) : value
156
158
  end
157
159
 
158
- def set_value_in_output(output, path, value)
160
+ def set_value_in_output(output, path, value, context: context)
159
161
  if value == NO_VALUE
160
162
  if default_value == NO_DEFAULT
161
163
  return
@@ -163,7 +165,7 @@ module HashMapper
163
165
  value = default_value
164
166
  end
165
167
  end
166
- add_value_to_hash!(output, path, value)
168
+ add_value_to_hash!(output, path, value, context: context)
167
169
  end
168
170
 
169
171
  def delegate_to_nested_mapper(value, method_name, context:)
@@ -177,13 +179,13 @@ module HashMapper
177
179
  end
178
180
  end
179
181
 
180
- def add_value_to_hash!(hash, path, value)
182
+ def add_value_to_hash!(hash, path, value, context: nil)
181
183
  path.inject_with_index(hash) do |h,e,i|
182
184
  if !h[e].nil? # it can be FALSE
183
185
  h[e]
184
186
  else
185
187
  h[e] = if i == path.size-1
186
- path.apply_filter(value)
188
+ path.apply_filter(value, context: context)
187
189
  else
188
190
  if path.segments[i+1].is_a? Integer
189
191
  []
@@ -204,17 +206,24 @@ module HashMapper
204
206
  include Enumerable
205
207
 
206
208
  attr_reader :segments
207
- attr_writer :filter
208
209
  attr_reader :path
209
210
 
210
211
  def initialize(path)
211
212
  @path = path.dup
212
213
  @segments = parse(path)
213
214
  @filter = lambda{|value| value}# default filter does nothing
215
+ @filter_arity = 1
216
+ end
217
+
218
+ def filter=(f)
219
+ @filter_arity = f.respond_to?(:arity) ? f.arity : 1
220
+ @filter = f
214
221
  end
215
222
 
216
- def apply_filter(value)
217
- @filter.call(value)
223
+ def apply_filter(value, context: nil)
224
+ args = [value]
225
+ args << context if @filter_arity > 1
226
+ @filter.call(*args)
218
227
  end
219
228
 
220
229
  def each(&blk)
@@ -1,3 +1,3 @@
1
1
  module HashMapper
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
@@ -628,3 +628,19 @@ describe 'passing custom context object' do
628
628
  expect(ctx[:names]).to eq(%w(Ismael Joe))
629
629
  end
630
630
  end
631
+
632
+ describe 'passing context down to filters' do
633
+ it 'yields context to filters' do
634
+ mapper = Class.new do
635
+ extend HashMapper
636
+
637
+ map from('/name'), to('/name', &(->(name, ctx) { "#{ctx[:title]} #{name}" }))
638
+ map from('/age'), to('/age') do |age, ctx|
639
+ "#{age} #{ctx[:age_suffix]}"
640
+ end
641
+ end
642
+
643
+ output = mapper.normalize({ name: 'Ismael', age: 43 }, context: { title: 'Mr.', age_suffix: 'years old' })
644
+ expect(output).to eq({ name: 'Mr. Ismael', age: '43 years old' })
645
+ end
646
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ismael Celis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-02 00:00:00.000000000 Z
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport