hcast 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NTM3NjVkMmE2YzUwZGJkYzdkMGRlYzZmZjhmMzQ5ZDJiNWYyYTNmMg==
5
- data.tar.gz: !binary |-
6
- MDRiN2M2ZTM1Mjc2MzhjMTkyNTBjOTM5YjNhZjE4OTAxZDEzYWRjNw==
2
+ SHA1:
3
+ metadata.gz: e0525db10e7d9bce5ff3269a13fdef62dad0bf78
4
+ data.tar.gz: 062eb82bf96f6bbdb4fa161305cf2649c6d1433d
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- N2JkYTdhMDg0ZTg5MDkzYjVjZGJlNjZjMGYzYzY5ZTRiYTAyYmJkYzA4NDYz
10
- NGE5ZmYyMDIzNzdmZWJjYjg0M2M4NTAzYTU0YTdlMmZjNTg0YzRkYjYwNDQ5
11
- ZjNlOWFjODEzNjk1YmQ4MDY5MWEzMWM4MGU5NWUyODc3ZGEzMmU=
12
- data.tar.gz: !binary |-
13
- MGZhZWU4YTdhNWEyMGYzZjM5OWY4MTI2MWVkNjY5MmRmYzY3Y2U4NTc3MTRm
14
- Nzc0ZTVhOTgwYmVkODI2YzQxNzE5YTRiNWJmYzM2YjBlYjZhM2Y5OGI4N2Yy
15
- ZTU1MzRiYzA5OGYwMDQ4MDA2NWI4OGRjZGYwOTYzYjczNGViNjU=
6
+ metadata.gz: 3072c990ef4a95fc8c80d44548de93b69b7ddeb81e1de604249b02dfad7020c88cc8eca58f606b86c4bc4f71e682d58e31fa93f67f154ffb01d63a2776f7f4d4
7
+ data.tar.gz: 332d625a49002997180a3421a49ef382b4ec2597e82e6ff8d2b86f87eb37a55caabcaf502c5d60c003764090f5a41b6fef550591e8580724e7cfe90ef8eb1818
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hcast (0.1.1)
4
+ hcast (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -36,19 +36,27 @@ class HCast::AttributesCaster
36
36
  nil
37
37
  else
38
38
  casted_value = attribute.caster.cast(value, attribute.name, attribute.options)
39
- attribute.has_children? ? cast_children(hash, attribute) : casted_value
39
+ if attribute.has_children?
40
+ cast_children(casted_value, attribute)
41
+ elsif caster = attribute.options[:caster]
42
+ cast_children_with_caster(casted_value, attribute, caster)
43
+ else
44
+ casted_value
45
+ end
40
46
  end
41
47
  end
42
48
 
43
- def cast_children(hash, attribute)
44
- value = get_value(hash, attribute.name)
49
+ def cast_children(value, attribute)
50
+ caster = self.class.new(attribute.children, options)
51
+ cast_children_with_caster(value, attribute, caster)
52
+ end
53
+
54
+ def cast_children_with_caster(value, attribute, caster)
45
55
  if attribute.caster == HCast::Casters::ArrayCaster
46
56
  value.map do |val|
47
- caster = self.class.new(attribute.children, options)
48
57
  caster.cast(val)
49
58
  end
50
59
  else
51
- caster = self.class.new(attribute.children, options)
52
60
  caster.cast(value)
53
61
  end
54
62
  end
@@ -1,3 +1,3 @@
1
1
  module HCast
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -80,6 +80,56 @@ describe HCast::Caster do
80
80
  }
81
81
  end
82
82
 
83
+ describe "Custom casters" do
84
+ class SettingsCaster
85
+ include HCast::Caster
86
+
87
+ attributes do
88
+ string :account
89
+ end
90
+ end
91
+
92
+ class EmailCaster
93
+ include HCast::Caster
94
+
95
+ attributes do
96
+ string :address
97
+ end
98
+ end
99
+
100
+ class CompanyCaster
101
+ include HCast::Caster
102
+
103
+ attributes do
104
+ string :name
105
+ hash :settings, caster: SettingsCaster
106
+ array :emails, caster: EmailCaster
107
+ end
108
+ end
109
+
110
+ it "should allow specify caster for nested hash attribute" do
111
+ casted_hash = CompanyCaster.cast(
112
+ name: 'Might & Magic',
113
+ settings: {
114
+ account: :'migthy_lord'
115
+ },
116
+ emails: [
117
+ { address: :'test1@example.com' },
118
+ { address: :'test2@example.com' },
119
+ ]
120
+ )
121
+
122
+ casted_hash.should == {
123
+ name: "Might & Magic",
124
+ settings: { account: "migthy_lord" },
125
+ emails: [
126
+ { address: "test1@example.com" },
127
+ { address: "test2@example.com" }
128
+ ]
129
+ }
130
+ end
131
+ end
132
+
83
133
  it "should raise error if some attribute can't be casted" do
84
134
  input_hash = {
85
135
  contact: {
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hcast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Albert Gazizov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-06 00:00:00.000000000 Z
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- type: :development
14
+ name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
- name: bundler
20
+ type: :development
21
+ prerelease: false
21
22
  version_requirements: !ruby/object:Gem::Requirement
22
23
  requirements:
23
- - - ~>
24
+ - - "~>"
24
25
  - !ruby/object:Gem::Version
25
26
  version: '1.3'
26
- prerelease: false
27
27
  - !ruby/object:Gem::Dependency
28
- type: :development
28
+ name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- name: rake
34
+ type: :development
35
+ prerelease: false
35
36
  version_requirements: !ruby/object:Gem::Requirement
36
37
  requirements:
37
- - - ! '>='
38
+ - - ">="
38
39
  - !ruby/object:Gem::Version
39
40
  version: '0'
40
- prerelease: false
41
41
  description: Hash Caster and Validator
42
42
  email:
43
43
  - deeper4k@gmail.com
@@ -45,8 +45,8 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - .gitignore
49
- - .travis.yml
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
50
  - Gemfile
51
51
  - Gemfile.lock
52
52
  - LICENSE.txt
@@ -86,17 +86,17 @@ require_paths:
86
86
  - lib
87
87
  required_ruby_version: !ruby/object:Gem::Requirement
88
88
  requirements:
89
- - - ! '>='
89
+ - - ">="
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
98
  rubyforge_project:
99
- rubygems_version: 2.4.1
99
+ rubygems_version: 2.2.2
100
100
  signing_key:
101
101
  specification_version: 4
102
102
  summary: Hash Caster and Validator