tender_hash 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/lib/tender_hash.rb +1 -0
- data/lib/tender_hash/caster.rb +44 -0
- data/lib/tender_hash/rule.rb +3 -24
- data/lib/tender_hash/version.rb +1 -1
- data/spec/tender_hash/map_spec.rb +5 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 739fe83c458318c0c7a617e036e43524711019f0
|
4
|
+
data.tar.gz: 0c67f2f658011b50ada59d6bf836d7d25203496f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2dc0a35c0b8835e3e97cf9a50e68e8990171a733ec453fd886ff40e1b008b45294f98e7f252b1fdae8d42e7b5e5d7bc23d006c545c9c6cc50fc1deb033e7311a
|
7
|
+
data.tar.gz: 9be743fcf9e8608049c863393c2c32f8f0d87ba29549b8364f4cb0215a547eba7433fb56d2b6df1a6892734f60861aa89e7c8a6c9adcb5b2480c52f103b55e05
|
data/README.md
CHANGED
@@ -61,15 +61,16 @@ end
|
|
61
61
|
```
|
62
62
|
|
63
63
|
The `key` and `map_key` methods also accept a `cast_to` option. The
|
64
|
-
possible alternatives are: `:integer`, `:string` and
|
64
|
+
possible alternatives are: `:integer`, `:string`, `:boolean` and any object that responds to `#call`.
|
65
65
|
|
66
66
|
```ruby
|
67
67
|
TenderHash.map( { name: 'Rodrigo', age: 27, logged_in: 'false' } ) do
|
68
68
|
key :age, cast_to: :string
|
69
69
|
key :logged_in, cast_to: :boolean
|
70
|
+
key :name, cast_to: -> (v) { v.upcase }
|
70
71
|
end
|
71
72
|
|
72
|
-
# => { age: '27', looged_in: false }
|
73
|
+
# => { age: '27', looged_in: false, name: 'RODRIGO' }
|
73
74
|
```
|
74
75
|
|
75
76
|
The `scope` method allows to nest a hash within a key.
|
data/lib/tender_hash.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
module TenderHash
|
2
|
+
class Caster
|
3
|
+
def self.for(cast_to)
|
4
|
+
if cast_to.respond_to?(:call)
|
5
|
+
cast_to
|
6
|
+
else
|
7
|
+
Default.new(cast_to)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Default
|
12
|
+
|
13
|
+
def initialize(cast_to)
|
14
|
+
@cast_to = cast_to
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(val)
|
18
|
+
case @cast_to && @cast_to.to_sym
|
19
|
+
when :integer
|
20
|
+
val.to_i
|
21
|
+
when :string
|
22
|
+
val.to_s
|
23
|
+
when :boolean
|
24
|
+
cast_to_boolean(val)
|
25
|
+
else
|
26
|
+
val
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def cast_to_boolean(val)
|
31
|
+
case val
|
32
|
+
when 'true', 1
|
33
|
+
true
|
34
|
+
when 'false', 0
|
35
|
+
false
|
36
|
+
else
|
37
|
+
val
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/lib/tender_hash/rule.rb
CHANGED
@@ -4,40 +4,19 @@ module TenderHash
|
|
4
4
|
def initialize(old_key, new_key=nil, options={})
|
5
5
|
@old_key = old_key
|
6
6
|
@new_key = new_key || old_key
|
7
|
-
@
|
7
|
+
@caster = Caster.for(options[:cast_to])
|
8
8
|
@default = options[:default]
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
11
|
def apply(old_hash, new_hash)
|
13
|
-
new_value = cast_value(old_hash[@old_key])
|
12
|
+
new_value = cast_value(old_hash[@old_key])
|
14
13
|
new_hash[@new_key] = new_value.nil? ? @default : new_value
|
15
14
|
end
|
16
15
|
|
17
16
|
private
|
18
17
|
|
19
18
|
def cast_value(val)
|
20
|
-
|
21
|
-
when :integer
|
22
|
-
val.to_i
|
23
|
-
when :string
|
24
|
-
val.to_s
|
25
|
-
when :boolean
|
26
|
-
cast_to_boolean(val)
|
27
|
-
else
|
28
|
-
val
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def cast_to_boolean(val)
|
33
|
-
case val
|
34
|
-
when 'true', 1
|
35
|
-
true
|
36
|
-
when 'false', 0
|
37
|
-
false
|
38
|
-
else
|
39
|
-
val
|
40
|
-
end
|
19
|
+
@caster.call(val) unless val.nil?
|
41
20
|
end
|
42
21
|
|
43
22
|
end
|
data/lib/tender_hash/version.rb
CHANGED
@@ -35,6 +35,11 @@ describe TenderHash::Map do
|
|
35
35
|
end
|
36
36
|
|
37
37
|
describe "cast_to option" do
|
38
|
+
it "calls the given proc with the string and assigns the output to corresponding key" do
|
39
|
+
mapper.key :alice, cast_to: -> (v) { v.upcase }
|
40
|
+
expect( mapper.to_h ).to eql( alice: 'COOPER' )
|
41
|
+
end
|
42
|
+
|
38
43
|
it "calls to_i on the mapped value if cast_to is set to integer" do
|
39
44
|
mapper.key :johnny, cast_to: :integer
|
40
45
|
expect( mapper.to_h ).to eql( johnny: 13 )
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tender_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Pavano
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- lib/tender_hash.rb
|
72
|
+
- lib/tender_hash/caster.rb
|
72
73
|
- lib/tender_hash/map.rb
|
73
74
|
- lib/tender_hash/rule.rb
|
74
75
|
- lib/tender_hash/scope_rule.rb
|