hash_map 0.3.7 → 0.4.0

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: e04f1953cd197e037e9a3a3b0c2381d0ea878062
4
- data.tar.gz: 6b769f5fce5ca758b17eac83a7e5da12d2e81c77
3
+ metadata.gz: 8087bb03fb5521f63c3fc63689b7047610996f04
4
+ data.tar.gz: 139b3dbac032b2799a5bc825c0473a1c6f2b6954
5
5
  SHA512:
6
- metadata.gz: 5fa67a56292f6e9368780f024b8dda1c3c93d58f357f6acf8e1c2e5824ec15486e38189ad511ae96596fa57f04e06577ea129065bd0b20301cae5e07baf5925a
7
- data.tar.gz: 6dc6282c26cd48a0a9940a85eefc330aa2ac38766a255ba70c69d0140fdd143df95cdf8b0bf45ccff552b234893a2b05eb029a3fc84afe4e804414f26e726852
6
+ metadata.gz: 4a1713d1a9e4c18474b7f86626b0c8c51a0b12c92619ac4bc1fbae3e75403cc0780de1732c473f230aed2799e9ed70f09f27ee39d0274822c404beb54b28bc39
7
+ data.tar.gz: 50292c55159b07a14421df7dfc4720d447f4b2e25b16e5d4b847195ebb32dd9f9da75a244966cbdb6124974245224d3d3578223a76695ad9b931485c31f847cf
data/README.md CHANGED
@@ -232,6 +232,99 @@ Blocks.map(hash)
232
232
 
233
233
  ```
234
234
 
235
+ ### Middlewares
236
+
237
+ #### transforms_output
238
+ ```ruby
239
+ original = {
240
+ "StatusCode" => 200,
241
+ "ErrorDescription" => nil,
242
+ "Messages" => nil,
243
+ "CompanySettings" => {
244
+ "CompanyIdentity" => {
245
+ "CompanyGuid" => "0A6005FA-161D-4290-BB7D-B21B14313807",
246
+ "PseudoCity" => {
247
+ "Code" => "PARTQ2447"
248
+ }
249
+ },
250
+ "IsCertifyEnabled" => false,
251
+ "IsProfileEnabled" => true,
252
+ "PathMobileConfig" => nil
253
+ }
254
+ }
255
+
256
+ class TransformsOutput < HashMap::Base
257
+ transforms_output HashMap::UnderscoreKeys
258
+ from_child 'CompanySettings' do
259
+ from_child 'CompanyIdentity' do
260
+ property 'CompanyGuid'
261
+ end
262
+ properties 'IsCertifyEnabled', 'IsProfileEnabled', 'PathMobileConfig'
263
+ end
264
+ end
265
+
266
+ TransformsOutput.call(original)
267
+ # => {:company_guid=>"0A6005FA-161D-4290-BB7D-B21B14313807", :is_certify_enabled=>false, :is_profile_enabled=>true, :path_mobile_config=>nil}
268
+ ```
269
+
270
+ #### Transforms input
271
+
272
+ ```ruby
273
+ original = {
274
+ "StatusCode" => 200,
275
+ "ErrorDescription" => nil,
276
+ "Messages" => nil,
277
+ "CompanySettings" => {
278
+ "CompanyIdentity" => {
279
+ "CompanyGuid" => "0A6005FA-161D-4290-BB7D-B21B14313807",
280
+ "PseudoCity" => {
281
+ "Code" => "PARTQ2447"
282
+ }
283
+ },
284
+ "IsCertifyEnabled" => false,
285
+ "IsProfileEnabled" => true,
286
+ "PathMobileConfig" => nil
287
+ }
288
+ }
289
+
290
+ class TransformsInput < HashMap::Base
291
+ transforms_input HashMap::UnderscoreKeys
292
+ from_child :company_settings do
293
+ from_child :company_identity do
294
+ property :company_guid
295
+ end
296
+ properties :is_certify_enabled, :is_profile_enabled, :path_mobile_config
297
+ end
298
+ end
299
+
300
+ TransformsInput.call(original)
301
+ # => {:company_guid=>"0A6005FA-161D-4290-BB7D-B21B14313807", :is_certify_enabled=>false, :is_profile_enabled=>true, :path_mobile_config=>nil}
302
+ ```
303
+
304
+
305
+ #### After each
306
+
307
+ ```ruby
308
+ class AfterEach < HashMap::Base
309
+ properties :name, :age
310
+ after_each HashMap::BlankToNil, HashMap::StringToBoolean
311
+ end
312
+
313
+ blanks = {
314
+ name: '',
315
+ age: ''
316
+ }
317
+ booleans = {
318
+ name: 'true',
319
+ age: 'false'
320
+ }
321
+ AfterEach.call(blanks)
322
+ #=> {"name"=>nil, "age"=>nil}
323
+
324
+ AfterEach.call(booleans)
325
+ #=> {"name"=>true, "age"=>false}
326
+ ```
327
+
235
328
  ### JSON Adapter
236
329
  ```ruby
237
330
  class UserMapper < HashMap::Base
data/lib/hash_map.rb CHANGED
@@ -9,7 +9,7 @@ require 'hash_map/dsl'
9
9
  require 'hash_map/mapper'
10
10
  require 'hash_map/base'
11
11
  require 'hash_map/json_adapter'
12
+ require 'hash_map/plugins'
12
13
 
13
14
  require 'hash_map/core_ext/hash'
14
15
  require 'hash_map/core_ext/string'
15
-
data/lib/hash_map/base.rb CHANGED
@@ -17,7 +17,7 @@ module HashMap
17
17
 
18
18
  attr_reader :original
19
19
  def initialize(original)
20
- @original = prepare_input(original)
20
+ @original = _transforms_input(prepare_input(original))
21
21
  end
22
22
 
23
23
  def mapper
@@ -25,13 +25,33 @@ module HashMap
25
25
  end
26
26
 
27
27
  def output
28
- @output ||= mapper.output
28
+ @output ||= _transforms_output(mapper.output)
29
29
  end
30
30
  alias_method :to_h, :output
31
31
  alias_method :to_hash, :output
32
32
 
33
33
  private
34
34
 
35
+ def _transforms_output(output)
36
+ if middlewares = self.class.dsl.instance_variable_get(:@transform_output)
37
+ middlewares.inject(output) do |out, proccess|
38
+ proccess.call(out)
39
+ end
40
+ else
41
+ output
42
+ end
43
+ end
44
+
45
+ def _transforms_input(input)
46
+ if middlewares = self.class.dsl.instance_variable_get(:@transform_input)
47
+ middlewares.inject(input) do |out, proccess|
48
+ proccess.call(out)
49
+ end
50
+ else
51
+ input
52
+ end
53
+ end
54
+
35
55
  def prepare_input(input)
36
56
  case input
37
57
  when ->(str) { str.class <= String }
data/lib/hash_map/dsl.rb CHANGED
@@ -30,6 +30,21 @@ module HashMap
30
30
  @attributes = []
31
31
  end
32
32
 
33
+ def after_each(*middlewares)
34
+ @after_each ||= []
35
+ @after_each += middlewares
36
+ end
37
+
38
+ def transforms_output(*middlewares)
39
+ @transform_output ||= []
40
+ @transform_output += middlewares
41
+ end
42
+
43
+ def transforms_input(*middlewares)
44
+ @transform_input ||= []
45
+ @transform_input += middlewares
46
+ end
47
+
33
48
  def _set_attributes(attrs)
34
49
  @attributes = attrs
35
50
  end
@@ -28,6 +28,18 @@ module HashMap
28
28
  nil_to_default(value, struct)
29
29
  end
30
30
 
31
+ def after_each_middleware(value, key)
32
+ after_each_callbacks.inject(value) do |output, middle|
33
+ middle.call(output)
34
+ end
35
+ end
36
+
37
+ def after_each_callbacks
38
+ hash_map.class.dsl.after_each
39
+ rescue
40
+ []
41
+ end
42
+
31
43
  def map_collection(struct)
32
44
  value = get_value_from_key(struct)
33
45
  value = Array.wrap(value)
@@ -35,20 +47,22 @@ module HashMap
35
47
  end
36
48
 
37
49
  def get_value_from_key(struct, from = :from)
38
- struct[from].inject(original) do |output, k|
50
+ value = struct[from].inject(original) do |output, k|
39
51
  break unless output.respond_to?(:[])
40
52
  output.send(:[], k)
41
53
  end
54
+ after_each_middleware(value, struct[:key].last)
42
55
  end
43
56
 
44
57
  def execute_block(struct)
45
58
  block = struct[:proc]
46
- if struct[:from_child]
59
+ value = if struct[:from_child]
47
60
  nested = get_value_from_key(struct, :from_child)
48
61
  hash_map.instance_exec nested, original, &block
49
62
  else
50
63
  hash_map.instance_exec original, original, &block
51
64
  end
65
+ after_each_middleware(value, struct[:key].last)
52
66
  end
53
67
 
54
68
  def build_keys(ary, value)
@@ -0,0 +1,15 @@
1
+ module HashMap
2
+ BlankToNil = lambda do |v|
3
+ v.blank? ? nil : v
4
+ end
5
+
6
+ StringToBoolean = lambda do |v|
7
+ return false if v == 'false'
8
+ return true if v == 'true'
9
+ v
10
+ end
11
+
12
+ UnderscoreKeys = lambda do |output|
13
+ output.deep_transform_keys{ |k| k.underscore.to_sym }
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module HashMap
2
- VERSION = "0.3.7"
2
+ VERSION = "0.4.0"
3
3
  end
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.7
4
+ version: 0.4.0
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: 2016-04-28 00:00:00.000000000 Z
11
+ date: 2016-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,6 +94,7 @@ files:
94
94
  - lib/hash_map/dsl.rb
95
95
  - lib/hash_map/json_adapter.rb
96
96
  - lib/hash_map/mapper.rb
97
+ - lib/hash_map/plugins.rb
97
98
  - lib/hash_map/version.rb
98
99
  homepage: https://github.com/arturictus/hash_map
99
100
  licenses:
@@ -115,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
116
  version: '0'
116
117
  requirements: []
117
118
  rubyforge_project:
118
- rubygems_version: 2.2.3
119
+ rubygems_version: 2.6.3
119
120
  signing_key:
120
121
  specification_version: 4
121
122
  summary: Library to map easily hashes.