hash_map 0.3.4 → 0.3.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
  SHA1:
3
- metadata.gz: 50e7e3fbec8b04b63f4014962d9c3eb6c9b36682
4
- data.tar.gz: 00b4fb3f90c339c8291ce8c132e7827b962bdbdf
3
+ metadata.gz: 1b34bf9da8846a7016da845a50f12f5b56906135
4
+ data.tar.gz: 90939f2f29f4fe79f824545b14d04a0c1934d9b7
5
5
  SHA512:
6
- metadata.gz: 21035218b21db6eb5cda915085f2564db8126829864ae9faf8bc8adf33c8c5038d0766d10eb4b8ccde05e1c716259542bbe22ad6c43bd1772fb32df61e46d4a4
7
- data.tar.gz: 1845e95b6e6460fc0cd54c1d2edefb6af6f9121976216d2ebfb621ec908bdcad2377a3a746f08b1cdb918fd26a7bc5be3f685263fa5b44993d91e73a7b330926
6
+ metadata.gz: 1dedeaef38d7745b11437abee4bc3ef0a266f924f453449ae367142a084a07feb607e14e7bf75b0c036a036793de3ed0f93f0bd7bc5c8f62f68ab0cd62d8645b
7
+ data.tar.gz: 94c97df1807f5e9ce07993be731bd8256b095a02c9ea9c41fe76e0527499d7ac230decc978fb2ef6d6326c9afce68ac255b5411443c21d9f9bb0585c21cfdf2c
data/README.md CHANGED
@@ -200,6 +200,42 @@ Blocks.map(hash)
200
200
 
201
201
  ```
202
202
 
203
+ ### JSON Adapter
204
+ ```ruby
205
+ class UserMapper < HashMap::Base
206
+ from_child :user do
207
+ properties :name, :surname
208
+ end
209
+ end
210
+ json = %Q[{"user":{"name":"John","surname":"Doe"}}]
211
+ UserMapper.map(json)
212
+ # => {"name"=>"John", "surname"=>"Doe"}
213
+ ```
214
+ ### Core Extensions
215
+
216
+ #### String
217
+ ```ruby
218
+ class UserMapper < HashMap::Base
219
+ from_child :user do
220
+ properties :name, :surname
221
+ end
222
+ end
223
+ json = %Q[{"user":{"name":"John","surname":"Doe"}}]
224
+ json.hash_map_with(UserMapper)
225
+ # => {"name"=>"John", "surname"=>"Doe"}
226
+ ```
227
+ #### Hash
228
+
229
+ ```ruby
230
+ class UserMapper < HashMap::Base
231
+ from_child :user do
232
+ properties :name, :surname
233
+ end
234
+ end
235
+ hash = { user: { name: 'John', surname: 'Doe' } }
236
+ hash.hash_map_with(UserMapper)
237
+ # => {"name"=>"John", "surname"=>"Doe"}
238
+ ```
203
239
 
204
240
 
205
241
  ### Motivation
@@ -231,7 +267,7 @@ user.country = Country.find_by(code: user_hash[:country][:code])
231
267
 
232
268
  solution:
233
269
  ```ruby
234
- user = User.new(MyMapper.map(hash)) # done
270
+ User.create(MyMapper.map(api_response)) # done
235
271
  ```
236
272
 
237
273
  ## Development
data/lib/hash_map.rb CHANGED
@@ -8,3 +8,8 @@ end
8
8
  require 'hash_map/dsl'
9
9
  require 'hash_map/mapper'
10
10
  require 'hash_map/base'
11
+ require 'hash_map/json_adapter'
12
+
13
+ require 'hash_map/core_ext/hash'
14
+ require 'hash_map/core_ext/string'
15
+
data/lib/hash_map/base.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module HashMap
2
- Base = Struct.new(:original) do
2
+ class Base
3
3
  include ToDSL
4
4
  delegate :[], to: :output
5
5
 
@@ -8,6 +8,11 @@ module HashMap
8
8
  end
9
9
  singleton_class.send(:alias_method, :call, :map)
10
10
 
11
+ attr_reader :original
12
+ def initialize(original)
13
+ @original = prepare_input(original)
14
+ end
15
+
11
16
  def mapper
12
17
  @mapper ||= Mapper.new(original, self)
13
18
  end
@@ -17,5 +22,16 @@ module HashMap
17
22
  end
18
23
  alias_method :to_h, :output
19
24
  alias_method :to_hash, :output
25
+
26
+ private
27
+
28
+ def prepare_input(input)
29
+ case input
30
+ when ->(str) { str.class <= String }
31
+ JSONAdapter.call(input)
32
+ else
33
+ input
34
+ end
35
+ end
20
36
  end
21
37
  end
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ # Return the hash with the structure from the Mapper
3
+ #
4
+ # hash = { user: { name: 'John', surname: 'Doe' } }
5
+ # hash.hash_map_with(UserMapper)
6
+ def hash_map_with(mapper)
7
+ mapper.call(self)
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ # Return the string with the structure from the Mapper
3
+ #
4
+ # api_response = "{\"user\":{\"name\":\"John\",\"surname\":\"Doe\"}}"
5
+ # api_response.hash_map_with(UserMapper)
6
+ def hash_map_with(mapper)
7
+ mapper.call(self)
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ module HashMap
2
+ class JSONAdapter
3
+ class InvalidJOSN < StandardError; end
4
+ def self.call(string)
5
+ JSON[string]
6
+ rescue JSON::ParserError
7
+ fail InvalidJOSN, "[HashMap Error] using: `map` with invalid JSON, please check your json before mapping"
8
+ end
9
+ end
10
+ end
@@ -1,3 +1,3 @@
1
1
  module HashMap
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.6"
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.4
4
+ version: 0.3.6
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-03-10 00:00:00.000000000 Z
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -86,7 +86,10 @@ files:
86
86
  - hash_map.gemspec
87
87
  - lib/hash_map.rb
88
88
  - lib/hash_map/base.rb
89
+ - lib/hash_map/core_ext/hash.rb
90
+ - lib/hash_map/core_ext/string.rb
89
91
  - lib/hash_map/dsl.rb
92
+ - lib/hash_map/json_adapter.rb
90
93
  - lib/hash_map/mapper.rb
91
94
  - lib/hash_map/version.rb
92
95
  homepage: https://github.com/arturictus/hash_map