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 CHANGED
@@ -2,3 +2,5 @@ vendor/
2
2
  tags
3
3
  .bundle
4
4
  .DS_Store
5
+ tmp/
6
+ pkg
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hcast (0.0.1)
4
+ hcast (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -26,13 +26,12 @@ class HCast::AttributesCaster
26
26
  private
27
27
 
28
28
  def cast_attribute(attribute, hash)
29
- value = get_value(hash, attribute.name)
30
- casted_value = attribute.caster.cast(value, attribute.name, attribute.options)
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
 
@@ -19,7 +19,11 @@ module HCast::Metadata
19
19
  end
20
20
 
21
21
  def optional?
22
- options[:optional]
22
+ !!options[:optional]
23
+ end
24
+
25
+ def allow_nil?
26
+ !!options[:allow_nil]
23
27
  end
24
28
 
25
29
  end
@@ -1,3 +1,3 @@
1
1
  module HCast
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -271,69 +271,32 @@ describe HCast::Caster do
271
271
  end
272
272
  end
273
273
 
274
- context "validations" do
275
- before :all do
276
- class ContactWithValidationsCaster
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
- hash :contact do
281
- string :name, presence: true, length: { max: 5 }
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 collect validation errors and raise exception when hash is invalid" do
301
- begin
302
- ContactWithValidationsCaster.cast(
303
- contact: {
304
- name: "John Smith",
305
- age: "22",
306
- weight: "65.5",
307
- birthday: "2014-02-02",
308
- last_logged_in: "2014-02-02 10:10:00",
309
- last_visited_at: "2014-02-02 10:10:00",
310
- company: {
311
- name: "MyCo",
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
- rescue HCast::Errors::ValidationError => e
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.1
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-02-24 00:00:00.000000000 Z
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: