virtus 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. data/.travis.yml +9 -2
  2. data/.yardopts +1 -0
  3. data/History.md +51 -0
  4. data/{README.markdown → README.md} +63 -7
  5. data/TODO +2 -4
  6. data/VERSION +1 -1
  7. data/config/flay.yml +2 -2
  8. data/config/flog.yml +1 -1
  9. data/config/roodi.yml +5 -6
  10. data/config/site.reek +3 -3
  11. data/examples/custom_coercion_spec.rb +50 -0
  12. data/examples/default_values_spec.rb +21 -0
  13. data/lib/virtus.rb +21 -6
  14. data/lib/virtus/attribute.rb +113 -253
  15. data/lib/virtus/attribute/array.rb +6 -3
  16. data/lib/virtus/attribute/boolean.rb +9 -28
  17. data/lib/virtus/attribute/date.rb +9 -12
  18. data/lib/virtus/attribute/date_time.rb +10 -12
  19. data/lib/virtus/attribute/decimal.rb +4 -11
  20. data/lib/virtus/attribute/float.rb +4 -11
  21. data/lib/virtus/attribute/hash.rb +5 -3
  22. data/lib/virtus/attribute/integer.rb +4 -11
  23. data/lib/virtus/attribute/numeric.rb +1 -0
  24. data/lib/virtus/attribute/object.rb +1 -0
  25. data/lib/virtus/attribute/string.rb +4 -11
  26. data/lib/virtus/attribute/time.rb +9 -16
  27. data/lib/virtus/class_methods.rb +42 -7
  28. data/lib/virtus/coercion.rb +32 -0
  29. data/lib/virtus/coercion/date.rb +26 -0
  30. data/lib/virtus/coercion/date_time.rb +26 -0
  31. data/lib/virtus/coercion/decimal.rb +40 -0
  32. data/lib/virtus/coercion/false_class.rb +24 -0
  33. data/lib/virtus/coercion/float.rb +24 -0
  34. data/lib/virtus/coercion/hash.rb +82 -0
  35. data/lib/virtus/coercion/integer.rb +60 -0
  36. data/lib/virtus/coercion/numeric.rb +66 -0
  37. data/lib/virtus/coercion/object.rb +25 -0
  38. data/lib/virtus/coercion/string.rb +155 -0
  39. data/lib/virtus/coercion/symbol.rb +24 -0
  40. data/lib/virtus/coercion/time.rb +26 -0
  41. data/lib/virtus/coercion/time_coercions.rb +85 -0
  42. data/lib/virtus/coercion/true_class.rb +24 -0
  43. data/lib/virtus/instance_methods.rb +7 -0
  44. data/lib/virtus/support/descendants_tracker.rb +1 -1
  45. data/lib/virtus/support/options.rb +114 -0
  46. data/lib/virtus/support/type_lookup.rb +95 -0
  47. data/spec/integration/virtus/attributes/attribute/{typecast_spec.rb → set_spec.rb} +7 -7
  48. data/spec/unit/shared/attribute.rb +3 -3
  49. data/spec/unit/shared/attribute/accept_options.rb +0 -18
  50. data/spec/unit/shared/attribute/accepted_options.rb +0 -6
  51. data/spec/unit/shared/attribute/get.rb +32 -17
  52. data/spec/unit/shared/attribute/inspect.rb +7 -0
  53. data/spec/unit/shared/attribute/primitive.rb +15 -0
  54. data/spec/unit/shared/attribute/set.rb +16 -21
  55. data/spec/unit/virtus/attribute/array_spec.rb +18 -3
  56. data/spec/unit/virtus/attribute/boolean_spec.rb +8 -6
  57. data/spec/unit/virtus/attribute/date_spec.rb +8 -6
  58. data/spec/unit/virtus/attribute/date_time_spec.rb +8 -6
  59. data/spec/unit/virtus/attribute/decimal_spec.rb +18 -6
  60. data/spec/unit/virtus/attribute/float_spec.rb +19 -7
  61. data/spec/unit/virtus/attribute/hash_spec.rb +5 -3
  62. data/spec/unit/virtus/attribute/integer_spec.rb +10 -8
  63. data/spec/unit/virtus/attribute/string_spec.rb +10 -8
  64. data/spec/unit/virtus/attribute/time_spec.rb +8 -6
  65. data/spec/unit/virtus/class_methods/attributes_spec.rb +11 -0
  66. data/spec/unit/virtus/coercion/class_name_reference_spec.rb +17 -0
  67. data/spec/unit/virtus/coercion/date/class_methods/to_datetime_spec.rb +30 -0
  68. data/spec/unit/virtus/coercion/date/class_methods/to_string_spec.rb +12 -0
  69. data/spec/unit/virtus/coercion/date/class_methods/to_time_spec.rb +12 -0
  70. data/spec/unit/virtus/coercion/date_time/class_methods/to_date_spec.rb +30 -0
  71. data/spec/unit/virtus/coercion/date_time/class_methods/to_string_spec.rb +12 -0
  72. data/spec/unit/virtus/coercion/date_time/class_methods/to_time_spec.rb +30 -0
  73. data/spec/unit/virtus/coercion/decimal/class_methods/to_float_spec.rb +12 -0
  74. data/spec/unit/virtus/coercion/decimal/class_methods/to_integer_spec.rb +12 -0
  75. data/spec/unit/virtus/coercion/decimal/class_methods/to_string_spec.rb +12 -0
  76. data/spec/unit/virtus/coercion/false_class/class_methods/to_string_spec.rb +12 -0
  77. data/spec/unit/virtus/coercion/float/class_methods/to_decimal_spec.rb +12 -0
  78. data/spec/unit/virtus/coercion/float/class_methods/to_integer_spec.rb +12 -0
  79. data/spec/unit/virtus/coercion/float/class_methods/to_string_spec.rb +12 -0
  80. data/spec/unit/virtus/coercion/hash/class_methods/to_array_spec.rb +12 -0
  81. data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +31 -0
  82. data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +31 -0
  83. data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +31 -0
  84. data/spec/unit/virtus/coercion/integer/class_methods/to_boolean_spec.rb +25 -0
  85. data/spec/unit/virtus/coercion/integer/class_methods/to_decimal_spec.rb +12 -0
  86. data/spec/unit/virtus/coercion/integer/class_methods/to_float_spec.rb +12 -0
  87. data/spec/unit/virtus/coercion/integer/class_methods/to_string_spec.rb +12 -0
  88. data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +33 -0
  89. data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +29 -0
  90. data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +23 -0
  91. data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +50 -0
  92. data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +23 -0
  93. data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +21 -0
  94. data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +21 -0
  95. data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +50 -0
  96. data/spec/unit/virtus/coercion/symbol/class_methods/to_string_spec.rb +12 -0
  97. data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +12 -0
  98. data/spec/unit/virtus/instance_methods/attributes_spec.rb +7 -0
  99. data/spec/unit/virtus/options/accept_options_spec.rb +38 -0
  100. data/spec/unit/virtus/options/accepted_options_spec.rb +21 -0
  101. data/spec/unit/virtus/options/options_spec.rb +11 -0
  102. data/spec/unit/virtus/type_lookup/determine_type_spec.rb +68 -0
  103. data/spec/unit/virtus/type_lookup/primitive_spec.rb +9 -0
  104. data/virtus.gemspec +70 -17
  105. metadata +78 -27
  106. data/History.txt +0 -38
  107. data/lib/virtus/typecast/boolean.rb +0 -29
  108. data/lib/virtus/typecast/numeric.rb +0 -87
  109. data/lib/virtus/typecast/string.rb +0 -24
  110. data/lib/virtus/typecast/time.rb +0 -192
  111. data/spec/unit/shared/attribute/complex.rb +0 -15
  112. data/spec/unit/shared/attribute/options.rb +0 -7
  113. data/spec/unit/virtus/attribute/attribute_spec.rb +0 -12
@@ -2,29 +2,11 @@ shared_examples_for 'Attribute.accept_options' do
2
2
  let(:sub_attribute) { Class.new(described_class) }
3
3
  let(:new_option) { :width }
4
4
 
5
- specify { described_class.should respond_to(:accept_options) }
6
-
7
5
  before :all do
8
6
  described_class.accepted_options.should_not include(new_option)
9
7
  described_class.accept_options(new_option)
10
8
  end
11
9
 
12
- it "sets new accepted options on itself" do
13
- described_class.accepted_options.should include(new_option)
14
- end
15
-
16
- it "sets new accepted option on its descendants" do
17
- sub_attribute.accepted_options.should include(new_option)
18
- end
19
-
20
- it "creates option accessors" do
21
- described_class.should respond_to(new_option)
22
- end
23
-
24
- it "creates option accessors on descendants" do
25
- sub_attribute.should respond_to(new_option)
26
- end
27
-
28
10
  context 'with default option value' do
29
11
  let(:option) { :height }
30
12
  let(:value) { 10 }
@@ -1,10 +1,4 @@
1
1
  shared_examples_for 'Attribute.accepted_options' do
2
- specify { described_class.should respond_to(:accepted_options) }
3
-
4
- it "returns an array of accepted options" do
5
- described_class.accepted_options.should be_instance_of(Array)
6
- end
7
-
8
2
  it "includes base options" do
9
3
  described_class.accepted_options.should include(*Virtus::Attribute::OPTIONS)
10
4
  end
@@ -1,29 +1,44 @@
1
1
  shared_examples_for 'Attribute#get' do
2
- let(:model) do
3
- Class.new { include Virtus }
4
- end
2
+ subject { attribute.get(instance) }
5
3
 
6
- let(:attribute) do
7
- model.attribute(attribute_name, described_class).attributes[attribute_name]
8
- end
4
+ let(:model) { Class.new }
5
+ let(:instance) { model.new }
9
6
 
10
- let(:object) do
11
- model.new
12
- end
7
+ context 'without default value' do
8
+ let(:attribute) { described_class.new(attribute_name) }
13
9
 
14
- context "when a non-nil value is set" do
15
- before { attribute.set(object, attribute_value) }
10
+ before { attribute.set(instance, value) }
16
11
 
17
- subject { attribute.get(object) }
12
+ context "when a non-nil value is set" do
13
+ let(:value) { attribute_value }
14
+ it { should eql(attribute_value) }
15
+ end
18
16
 
19
- it { should eql(attribute_value) }
17
+ context "when nil is set" do
18
+ let(:value) { nil }
19
+ it { should be(value) }
20
+ end
20
21
  end
21
22
 
22
- context "when nil is set" do
23
- before { attribute.set(object, nil) }
23
+ context 'with default value' do
24
+ context 'set to proc' do
25
+ let(:attribute) do
26
+ described_class.new(attribute_name, :default => attribute_default_proc)
27
+ end
28
+
29
+ let(:evaluated_proc_value) do
30
+ attribute.default.call(instance, attribute)
31
+ end
32
+
33
+ it { should == evaluated_proc_value }
34
+ end
24
35
 
25
- subject { attribute.get(object) }
36
+ context 'not set to proc' do
37
+ let(:attribute) do
38
+ described_class.new(attribute_name, :default => attribute_default)
39
+ end
26
40
 
27
- it { should be(nil) }
41
+ it { should == attribute_default }
42
+ end
28
43
  end
29
44
  end
@@ -0,0 +1,7 @@
1
+ shared_examples_for 'Attribute#inspect' do
2
+ subject { attribute.inspect }
3
+
4
+ let(:attribute) { described_class.new(attribute_name) }
5
+
6
+ it { should == "#<#{described_class.inspect} @name=#{attribute_name.inspect}>" }
7
+ end
@@ -0,0 +1,15 @@
1
+ shared_examples_for 'Attribute.primitive?' do
2
+ subject { described_class.primitive?(value) }
3
+
4
+ let(:attribute) { described_class.new(attribute_name) }
5
+
6
+ context 'with a primitive value' do
7
+ let(:value) { attribute_value }
8
+ it { should be(true) }
9
+ end
10
+
11
+ context 'with a non-primitive value' do
12
+ let(:value) { nil }
13
+ it { should be(false) }
14
+ end
15
+ end
@@ -1,42 +1,37 @@
1
1
  shared_examples_for 'Attribute#set' do
2
- let(:model) do
3
- Class.new { include Virtus }
4
- end
2
+ subject { attribute.set(instance, value) }
5
3
 
6
- let(:attribute) do
7
- model.attribute(attribute_name, described_class).attributes[attribute_name]
8
- end
4
+ let(:attribute) { described_class.new(attribute_name) }
5
+ let(:model) { Class.new }
6
+ let(:instance) { model.new }
9
7
 
10
- let(:object) do
11
- model.new
12
- end
8
+ before { subject }
13
9
 
14
10
  context "with nil" do
15
- subject { attribute.set(object, nil) }
11
+ let(:value) { nil }
16
12
 
17
- it "doesn't set the ivar" do
18
- subject
19
- object.instance_variable_defined?(attribute.instance_variable_name).should be(false)
20
- end
13
+ it { should be(nil) }
21
14
 
22
- it "returns nil" do
23
- subject.should be(nil)
15
+ it "sets the value in an ivar" do
16
+ instance.instance_variable_get(attribute.instance_variable_name).should be(nil)
24
17
  end
25
18
  end
26
19
 
27
20
  context "with a primitive value" do
28
- before { attribute.set(object, attribute_value) }
21
+ let(:value) { attribute_value }
22
+
23
+ it { should == attribute_value }
29
24
 
30
25
  it "sets the value in an ivar" do
31
- object.instance_variable_get(attribute.instance_variable_name).should eql(attribute_value)
26
+ instance.instance_variable_get(attribute.instance_variable_name).should eql(attribute_value)
32
27
  end
33
28
  end
34
29
 
35
30
  context "with a non-primitive value" do
36
- before { attribute.set(object, attribute_value_other) }
31
+ let(:value) { attribute_value_other }
37
32
 
38
- it "sets the value in an ivar converted to the primitive type" do
39
- object.instance_variable_get(attribute.instance_variable_name).should be_kind_of(described_class.primitive)
33
+ it "sets the value in an ivar coerced to the primitive type" do
34
+ instance.instance_variable_get(attribute.instance_variable_name).should be_kind_of(described_class.primitive)
40
35
  end
41
36
  end
42
37
  end
@@ -2,8 +2,23 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Array do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :colors }
6
- let(:attribute_value) { [ 'red', 'green', 'blue' ] }
7
- let(:attribute_value_other) { [ 'orange', 'yellow', 'gray' ] }
5
+ let(:attribute_name) { :colors }
6
+ let(:attribute_value) { [ 'red', 'green', 'blue' ] }
7
+ let(:attribute_value_other) { [ 'orange', 'yellow', 'gray' ] }
8
+ let(:attribute_default) { [] }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :colors } }
8
10
  end
11
+
12
+ describe '#coerce' do
13
+ let(:attribute) { described_class.new(:colors) }
14
+
15
+ subject { attribute.coerce(value) }
16
+
17
+ context 'with a Hash' do
18
+ let(:value) { { :foo => 'bar' } }
19
+
20
+ it { should == [ [:foo, 'bar'] ] }
21
+ end
22
+ end
23
+
9
24
  end
@@ -2,9 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Boolean do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :is_admin }
6
- let(:attribute_value) { true }
7
- let(:attribute_value_other) { '1' }
5
+ let(:attribute_name) { :is_admin }
6
+ let(:attribute_value) { true }
7
+ let(:attribute_value_other) { '1' }
8
+ let(:attribute_default) { true }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :is_admin } }
8
10
  end
9
11
 
10
12
  describe "accessor names" do
@@ -23,10 +25,10 @@ describe Virtus::Attribute::Boolean do
23
25
  end
24
26
  end
25
27
 
26
- describe '#typecast' do
27
- let(:attribute) { Virtus::Attribute::Boolean.new(:is_admin) }
28
+ describe '#coerce' do
29
+ let(:attribute) { described_class.new(:is_admin) }
28
30
 
29
- subject { attribute.typecast(value) }
31
+ subject { attribute.coerce(value) }
30
32
 
31
33
  context "with 1" do
32
34
  let(:value) { 1 }
@@ -2,19 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Date do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :created_on }
6
- let(:attribute_value) { Date.today }
7
- let(:attribute_value_other) { (Date.today+1).to_s }
5
+ let(:attribute_name) { :created_on }
6
+ let(:attribute_value) { Date.today }
7
+ let(:attribute_value_other) { (Date.today+1).to_s }
8
+ let(:attribute_default) { Date.today-1 }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :created_on } }
8
10
  end
9
11
 
10
- describe '#typecast' do
11
- let(:attribute) { Virtus::Attribute::Date.new(:bday) }
12
+ describe '#coerce' do
13
+ let(:attribute) { described_class.new(:bday) }
12
14
 
13
15
  let(:year) { 2011 }
14
16
  let(:month) { 4 }
15
17
  let(:day) { 7 }
16
18
 
17
- subject { attribute.typecast(value) }
19
+ subject { attribute.coerce(value) }
18
20
 
19
21
  shared_examples_for "a correct date" do
20
22
  it { should be_kind_of(Date) }
@@ -2,13 +2,15 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::DateTime do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :created_at }
6
- let(:attribute_value) { DateTime.now }
7
- let(:attribute_value_other) { DateTime.now.to_s }
5
+ let(:attribute_name) { :created_at }
6
+ let(:attribute_value) { DateTime.now }
7
+ let(:attribute_value_other) { DateTime.now.to_s }
8
+ let(:attribute_default) { DateTime.now-1 }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :created_at } }
8
10
  end
9
11
 
10
- describe '#typecast' do
11
- let(:attribute) { Virtus::Attribute::DateTime.new(:bday) }
12
+ describe '#coerce' do
13
+ let(:attribute) { described_class.new(:bday) }
12
14
 
13
15
  let(:year) { 2011 }
14
16
  let(:month) { 4 }
@@ -17,7 +19,7 @@ describe Virtus::Attribute::DateTime do
17
19
  let(:min) { 26 }
18
20
  let(:sec) { 49 }
19
21
 
20
- subject { attribute.typecast(value) }
22
+ subject { attribute.coerce(value) }
21
23
 
22
24
  shared_examples_for "a correct date time" do
23
25
  it { should be_kind_of(DateTime) }
@@ -2,15 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Decimal do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :price }
6
- let(:attribute_value) { BigDecimal("12.3456789") }
7
- let(:attribute_value_other) { "12.3456789" }
5
+ let(:attribute_name) { :price }
6
+ let(:attribute_value) { BigDecimal("12.3456789") }
7
+ let(:attribute_value_other) { "12.3456789" }
8
+ let(:attribute_default) { BigDecimal('0') }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :price } }
8
10
  end
9
11
 
10
- describe '#typecast' do
11
- let(:attribute) { Virtus::Attribute::Decimal.new(:price) }
12
+ describe '#coerce' do
13
+ let(:attribute) { described_class.new(:price) }
12
14
 
13
- subject { attribute.typecast(value) }
15
+ subject { attribute.coerce(value) }
14
16
 
15
17
  context "with 24.0 big decimal" do
16
18
  let(:value) { BigDecimal('24.0') }
@@ -72,6 +74,16 @@ describe Virtus::Attribute::Decimal do
72
74
  it { should eql(BigDecimal('-24.0')) }
73
75
  end
74
76
 
77
+ context 'with a positive bignum' do
78
+ let(:value) { 1311936052 }
79
+ it { should eql(BigDecimal('1311936052.0')) }
80
+ end
81
+
82
+ context 'with a negative bignum' do
83
+ let(:value) { -1311936052 }
84
+ it { should eql(BigDecimal('-1311936052.0')) }
85
+ end
86
+
75
87
  context 'with a zero float' do
76
88
  let(:value) { 0.0 }
77
89
  it { should eql(BigDecimal('0.0')) }
@@ -2,15 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Float do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :score }
6
- let(:attribute_value) { 12.34 }
7
- let(:attribute_value_other) { "12.34" }
5
+ let(:attribute_name) { :score }
6
+ let(:attribute_value) { 12.34 }
7
+ let(:attribute_value_other) { "12.34" }
8
+ let(:attribute_default) { 0.0 }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :score } }
8
10
  end
9
11
 
10
- describe '#typecast' do
11
- let(:attribute) { Virtus::Attribute::Float.new(:score) }
12
+ describe '#coerce' do
13
+ let(:attribute) { described_class.new(:score) }
12
14
 
13
- subject { attribute.typecast(value) }
15
+ subject { attribute.coerce(value) }
14
16
 
15
17
  context "with a float" do
16
18
  let(:value) { 24.0 }
@@ -72,6 +74,16 @@ describe Virtus::Attribute::Float do
72
74
  it { should eql(-24.0) }
73
75
  end
74
76
 
77
+ context 'with a positive bignum' do
78
+ let(:value) { 1311935550 }
79
+ it { should eql(1311935550.0) }
80
+ end
81
+
82
+ context 'with a negative bignum' do
83
+ let(:value) { -1311935550 }
84
+ it { should eql(-1311935550.0) }
85
+ end
86
+
75
87
  context 'with a zero decimal' do
76
88
  let(:value) { BigDecimal('0.0') }
77
89
  it { should eql(0.0) }
@@ -88,7 +100,7 @@ describe Virtus::Attribute::Float do
88
100
  end
89
101
 
90
102
  [ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |non_num_value|
91
- context "does not typecast non-numeric value #{non_num_value.inspect}" do
103
+ context "does not coerce non-numeric value #{non_num_value.inspect}" do
92
104
  let(:value) { non_num_value }
93
105
  it { should equal(non_num_value) }
94
106
  end
@@ -2,8 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Hash do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :settings }
6
- let(:attribute_value) { Hash[:one => 1] }
7
- let(:attribute_value_other) { Hash[:two => 2] }
5
+ let(:attribute_name) { :settings }
6
+ let(:attribute_value) { Hash[:one => 1] }
7
+ let(:attribute_value_other) { Hash[:two => 2] }
8
+ let(:attribute_default) { Hash.new }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :settings } }
8
10
  end
9
11
  end
@@ -2,15 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe Virtus::Attribute::Integer do
4
4
  it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :age }
6
- let(:attribute_value) { 28 }
7
- let(:attribute_value_other) { "28" }
5
+ let(:attribute_name) { :age }
6
+ let(:attribute_value) { 28 }
7
+ let(:attribute_value_other) { "28" }
8
+ let(:attribute_default) { 0 }
9
+ let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :age } }
8
10
  end
9
11
 
10
- describe '#typecast' do
11
- let(:attribute) { Virtus::Attribute::Integer.new(:age) }
12
+ describe '#coerce' do
13
+ let(:attribute) { described_class.new(:age) }
12
14
 
13
- subject { attribute.typecast(value) }
15
+ subject { attribute.coerce(value) }
14
16
 
15
17
  context "with an integer" do
16
18
  let(:value) { 24 }
@@ -87,8 +89,8 @@ describe Virtus::Attribute::Integer do
87
89
  it { should eql(-24) }
88
90
  end
89
91
 
90
- [ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |non_num_value|
91
- context "does not typecast non-numeric value #{non_num_value.inspect}" do
92
+ [ Object.new, true, false, '00.0', '0.', '-.0', 'string' ].each do |non_num_value|
93
+ context "does not coerce non-numeric value #{non_num_value.inspect}" do
92
94
  let(:value) { non_num_value }
93
95
  it { should equal(non_num_value) }
94
96
  end