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 +4 -4
- data/README.md +37 -1
- data/lib/hash_map.rb +5 -0
- data/lib/hash_map/base.rb +17 -1
- data/lib/hash_map/core_ext/hash.rb +9 -0
- data/lib/hash_map/core_ext/string.rb +9 -0
- data/lib/hash_map/json_adapter.rb +10 -0
- data/lib/hash_map/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b34bf9da8846a7016da845a50f12f5b56906135
|
4
|
+
data.tar.gz: 90939f2f29f4fe79f824545b14d04a0c1934d9b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
270
|
+
User.create(MyMapper.map(api_response)) # done
|
235
271
|
```
|
236
272
|
|
237
273
|
## Development
|
data/lib/hash_map.rb
CHANGED
data/lib/hash_map/base.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module HashMap
|
2
|
-
Base
|
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,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
|
data/lib/hash_map/version.rb
CHANGED
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
|
+
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-
|
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
|