hcast 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile.lock +1 -1
- data/lib/hcast/attributes_caster.rb +5 -6
- data/lib/hcast/metadata/attribute.rb +5 -1
- data/lib/hcast/version.rb +1 -1
- data/spec/hcast/caster_spec.rb +18 -55
- metadata +14 -9
data/Gemfile.lock
CHANGED
@@ -26,13 +26,12 @@ class HCast::AttributesCaster
|
|
26
26
|
private
|
27
27
|
|
28
28
|
def cast_attribute(attribute, hash)
|
29
|
-
value
|
30
|
-
|
31
|
-
|
32
|
-
if attribute.has_children?
|
33
|
-
cast_children(hash, attribute)
|
29
|
+
value = get_value(hash, attribute.name)
|
30
|
+
if value.nil? && attribute.allow_nil?
|
31
|
+
nil
|
34
32
|
else
|
35
|
-
casted_value
|
33
|
+
casted_value = attribute.caster.cast(value, attribute.name, attribute.options)
|
34
|
+
attribute.has_children? ? cast_children(hash, attribute) : casted_value
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
data/lib/hcast/version.rb
CHANGED
data/spec/hcast/caster_spec.rb
CHANGED
@@ -271,69 +271,32 @@ describe HCast::Caster do
|
|
271
271
|
end
|
272
272
|
end
|
273
273
|
|
274
|
-
context "
|
275
|
-
before
|
276
|
-
class
|
274
|
+
context "allow nil values" do
|
275
|
+
before(:all) do
|
276
|
+
class HomeCaster
|
277
277
|
include HCast::Caster
|
278
278
|
|
279
279
|
attributes do
|
280
|
-
|
281
|
-
|
282
|
-
integer :age, optional: true
|
283
|
-
float :weight, numericality: { less_than_or_equal_to: 200 }
|
284
|
-
date :birthday
|
285
|
-
datetime :last_logged_in
|
286
|
-
time :last_visited_at
|
287
|
-
hash :company do
|
288
|
-
string :name, length: { min: 2 }
|
289
|
-
end
|
290
|
-
array :emails, each: :string
|
291
|
-
array :social_accounts, each: :hash do
|
292
|
-
string :name
|
293
|
-
symbol :type, inclusion: { in: [:twitter, :facebook] }
|
294
|
-
end
|
295
|
-
end
|
280
|
+
string :city
|
281
|
+
integer :zip, allow_nil: true
|
296
282
|
end
|
297
283
|
end
|
298
284
|
end
|
299
285
|
|
300
|
-
it "should
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
},
|
313
|
-
emails: [ "test@example.com", "test2@example.com" ],
|
314
|
-
social_accounts: [
|
315
|
-
{
|
316
|
-
name: "john_smith",
|
317
|
-
type: 'twitter',
|
318
|
-
},
|
319
|
-
{
|
320
|
-
name: "John",
|
321
|
-
type: :yahoo,
|
322
|
-
},
|
323
|
-
]
|
324
|
-
}
|
286
|
+
it "should allow nil values if allow_nil is set to true" do
|
287
|
+
HomeCaster.cast(
|
288
|
+
city: 'Kazan',
|
289
|
+
zip: nil
|
290
|
+
)
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should allow nil values unless allow_nil is set to true" do
|
294
|
+
expect do
|
295
|
+
HomeCaster.cast(
|
296
|
+
city: nil,
|
297
|
+
zip: nil
|
325
298
|
)
|
326
|
-
|
327
|
-
e.errors.to_hash.should == {
|
328
|
-
contact: {
|
329
|
-
name: ["can't be more than 5"],
|
330
|
-
social_accounts: [
|
331
|
-
{},
|
332
|
-
{ type: ["should be included in [:twitter, :facebook]"] },
|
333
|
-
]
|
334
|
-
}
|
335
|
-
}
|
336
|
-
end
|
299
|
+
end.to raise_error(HCast::Errors::CastingError, "city should be a string")
|
337
300
|
end
|
338
301
|
end
|
339
302
|
end
|
metadata
CHANGED
@@ -2,25 +2,25 @@
|
|
2
2
|
name: hcast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Albert Gazizov
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-04-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
+
prerelease: false
|
16
|
+
name: bundler
|
17
|
+
type: :development
|
15
18
|
version_requirements: !ruby/object:Gem::Requirement
|
16
19
|
requirements:
|
17
20
|
- - ~>
|
18
21
|
- !ruby/object:Gem::Version
|
19
22
|
version: '1.3'
|
20
23
|
none: false
|
21
|
-
name: bundler
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
24
|
requirement: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - ~>
|
@@ -28,15 +28,15 @@ dependencies:
|
|
28
28
|
version: '1.3'
|
29
29
|
none: false
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
+
prerelease: false
|
32
|
+
name: rake
|
33
|
+
type: :development
|
31
34
|
version_requirements: !ruby/object:Gem::Requirement
|
32
35
|
requirements:
|
33
36
|
- - ! '>='
|
34
37
|
- !ruby/object:Gem::Version
|
35
38
|
version: '0'
|
36
39
|
none: false
|
37
|
-
name: rake
|
38
|
-
type: :development
|
39
|
-
prerelease: false
|
40
40
|
requirement: !ruby/object:Gem::Requirement
|
41
41
|
requirements:
|
42
42
|
- - ! '>='
|
@@ -92,12 +92,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
92
92
|
requirements:
|
93
93
|
- - ! '>='
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
hash: 3528154238926352388
|
95
98
|
version: '0'
|
96
99
|
none: false
|
97
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
101
|
requirements:
|
99
102
|
- - ! '>='
|
100
103
|
- !ruby/object:Gem::Version
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
hash: 3528154238926352388
|
101
107
|
version: '0'
|
102
108
|
none: false
|
103
109
|
requirements: []
|
@@ -110,4 +116,3 @@ test_files:
|
|
110
116
|
- spec/hcast/caster_spec.rb
|
111
117
|
- spec/hcast/hcast_spec.rb
|
112
118
|
- spec/spec_helper.rb
|
113
|
-
has_rdoc:
|