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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee03cb172d181f720bc7c2eb441cfd6d85303c59
4
- data.tar.gz: ec3a363227baff6705f71af417f94f46ca15a4ea
3
+ metadata.gz: 739fe83c458318c0c7a617e036e43524711019f0
4
+ data.tar.gz: 0c67f2f658011b50ada59d6bf836d7d25203496f
5
5
  SHA512:
6
- metadata.gz: 8ff5b3c3b9755129daa10f45d92441597cd9f927e1671e478cff3fc70acb57cf339442604ccd9654c9eb33de8d3eaa03b5433c6defa69f29cbe3e653a9f3fffc
7
- data.tar.gz: 39e7ac37410ed8a73e9606d746925267a9bb3ddaedff87aa161c3737c8c8fea5b2d933baf44ac5b806cf9192ab443ae9947aaa7297764bb7b27410e49a96691e
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 `:boolean`.
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.
@@ -2,6 +2,7 @@ require "tender_hash/version"
2
2
  require "tender_hash/map"
3
3
  require "tender_hash/rule"
4
4
  require "tender_hash/scope_rule"
5
+ require "tender_hash/caster"
5
6
 
6
7
  module TenderHash
7
8
 
@@ -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
@@ -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
- @cast_to = options[:cast_to] && options[:cast_to].to_sym
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]) unless old_hash[@old_key].nil?
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
- case @cast_to
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
@@ -1,3 +1,3 @@
1
1
  module TenderHash
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -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.2
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: 2014-08-31 00:00:00.000000000 Z
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