virtus 0.0.10 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +33 -7
- data/.travis.yml +4 -74
- data/Changelog.md +11 -2
- data/Gemfile +10 -9
- data/README.md +85 -1
- data/TODO +18 -0
- data/config/flay.yml +2 -2
- data/config/flog.yml +1 -1
- data/config/roodi.yml +3 -3
- data/config/site.reek +8 -3
- data/lib/virtus.rb +5 -0
- data/lib/virtus/attribute.rb +137 -30
- data/lib/virtus/attribute/array.rb +17 -1
- data/lib/virtus/attribute/boolean.rb +8 -13
- data/lib/virtus/attribute/collection.rb +96 -0
- data/lib/virtus/attribute/default_value.rb +11 -7
- data/lib/virtus/attribute/embedded_value.rb +70 -0
- data/lib/virtus/attribute/set.rb +25 -0
- data/lib/virtus/attribute_set.rb +18 -16
- data/lib/virtus/attributes_accessor.rb +66 -0
- data/lib/virtus/class_methods.rb +50 -28
- data/lib/virtus/coercion/array.rb +23 -0
- data/lib/virtus/coercion/string.rb +10 -4
- data/lib/virtus/instance_methods.rb +38 -31
- data/lib/virtus/support/options.rb +6 -8
- data/lib/virtus/support/type_lookup.rb +4 -11
- data/lib/virtus/version.rb +1 -1
- data/spec/integration/collection_member_coercion_spec.rb +75 -0
- data/spec/integration/custom_attributes_spec.rb +49 -0
- data/spec/integration/default_values_spec.rb +32 -0
- data/spec/integration/defining_attributes_spec.rb +79 -0
- data/spec/integration/embedded_value_spec.rb +50 -0
- data/spec/integration/overriding_virtus_spec.rb +46 -0
- data/spec/integration/virtus/instance_level_attributes_spec.rb +23 -0
- data/spec/rcov.opts +1 -0
- data/spec/shared/constants_helpers.rb +9 -0
- data/spec/shared/options_class_method.rb +19 -0
- data/spec/spec_helper.rb +20 -7
- data/spec/unit/virtus/attribute/array/coerce_spec.rb +13 -0
- data/spec/unit/virtus/attribute/boolean/coerce_spec.rb +85 -0
- data/spec/unit/virtus/attribute/boolean/define_reader_method_spec.rb +15 -0
- data/spec/unit/virtus/attribute/boolean/value_coerced_spec.rb +97 -0
- data/spec/unit/virtus/attribute/boolean_spec.rb +2 -81
- data/spec/unit/virtus/attribute/class/coerce_spec.rb +13 -0
- data/spec/unit/virtus/attribute/class_methods/accessor_spec.rb +12 -0
- data/spec/unit/virtus/attribute/class_methods/build_spec.rb +37 -0
- data/spec/unit/virtus/attribute/class_methods/coercion_method_spec.rb +9 -0
- data/spec/unit/virtus/attribute/class_methods/default_spec.rb +9 -0
- data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +31 -1
- data/spec/unit/virtus/attribute/class_methods/merge_options_spec.rb +11 -0
- data/spec/unit/virtus/attribute/class_methods/primitive_spec.rb +9 -0
- data/spec/unit/virtus/attribute/class_methods/reader_spec.rb +9 -0
- data/spec/unit/virtus/attribute/class_methods/writer_spec.rb +9 -0
- data/spec/unit/virtus/attribute/coerce_spec.rb +30 -0
- data/spec/unit/virtus/attribute/coercion_method_spec.rb +12 -0
- data/spec/unit/virtus/attribute/collection/class_methods/merge_options_spec.rb +40 -0
- data/spec/unit/virtus/attribute/collection/coerce_spec.rb +26 -0
- data/spec/unit/virtus/attribute/date/coerce_spec.rb +47 -0
- data/spec/unit/virtus/attribute/date/value_coerced_spec.rb +46 -0
- data/spec/unit/virtus/attribute/date_time/coerce_spec.rb +68 -0
- data/spec/unit/virtus/attribute/decimal/coerce_spec.rb +117 -0
- data/spec/unit/virtus/attribute/default_spec.rb +32 -0
- data/spec/unit/virtus/attribute/default_value/attribute_spec.rb +11 -0
- data/spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb +4 -2
- data/spec/unit/virtus/attribute/default_value/evaluate_spec.rb +51 -0
- data/spec/unit/virtus/attribute/default_value/instance_methods/evaluate_spec.rb +9 -6
- data/spec/unit/virtus/attribute/default_value/value_spec.rb +11 -0
- data/spec/unit/virtus/attribute/define_accessor_methods_spec.rb +26 -0
- data/spec/unit/virtus/attribute/define_reader_method_spec.rb +24 -0
- data/spec/unit/virtus/attribute/define_writer_method_spec.rb +24 -0
- data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +17 -0
- data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +50 -0
- data/spec/unit/virtus/attribute/float/coerce_spec.rb +117 -0
- data/spec/unit/virtus/attribute/get_spec.rb +80 -0
- data/spec/unit/virtus/attribute/inspect_spec.rb +27 -0
- data/spec/unit/virtus/attribute/instance_variable_name_spec.rb +12 -0
- data/spec/unit/virtus/attribute/integer/coerce_spec.rb +105 -0
- data/spec/unit/virtus/attribute/name_spec.rb +12 -0
- data/spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb +1 -1
- data/spec/unit/virtus/attribute/numeric/class_methods/max_spec.rb +9 -0
- data/spec/unit/virtus/attribute/numeric/class_methods/min_spec.rb +9 -0
- data/spec/unit/virtus/attribute/object/class_methods/descendants_spec.rb +9 -7
- data/spec/unit/virtus/attribute/options_spec.rb +14 -0
- data/spec/unit/virtus/attribute/public_reader_spec.rb +24 -0
- data/spec/unit/virtus/attribute/public_writer_spec.rb +24 -0
- data/spec/unit/virtus/attribute/reader_visibility_spec.rb +24 -0
- data/spec/unit/virtus/attribute/set/coerce_spec.rb +13 -0
- data/spec/unit/virtus/attribute/set_spec.rb +49 -0
- data/spec/unit/virtus/attribute/string/coerce_spec.rb +11 -0
- data/spec/unit/virtus/attribute/time/coerce_spec.rb +67 -0
- data/spec/unit/virtus/attribute/value_coerced_spec.rb +19 -0
- data/spec/unit/virtus/attribute/writer_visibility_spec.rb +24 -0
- data/spec/unit/virtus/attribute_set/append_spec.rb +12 -0
- data/spec/unit/virtus/attribute_set/element_reference_spec.rb +4 -0
- data/spec/unit/virtus/attribute_set/element_set_spec.rb +29 -9
- data/spec/unit/virtus/attributes_accessor/inspect_spec.rb +9 -0
- data/spec/unit/virtus/class_methods/attribute_spec.rb +23 -5
- data/spec/unit/virtus/class_methods/attributes_spec.rb +3 -5
- data/spec/unit/virtus/class_methods/const_missing_spec.rb +27 -0
- data/spec/unit/virtus/class_methods/inherited_spec.rb +21 -0
- data/spec/unit/virtus/coercion/array/to_set_spec.rb +12 -0
- data/spec/unit/virtus/coercion/date/class_methods/to_date_spec.rb +10 -0
- data/spec/unit/virtus/coercion/date_time/class_methods/to_datetime_spec.rb +10 -0
- data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +10 -3
- data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +10 -3
- data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +10 -3
- data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +8 -8
- data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +2 -2
- data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +1 -1
- data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +1 -1
- data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +13 -13
- data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +25 -1
- data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +25 -1
- data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +30 -1
- data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +13 -13
- data/spec/unit/virtus/coercion/time/class_methods/to_time_spec.rb +10 -0
- data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +1 -1
- data/spec/unit/virtus/instance_methods/attributes_spec.rb +77 -20
- data/spec/unit/virtus/instance_methods/element_reference_spec.rb +1 -1
- data/spec/unit/virtus/instance_methods/element_set_spec.rb +2 -2
- data/spec/unit/virtus/instance_methods/to_hash_spec.rb +23 -0
- data/spec/unit/virtus/options/accept_options_spec.rb +10 -11
- data/spec/unit/virtus/options/accepted_options_spec.rb +1 -1
- data/spec/unit/virtus/options/options_spec.rb +27 -4
- data/tasks/metrics/ci.rake +2 -1
- data/tasks/metrics/heckle.rake +207 -0
- data/tasks/spec.rake +14 -7
- data/virtus.gemspec +1 -1
- metadata +111 -97
- data/VERSION +0 -1
- data/examples/custom_coercion_spec.rb +0 -50
- data/examples/default_values_spec.rb +0 -21
- data/examples/override_attribute_methods_spec.rb +0 -40
- data/spec/integration/virtus/attributes/attribute/set_spec.rb +0 -36
- data/spec/integration/virtus/class_methods/attribute_spec.rb +0 -82
- data/spec/integration/virtus/class_methods/attributes_spec.rb +0 -22
- data/spec/integration/virtus/class_methods/const_missing_spec.rb +0 -44
- data/spec/unit/shared/attribute.rb +0 -7
- data/spec/unit/shared/attribute/accept_options.rb +0 -37
- data/spec/unit/shared/attribute/accepted_options.rb +0 -5
- data/spec/unit/shared/attribute/get.rb +0 -44
- data/spec/unit/shared/attribute/inspect.rb +0 -7
- data/spec/unit/shared/attribute/set.rb +0 -37
- data/spec/unit/virtus/attribute/array_spec.rb +0 -24
- data/spec/unit/virtus/attribute/class_spec.rb +0 -24
- data/spec/unit/virtus/attribute/date_spec.rb +0 -59
- data/spec/unit/virtus/attribute/date_time_spec.rb +0 -87
- data/spec/unit/virtus/attribute/decimal_spec.rb +0 -109
- data/spec/unit/virtus/attribute/float_spec.rb +0 -109
- data/spec/unit/virtus/attribute/hash_spec.rb +0 -11
- data/spec/unit/virtus/attribute/integer_spec.rb +0 -99
- data/spec/unit/virtus/attribute/string_spec.rb +0 -21
- data/spec/unit/virtus/attribute/time_spec.rb +0 -82
- data/spec/unit/virtus/class_methods/new_spec.rb +0 -41
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::DateTime do
|
|
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 }
|
|
8
|
-
let(:attribute_default) { DateTime.now-1 }
|
|
9
|
-
let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :created_at } }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe '#coerce' do
|
|
13
|
-
let(:attribute) { described_class.new(:bday) }
|
|
14
|
-
|
|
15
|
-
let(:year) { 2011 }
|
|
16
|
-
let(:month) { 4 }
|
|
17
|
-
let(:day) { 7 }
|
|
18
|
-
let(:hour) { 1 }
|
|
19
|
-
let(:min) { 26 }
|
|
20
|
-
let(:sec) { 49 }
|
|
21
|
-
|
|
22
|
-
subject { attribute.coerce(value) }
|
|
23
|
-
|
|
24
|
-
shared_examples_for "a correct date time" do
|
|
25
|
-
it { should be_kind_of(DateTime) }
|
|
26
|
-
|
|
27
|
-
its(:year) { should eql(year) }
|
|
28
|
-
its(:month) { should eql(month) }
|
|
29
|
-
its(:day) { should eql(day) }
|
|
30
|
-
its(:hour) { should eql(hour) }
|
|
31
|
-
its(:min) { should eql(min) }
|
|
32
|
-
its(:sec) { should eql(sec) }
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
context 'with a date' do
|
|
36
|
-
it_should_behave_like "a correct date time" do
|
|
37
|
-
let(:hour) { 0 }
|
|
38
|
-
let(:min) { 0 }
|
|
39
|
-
let(:sec) { 0 }
|
|
40
|
-
|
|
41
|
-
let(:value) do
|
|
42
|
-
Date.new(year, month, day)
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context 'with a time' do
|
|
48
|
-
it_should_behave_like "a correct date time" do
|
|
49
|
-
let(:value) do
|
|
50
|
-
Time.local(year, month, day, hour, min, sec)
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
context 'with a hash' do
|
|
56
|
-
it_should_behave_like "a correct date time" do
|
|
57
|
-
let(:value) do
|
|
58
|
-
{ :year => year, :month => month, :day => day,
|
|
59
|
-
:hour => hour, :min => min, :sec => sec }
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
context 'with a string' do
|
|
65
|
-
context "without hour, min and sec" do
|
|
66
|
-
let(:hour) { 0 }
|
|
67
|
-
let(:min) { 0 }
|
|
68
|
-
let(:sec) { 0 }
|
|
69
|
-
|
|
70
|
-
it_should_behave_like "a correct date time" do
|
|
71
|
-
let(:value) { "April #{day}th, #{year}" }
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
context "with hour, min and sec" do
|
|
76
|
-
it_should_behave_like "a correct date time" do
|
|
77
|
-
let(:value) { "April #{day}th, #{year}, #{hour}:#{min}:#{sec}" }
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
context 'with a on-date value' do
|
|
83
|
-
let(:value) { 'non-date' }
|
|
84
|
-
it { should equal(value) }
|
|
85
|
-
end
|
|
86
|
-
end
|
|
87
|
-
end
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::Decimal do
|
|
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" }
|
|
8
|
-
let(:attribute_default) { BigDecimal('0') }
|
|
9
|
-
let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :price } }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe '#coerce' do
|
|
13
|
-
let(:attribute) { described_class.new(:price) }
|
|
14
|
-
|
|
15
|
-
subject { attribute.coerce(value) }
|
|
16
|
-
|
|
17
|
-
context "with 24.0 big decimal" do
|
|
18
|
-
let(:value) { BigDecimal('24.0') }
|
|
19
|
-
it { should eql(value) }
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
context 'with a zero string integer' do
|
|
23
|
-
let(:value) { '0' }
|
|
24
|
-
it { should eql(BigDecimal('0.0')) }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
context 'with a positive string integer' do
|
|
28
|
-
let(:value) { '24' }
|
|
29
|
-
it { should eql(BigDecimal('24.0')) }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
context 'with a negative string integer' do
|
|
33
|
-
let(:value) { '-24' }
|
|
34
|
-
it { should eql(BigDecimal('-24.0')) }
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
context 'with a zero string float' do
|
|
38
|
-
let(:value) { '0.0' }
|
|
39
|
-
it { should eql(BigDecimal('0.0')) }
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
context 'with a positive string float' do
|
|
43
|
-
let(:value) { '24.35' }
|
|
44
|
-
it { should eql(BigDecimal('24.35')) }
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context 'with a negative string float' do
|
|
48
|
-
let(:value) { '-24.35' }
|
|
49
|
-
it { should eql(BigDecimal('-24.35')) }
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context 'with a zero string float, with no leading digits' do
|
|
53
|
-
let(:value) { '.0' }
|
|
54
|
-
it { should eql(BigDecimal('0.0')) }
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
context 'with a positive string float, with no leading digits' do
|
|
58
|
-
let(:value) { '0.41' }
|
|
59
|
-
it { should eql(BigDecimal('0.41')) }
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
context 'with a zero integer' do
|
|
63
|
-
let(:value) { 0 }
|
|
64
|
-
it { should eql(BigDecimal('0.0')) }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
context 'with a positive integer' do
|
|
68
|
-
let(:value) { 24 }
|
|
69
|
-
it { should eql(BigDecimal('24.0')) }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
context 'with a negative integer' do
|
|
73
|
-
let(:value) { -24 }
|
|
74
|
-
it { should eql(BigDecimal('-24.0')) }
|
|
75
|
-
end
|
|
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
|
-
|
|
87
|
-
context 'with a zero float' do
|
|
88
|
-
let(:value) { 0.0 }
|
|
89
|
-
it { should eql(BigDecimal('0.0')) }
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
context 'with a positive float' do
|
|
93
|
-
let(:value) { 24.35 }
|
|
94
|
-
it { should eql(BigDecimal('24.35')) }
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
context 'with a negative float' do
|
|
98
|
-
let(:value) { -24.35 }
|
|
99
|
-
it { should eql(BigDecimal('-24.35')) }
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |non_num_value|
|
|
103
|
-
context "with a non-numeric value = #{non_num_value.inspect}" do
|
|
104
|
-
let(:value) { non_num_value }
|
|
105
|
-
it { should equal(non_num_value) }
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::Float do
|
|
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" }
|
|
8
|
-
let(:attribute_default) { 0.0 }
|
|
9
|
-
let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :score } }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe '#coerce' do
|
|
13
|
-
let(:attribute) { described_class.new(:score) }
|
|
14
|
-
|
|
15
|
-
subject { attribute.coerce(value) }
|
|
16
|
-
|
|
17
|
-
context "with a float" do
|
|
18
|
-
let(:value) { 24.0 }
|
|
19
|
-
it { should eql(value) }
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
context 'with a zero string integer' do
|
|
23
|
-
let(:value) { '0' }
|
|
24
|
-
it { should eql(0.0) }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
context 'with a positive string integer' do
|
|
28
|
-
let(:value) { '24' }
|
|
29
|
-
it { should eql(24.0) }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
context 'with a negative string integer' do
|
|
33
|
-
let(:value) { '-24' }
|
|
34
|
-
it { should eql(-24.0) }
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
context 'with a zero string float' do
|
|
38
|
-
let(:value) { '0.0' }
|
|
39
|
-
it { should eql(0.0) }
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
context 'with a positive string float' do
|
|
43
|
-
let(:value) { '24.35' }
|
|
44
|
-
it { should eql(24.35) }
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context 'with a negative string float' do
|
|
48
|
-
let(:value) { '-24.35' }
|
|
49
|
-
it { should eql(-24.35) }
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context 'with a zero string float, with no leading digits' do
|
|
53
|
-
let(:value) { '.0' }
|
|
54
|
-
it { should eql(0.0) }
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
context 'with a positive string float, with no leading digits' do
|
|
58
|
-
let(:value) { '.41' }
|
|
59
|
-
it { should eql(0.41) }
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
context 'with a zero integer' do
|
|
63
|
-
let(:value) { 0 }
|
|
64
|
-
it { should eql(0.0) }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
context 'with a positive integer' do
|
|
68
|
-
let(:value) { 24 }
|
|
69
|
-
it { should eql(24.0) }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
context 'with a negative integer' do
|
|
73
|
-
let(:value) { -24 }
|
|
74
|
-
it { should eql(-24.0) }
|
|
75
|
-
end
|
|
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
|
-
|
|
87
|
-
context 'with a zero decimal' do
|
|
88
|
-
let(:value) { BigDecimal('0.0') }
|
|
89
|
-
it { should eql(0.0) }
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
context 'with a positive decimal' do
|
|
93
|
-
let(:value) { BigDecimal('24.35') }
|
|
94
|
-
it { should eql(24.35) }
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
context 'with a negative decimal' do
|
|
98
|
-
let(:value) { BigDecimal('-24.35') }
|
|
99
|
-
it { should eql(-24.35) }
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
[ Object.new, true, '00.0', '0.', '-.0', 'string' ].each do |non_num_value|
|
|
103
|
-
context "does not coerce non-numeric value #{non_num_value.inspect}" do
|
|
104
|
-
let(:value) { non_num_value }
|
|
105
|
-
it { should equal(non_num_value) }
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
end
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::Hash do
|
|
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] }
|
|
8
|
-
let(:attribute_default) { Hash.new }
|
|
9
|
-
let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :settings } }
|
|
10
|
-
end
|
|
11
|
-
end
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::Integer do
|
|
4
|
-
it_should_behave_like 'Attribute' do
|
|
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 } }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe '#coerce' do
|
|
13
|
-
let(:attribute) { described_class.new(:age) }
|
|
14
|
-
|
|
15
|
-
subject { attribute.coerce(value) }
|
|
16
|
-
|
|
17
|
-
context "with an integer" do
|
|
18
|
-
let(:value) { 24 }
|
|
19
|
-
it { should eql(value) }
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
context 'with a zero string integer' do
|
|
23
|
-
let(:value) { 24 }
|
|
24
|
-
it { should eql(value) }
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
context 'with a positive string integer' do
|
|
28
|
-
let(:value) { '24' }
|
|
29
|
-
it { should eql(24) }
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
context 'with a negative string integer' do
|
|
33
|
-
let(:value) { '-24' }
|
|
34
|
-
it { should eql(-24) }
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
context 'with a zero string float' do
|
|
38
|
-
let(:value) { 0.0 }
|
|
39
|
-
it { should eql(0) }
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
context 'with a positive string float' do
|
|
43
|
-
let(:value) { '24.35' }
|
|
44
|
-
it { should eql(24) }
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context 'with a negative string float' do
|
|
48
|
-
let(:value) { '-24.35' }
|
|
49
|
-
it { should eql(-24) }
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
context 'with a zero string float, with no leading digits' do
|
|
53
|
-
let(:value) { '.0' }
|
|
54
|
-
it { should eql(0) }
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
context 'with a positive string float, with no leading digits' do
|
|
58
|
-
let(:value) { '.41' }
|
|
59
|
-
it { should eql(0) }
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
context 'with a zero float' do
|
|
63
|
-
let(:value) { 0.0 }
|
|
64
|
-
it { should eql(0) }
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
context 'with a positive float' do
|
|
68
|
-
let(:value) { 24.35 }
|
|
69
|
-
it { should eql(24) }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
context 'with a negative float' do
|
|
73
|
-
let(:value) { -24.35 }
|
|
74
|
-
it { should eql(-24) }
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
context 'with a zero decimal' do
|
|
78
|
-
let(:value) { BigDecimal('0.0') }
|
|
79
|
-
it { should eql(0) }
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
context 'with a positive decimal' do
|
|
83
|
-
let(:value) { BigDecimal('24.35') }
|
|
84
|
-
it { should eql(24) }
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
context 'with a negative decimal' do
|
|
88
|
-
let(:value) { BigDecimal('-24.35') }
|
|
89
|
-
it { should eql(-24) }
|
|
90
|
-
end
|
|
91
|
-
|
|
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
|
|
94
|
-
let(:value) { non_num_value }
|
|
95
|
-
it { should equal(non_num_value) }
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
|
99
|
-
end
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::String do
|
|
4
|
-
it_should_behave_like 'Attribute' do
|
|
5
|
-
let(:attribute_name) { :email }
|
|
6
|
-
let(:attribute_value) { 'red john' }
|
|
7
|
-
let(:attribute_value_other) { :'red john' }
|
|
8
|
-
let(:attribute_default) { '' }
|
|
9
|
-
let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :email } }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe '#coerce' do
|
|
13
|
-
let(:attribute) { described_class.new(:name) }
|
|
14
|
-
let(:value) { 1 }
|
|
15
|
-
let(:coerce_value) { '1' }
|
|
16
|
-
|
|
17
|
-
subject { attribute.coerce(value) }
|
|
18
|
-
|
|
19
|
-
it { should eql(coerce_value) }
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Virtus::Attribute::Time do
|
|
4
|
-
it_should_behave_like 'Attribute' do
|
|
5
|
-
let(:attribute_name) { :birthday }
|
|
6
|
-
let(:attribute_value) { Time.now }
|
|
7
|
-
let(:attribute_value_other) { Time.now.to_s }
|
|
8
|
-
let(:attribute_default) { Time.now-1 }
|
|
9
|
-
let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :birthday } }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
describe '#coerce' do
|
|
13
|
-
let(:attribute) { described_class.new(:bday) }
|
|
14
|
-
|
|
15
|
-
let(:year) { 1983 }
|
|
16
|
-
let(:month) { 11 }
|
|
17
|
-
let(:day) { 18 }
|
|
18
|
-
let(:hour) { 8 }
|
|
19
|
-
let(:min) { 16 }
|
|
20
|
-
let(:sec) { 32 }
|
|
21
|
-
|
|
22
|
-
subject { attribute.coerce(value) }
|
|
23
|
-
|
|
24
|
-
shared_examples_for "a correct time" do
|
|
25
|
-
it { should be_kind_of(Time) }
|
|
26
|
-
its(:year) { should eql(year) }
|
|
27
|
-
its(:month) { should eql(month) }
|
|
28
|
-
its(:day) { should eql(day) }
|
|
29
|
-
its(:hour) { should eql(hour) }
|
|
30
|
-
its(:min) { should eql(min) }
|
|
31
|
-
its(:sec) { should eql(sec) }
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
context 'with a date' do
|
|
35
|
-
let(:hour) { 0 }
|
|
36
|
-
let(:min) { 0 }
|
|
37
|
-
let(:sec) { 0 }
|
|
38
|
-
|
|
39
|
-
it_should_behave_like "a correct time" do
|
|
40
|
-
let(:value) { DateTime.new(year, month, day, hour, min, sec) }
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
context 'with a date time' do
|
|
45
|
-
it_should_behave_like "a correct time" do
|
|
46
|
-
let(:value) { DateTime.new(year, month, day, hour, min, sec) }
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
context 'with a hash' do
|
|
51
|
-
it_should_behave_like "a correct time" do
|
|
52
|
-
let(:value) do
|
|
53
|
-
{ :year => year, :month => month, :day => day,
|
|
54
|
-
:hour => hour, :min => min, :sec => sec }
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
context 'with a string' do
|
|
60
|
-
context "without hour, min and sec" do
|
|
61
|
-
let(:hour) { 0 }
|
|
62
|
-
let(:min) { 0 }
|
|
63
|
-
let(:sec) { 0 }
|
|
64
|
-
|
|
65
|
-
it_should_behave_like "a correct time" do
|
|
66
|
-
let(:value) { "November #{day}th, #{year}" }
|
|
67
|
-
end
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
context "with hour, min and sec" do
|
|
71
|
-
it_should_behave_like "a correct time" do
|
|
72
|
-
let(:value) { "November #{day}th, #{year}, #{hour}:#{min}:#{sec}" }
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
context 'with a non-date value' do
|
|
78
|
-
let(:value) { '2999' }
|
|
79
|
-
it { should equal(value) }
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
end
|