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.
Files changed (154) hide show
  1. data/.gitignore +33 -7
  2. data/.travis.yml +4 -74
  3. data/Changelog.md +11 -2
  4. data/Gemfile +10 -9
  5. data/README.md +85 -1
  6. data/TODO +18 -0
  7. data/config/flay.yml +2 -2
  8. data/config/flog.yml +1 -1
  9. data/config/roodi.yml +3 -3
  10. data/config/site.reek +8 -3
  11. data/lib/virtus.rb +5 -0
  12. data/lib/virtus/attribute.rb +137 -30
  13. data/lib/virtus/attribute/array.rb +17 -1
  14. data/lib/virtus/attribute/boolean.rb +8 -13
  15. data/lib/virtus/attribute/collection.rb +96 -0
  16. data/lib/virtus/attribute/default_value.rb +11 -7
  17. data/lib/virtus/attribute/embedded_value.rb +70 -0
  18. data/lib/virtus/attribute/set.rb +25 -0
  19. data/lib/virtus/attribute_set.rb +18 -16
  20. data/lib/virtus/attributes_accessor.rb +66 -0
  21. data/lib/virtus/class_methods.rb +50 -28
  22. data/lib/virtus/coercion/array.rb +23 -0
  23. data/lib/virtus/coercion/string.rb +10 -4
  24. data/lib/virtus/instance_methods.rb +38 -31
  25. data/lib/virtus/support/options.rb +6 -8
  26. data/lib/virtus/support/type_lookup.rb +4 -11
  27. data/lib/virtus/version.rb +1 -1
  28. data/spec/integration/collection_member_coercion_spec.rb +75 -0
  29. data/spec/integration/custom_attributes_spec.rb +49 -0
  30. data/spec/integration/default_values_spec.rb +32 -0
  31. data/spec/integration/defining_attributes_spec.rb +79 -0
  32. data/spec/integration/embedded_value_spec.rb +50 -0
  33. data/spec/integration/overriding_virtus_spec.rb +46 -0
  34. data/spec/integration/virtus/instance_level_attributes_spec.rb +23 -0
  35. data/spec/rcov.opts +1 -0
  36. data/spec/shared/constants_helpers.rb +9 -0
  37. data/spec/shared/options_class_method.rb +19 -0
  38. data/spec/spec_helper.rb +20 -7
  39. data/spec/unit/virtus/attribute/array/coerce_spec.rb +13 -0
  40. data/spec/unit/virtus/attribute/boolean/coerce_spec.rb +85 -0
  41. data/spec/unit/virtus/attribute/boolean/define_reader_method_spec.rb +15 -0
  42. data/spec/unit/virtus/attribute/boolean/value_coerced_spec.rb +97 -0
  43. data/spec/unit/virtus/attribute/boolean_spec.rb +2 -81
  44. data/spec/unit/virtus/attribute/class/coerce_spec.rb +13 -0
  45. data/spec/unit/virtus/attribute/class_methods/accessor_spec.rb +12 -0
  46. data/spec/unit/virtus/attribute/class_methods/build_spec.rb +37 -0
  47. data/spec/unit/virtus/attribute/class_methods/coercion_method_spec.rb +9 -0
  48. data/spec/unit/virtus/attribute/class_methods/default_spec.rb +9 -0
  49. data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +31 -1
  50. data/spec/unit/virtus/attribute/class_methods/merge_options_spec.rb +11 -0
  51. data/spec/unit/virtus/attribute/class_methods/primitive_spec.rb +9 -0
  52. data/spec/unit/virtus/attribute/class_methods/reader_spec.rb +9 -0
  53. data/spec/unit/virtus/attribute/class_methods/writer_spec.rb +9 -0
  54. data/spec/unit/virtus/attribute/coerce_spec.rb +30 -0
  55. data/spec/unit/virtus/attribute/coercion_method_spec.rb +12 -0
  56. data/spec/unit/virtus/attribute/collection/class_methods/merge_options_spec.rb +40 -0
  57. data/spec/unit/virtus/attribute/collection/coerce_spec.rb +26 -0
  58. data/spec/unit/virtus/attribute/date/coerce_spec.rb +47 -0
  59. data/spec/unit/virtus/attribute/date/value_coerced_spec.rb +46 -0
  60. data/spec/unit/virtus/attribute/date_time/coerce_spec.rb +68 -0
  61. data/spec/unit/virtus/attribute/decimal/coerce_spec.rb +117 -0
  62. data/spec/unit/virtus/attribute/default_spec.rb +32 -0
  63. data/spec/unit/virtus/attribute/default_value/attribute_spec.rb +11 -0
  64. data/spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb +4 -2
  65. data/spec/unit/virtus/attribute/default_value/evaluate_spec.rb +51 -0
  66. data/spec/unit/virtus/attribute/default_value/instance_methods/evaluate_spec.rb +9 -6
  67. data/spec/unit/virtus/attribute/default_value/value_spec.rb +11 -0
  68. data/spec/unit/virtus/attribute/define_accessor_methods_spec.rb +26 -0
  69. data/spec/unit/virtus/attribute/define_reader_method_spec.rb +24 -0
  70. data/spec/unit/virtus/attribute/define_writer_method_spec.rb +24 -0
  71. data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +17 -0
  72. data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +50 -0
  73. data/spec/unit/virtus/attribute/float/coerce_spec.rb +117 -0
  74. data/spec/unit/virtus/attribute/get_spec.rb +80 -0
  75. data/spec/unit/virtus/attribute/inspect_spec.rb +27 -0
  76. data/spec/unit/virtus/attribute/instance_variable_name_spec.rb +12 -0
  77. data/spec/unit/virtus/attribute/integer/coerce_spec.rb +105 -0
  78. data/spec/unit/virtus/attribute/name_spec.rb +12 -0
  79. data/spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb +1 -1
  80. data/spec/unit/virtus/attribute/numeric/class_methods/max_spec.rb +9 -0
  81. data/spec/unit/virtus/attribute/numeric/class_methods/min_spec.rb +9 -0
  82. data/spec/unit/virtus/attribute/object/class_methods/descendants_spec.rb +9 -7
  83. data/spec/unit/virtus/attribute/options_spec.rb +14 -0
  84. data/spec/unit/virtus/attribute/public_reader_spec.rb +24 -0
  85. data/spec/unit/virtus/attribute/public_writer_spec.rb +24 -0
  86. data/spec/unit/virtus/attribute/reader_visibility_spec.rb +24 -0
  87. data/spec/unit/virtus/attribute/set/coerce_spec.rb +13 -0
  88. data/spec/unit/virtus/attribute/set_spec.rb +49 -0
  89. data/spec/unit/virtus/attribute/string/coerce_spec.rb +11 -0
  90. data/spec/unit/virtus/attribute/time/coerce_spec.rb +67 -0
  91. data/spec/unit/virtus/attribute/value_coerced_spec.rb +19 -0
  92. data/spec/unit/virtus/attribute/writer_visibility_spec.rb +24 -0
  93. data/spec/unit/virtus/attribute_set/append_spec.rb +12 -0
  94. data/spec/unit/virtus/attribute_set/element_reference_spec.rb +4 -0
  95. data/spec/unit/virtus/attribute_set/element_set_spec.rb +29 -9
  96. data/spec/unit/virtus/attributes_accessor/inspect_spec.rb +9 -0
  97. data/spec/unit/virtus/class_methods/attribute_spec.rb +23 -5
  98. data/spec/unit/virtus/class_methods/attributes_spec.rb +3 -5
  99. data/spec/unit/virtus/class_methods/const_missing_spec.rb +27 -0
  100. data/spec/unit/virtus/class_methods/inherited_spec.rb +21 -0
  101. data/spec/unit/virtus/coercion/array/to_set_spec.rb +12 -0
  102. data/spec/unit/virtus/coercion/date/class_methods/to_date_spec.rb +10 -0
  103. data/spec/unit/virtus/coercion/date_time/class_methods/to_datetime_spec.rb +10 -0
  104. data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +10 -3
  105. data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +10 -3
  106. data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +10 -3
  107. data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +8 -8
  108. data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +2 -2
  109. data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +1 -1
  110. data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +1 -1
  111. data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +13 -13
  112. data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +25 -1
  113. data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +25 -1
  114. data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +30 -1
  115. data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +13 -13
  116. data/spec/unit/virtus/coercion/time/class_methods/to_time_spec.rb +10 -0
  117. data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +1 -1
  118. data/spec/unit/virtus/instance_methods/attributes_spec.rb +77 -20
  119. data/spec/unit/virtus/instance_methods/element_reference_spec.rb +1 -1
  120. data/spec/unit/virtus/instance_methods/element_set_spec.rb +2 -2
  121. data/spec/unit/virtus/instance_methods/to_hash_spec.rb +23 -0
  122. data/spec/unit/virtus/options/accept_options_spec.rb +10 -11
  123. data/spec/unit/virtus/options/accepted_options_spec.rb +1 -1
  124. data/spec/unit/virtus/options/options_spec.rb +27 -4
  125. data/tasks/metrics/ci.rake +2 -1
  126. data/tasks/metrics/heckle.rake +207 -0
  127. data/tasks/spec.rake +14 -7
  128. data/virtus.gemspec +1 -1
  129. metadata +111 -97
  130. data/VERSION +0 -1
  131. data/examples/custom_coercion_spec.rb +0 -50
  132. data/examples/default_values_spec.rb +0 -21
  133. data/examples/override_attribute_methods_spec.rb +0 -40
  134. data/spec/integration/virtus/attributes/attribute/set_spec.rb +0 -36
  135. data/spec/integration/virtus/class_methods/attribute_spec.rb +0 -82
  136. data/spec/integration/virtus/class_methods/attributes_spec.rb +0 -22
  137. data/spec/integration/virtus/class_methods/const_missing_spec.rb +0 -44
  138. data/spec/unit/shared/attribute.rb +0 -7
  139. data/spec/unit/shared/attribute/accept_options.rb +0 -37
  140. data/spec/unit/shared/attribute/accepted_options.rb +0 -5
  141. data/spec/unit/shared/attribute/get.rb +0 -44
  142. data/spec/unit/shared/attribute/inspect.rb +0 -7
  143. data/spec/unit/shared/attribute/set.rb +0 -37
  144. data/spec/unit/virtus/attribute/array_spec.rb +0 -24
  145. data/spec/unit/virtus/attribute/class_spec.rb +0 -24
  146. data/spec/unit/virtus/attribute/date_spec.rb +0 -59
  147. data/spec/unit/virtus/attribute/date_time_spec.rb +0 -87
  148. data/spec/unit/virtus/attribute/decimal_spec.rb +0 -109
  149. data/spec/unit/virtus/attribute/float_spec.rb +0 -109
  150. data/spec/unit/virtus/attribute/hash_spec.rb +0 -11
  151. data/spec/unit/virtus/attribute/integer_spec.rb +0 -99
  152. data/spec/unit/virtus/attribute/string_spec.rb +0 -21
  153. data/spec/unit/virtus/attribute/time_spec.rb +0 -82
  154. data/spec/unit/virtus/class_methods/new_spec.rb +0 -41
@@ -1,40 +0,0 @@
1
- require './spec/spec_helper'
2
-
3
- class Article
4
- include Virtus
5
-
6
- attribute :author, String
7
-
8
- def author
9
- super || '<unknown>'
10
- end
11
-
12
- def author=(name)
13
- super unless name == 'Brad'
14
- end
15
-
16
- end
17
-
18
- describe Article, 'override' do
19
-
20
- it 'Alice is an allowed author' do
21
- Article.new(:author => 'Alice').author.should == 'Alice'
22
- end
23
-
24
- it 'Brad isn not an allowed author' do
25
- Article.new(:author => 'Brad').author.should == '<unknown>'
26
- end
27
-
28
- context 'with author' do
29
- subject { Article.new(:author => 'John') }
30
-
31
- its(:author) { should == 'John' }
32
- end
33
-
34
- context 'without author' do
35
- subject { Article.new }
36
-
37
- its(:author) { should == '<unknown>' }
38
- end
39
-
40
- end
@@ -1,36 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute, '#set' do
4
- let(:attribute_class) do
5
- Class.new(Virtus::Attribute::Integer) do
6
- def set(instance, value)
7
- super(instance, coerce(value) + 1) unless value.nil?
8
- end
9
- end
10
- end
11
-
12
- let(:model) do
13
- model = Class.new do
14
- include Virtus
15
- end
16
- model.attribute(:count, attribute_class)
17
- model
18
- end
19
-
20
- let(:object) do
21
- model.new
22
- end
23
-
24
- context 'when overridden' do
25
- let(:input_value) { 1 }
26
- let(:output_value) { 2 }
27
-
28
- before do
29
- object.count = input_value
30
- end
31
-
32
- subject { object }
33
-
34
- its(:count) { should eql(output_value) }
35
- end
36
- end
@@ -1,82 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::ClassMethods, '.attribute' do
4
- let(:described_class) do
5
- Class.new { include Virtus }
6
- end
7
-
8
- (Virtus::Attribute.descendants + [Virtus::Attribute::Object]).each do |attribute_class|
9
- context "with #{attribute_class.inspect}" do
10
- subject { described_class.attribute(:name, attribute_class) }
11
- specify { subject.attributes[:name].should be_instance_of(attribute_class) }
12
- end
13
- end
14
-
15
- specify { described_class.should respond_to(:attribute) }
16
-
17
- context "in the class" do
18
- before do
19
- described_class.attribute(:name, String)
20
- described_class.attribute(:email, String, :accessor => :private)
21
- described_class.attribute(:address, String, :accessor => :protected)
22
- described_class.attribute(:age, Integer, :reader => :private)
23
- described_class.attribute(:bday, Date, :writer => :protected)
24
- end
25
-
26
- let(:public_instance_methods) { described_class.public_instance_methods.map { |method| method.to_s } }
27
- let(:protected_instance_methods) { described_class.protected_instance_methods.map { |method| method.to_s } }
28
- let(:private_instance_methods) { described_class.private_instance_methods.map { |method| method.to_s } }
29
-
30
- it "returns self" do
31
- described_class.attribute(:name, String).should be(described_class)
32
- end
33
-
34
- it "creates an attribute of a correct type" do
35
- described_class.attributes[:name].should be_instance_of(Virtus::Attribute::String)
36
- end
37
-
38
- it "creates attribute writer" do
39
- public_instance_methods.should include('name=')
40
- end
41
-
42
- it "creates attribute reader" do
43
- public_instance_methods.should include('name')
44
- end
45
-
46
- it "creates attribute private reader when :accessor => :private" do
47
- private_instance_methods.should include('email')
48
- end
49
-
50
- it "creates attribute private writer when :accessor => :private" do
51
- private_instance_methods.should include('email=')
52
- end
53
-
54
- it "creates attribute protected reader when :accessor => :protected" do
55
- protected_instance_methods.should include('address')
56
- end
57
-
58
- it "creates attribute protected writer when :accessor => :protected" do
59
- protected_instance_methods.should include('address=')
60
- end
61
-
62
- it "creates attribute private reader when :reader => :private" do
63
- private_instance_methods.should include('age')
64
- end
65
-
66
- it "creates attribute protected writer when :writer => :protected" do
67
- protected_instance_methods.should include('bday=')
68
- end
69
- end
70
-
71
- context "in the descendants" do
72
- subject { described_class.attribute(:name, String).attributes[:name] }
73
-
74
- let(:descendant) { Class.new(described_class) }
75
-
76
- it 'updates the descendant attributes' do
77
- descendant.attributes.to_a.should be_empty
78
- @attribute = subject
79
- descendant.attributes.to_a.should eql([ @attribute ])
80
- end
81
- end
82
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::ClassMethods, '.attributes' do
4
- let(:described_class) do
5
- Class.new { include Virtus }
6
- end
7
-
8
- it { described_class.should respond_to(:attributes) }
9
-
10
- describe ".attributes" do
11
- before do
12
- described_class.attribute(:name, String)
13
- described_class.attribute(:age, Integer)
14
- end
15
-
16
- subject { described_class.attributes }
17
-
18
- it "returns a set of attributes" do
19
- subject.should be_kind_of(Virtus::AttributeSet)
20
- end
21
- end
22
- end
@@ -1,44 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::ClassMethods, '.const_missing' do
4
- after do
5
- Object.send(:remove_const, :User)
6
- end
7
-
8
- context "with an existing attribute constant which doesn't exist in the global ns" do
9
- before do
10
- class User
11
- include Virtus
12
- attribute :name, String
13
- end
14
- end
15
-
16
- it "should create attribute of the correct type" do
17
- User.attributes[:name].should be_instance_of(Virtus::Attribute::String)
18
- end
19
- end
20
-
21
- context "with an existing attribute constant which doesn't exist in the global ns" do
22
- before do
23
- class User
24
- include Virtus
25
- attribute :admin, Boolean
26
- end
27
- end
28
-
29
- it "should create attribute of the correct type" do
30
- User.attributes[:admin].should be_instance_of(Virtus::Attribute::Boolean)
31
- end
32
- end
33
-
34
- context "with an unknown constant" do
35
- it "should raise NameError" do
36
- expect {
37
- class User
38
- include Virtus
39
- attribute :not_gonna_work, NoSuchThing
40
- end
41
- }.to raise_error(NameError)
42
- end
43
- end
44
- end
@@ -1,7 +0,0 @@
1
- shared_examples_for 'Attribute' do
2
- it_behaves_like 'Attribute.accept_options'
3
- it_behaves_like 'Attribute.accepted_options'
4
- it_behaves_like 'Attribute#inspect'
5
- it_behaves_like 'Attribute#set'
6
- it_behaves_like 'Attribute#get'
7
- end
@@ -1,37 +0,0 @@
1
- shared_examples_for 'Attribute.accept_options' do
2
- let(:sub_attribute) { Class.new(described_class) }
3
- let(:new_option) { :width }
4
-
5
- before :all do
6
- described_class.accepted_options.should_not include(new_option)
7
- described_class.accept_options(new_option)
8
- end
9
-
10
- context 'with default option value' do
11
- let(:option) { :height }
12
- let(:value) { 10 }
13
-
14
- before :all do
15
- sub_attribute.accept_options(option)
16
- sub_attribute.height(value)
17
- end
18
-
19
- context "when new attribute is created" do
20
- subject { sub_attribute.new(attribute_name) }
21
-
22
- it "sets the default value" do
23
- subject.options[option].should eql(value)
24
- end
25
- end
26
-
27
- context "when new attribute is created and overrides option's default value" do
28
- let(:new_value) { 11 }
29
-
30
- subject { sub_attribute.new(attribute_name, option => new_value) }
31
-
32
- it "sets the new value" do
33
- subject.options[option].should eql(new_value)
34
- end
35
- end
36
- end
37
- end
@@ -1,5 +0,0 @@
1
- shared_examples_for 'Attribute.accepted_options' do
2
- it "includes base options" do
3
- described_class.accepted_options.should include(*Virtus::Attribute::OPTIONS)
4
- end
5
- end
@@ -1,44 +0,0 @@
1
- shared_examples_for 'Attribute#get' do
2
- subject { attribute.get(instance) }
3
-
4
- let(:model) { Class.new }
5
- let(:instance) { model.new }
6
-
7
- context 'without default value' do
8
- let(:attribute) { described_class.new(attribute_name) }
9
-
10
- before { attribute.set(instance, value) }
11
-
12
- context "when a non-nil value is set" do
13
- let(:value) { attribute_value }
14
- it { should eql(attribute_value) }
15
- end
16
-
17
- context "when nil is set" do
18
- let(:value) { nil }
19
- it { should be(value) }
20
- end
21
- end
22
-
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.evaluate(attribute)
31
- end
32
-
33
- it { should == evaluated_proc_value }
34
- end
35
-
36
- context 'not set to proc' do
37
- let(:attribute) do
38
- described_class.new(attribute_name, :default => attribute_default)
39
- end
40
-
41
- it { should == attribute_default }
42
- end
43
- end
44
- end
@@ -1,7 +0,0 @@
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
@@ -1,37 +0,0 @@
1
- shared_examples_for 'Attribute#set' do
2
- subject { attribute.set(instance, value) }
3
-
4
- let(:attribute) { described_class.new(attribute_name) }
5
- let(:model) { Class.new }
6
- let(:instance) { model.new }
7
-
8
- before { subject }
9
-
10
- context "with nil" do
11
- let(:value) { nil }
12
-
13
- it { should be(nil) }
14
-
15
- it "sets the value in an ivar" do
16
- instance.instance_variable_get(attribute.instance_variable_name).should be(nil)
17
- end
18
- end
19
-
20
- context "with a primitive value" do
21
- let(:value) { attribute_value }
22
-
23
- it { should == attribute_value }
24
-
25
- it "sets the value in an ivar" do
26
- instance.instance_variable_get(attribute.instance_variable_name).should eql(attribute_value)
27
- end
28
- end
29
-
30
- context "with a non-primitive value" do
31
- let(:value) { attribute_value_other }
32
-
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)
35
- end
36
- end
37
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute::Array do
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' ] }
8
- let(:attribute_default) { [] }
9
- let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :colors } }
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
-
24
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute::Class do
4
- it_should_behave_like 'Attribute' do
5
- let(:attribute_name) { :String }
6
- let(:attribute_value) { String }
7
- let(:attribute_value_other) { 'String' }
8
- let(:attribute_default) { 'Object' }
9
- let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name.to_s } }
10
- end
11
-
12
- describe '#coerce' do
13
- let(:attribute) { described_class.new(:type) }
14
-
15
- subject { attribute.coerce(value) }
16
-
17
- context 'with a String' do
18
- let(:value) { 'String' }
19
-
20
- it { should be(String) }
21
- end
22
- end
23
-
24
- end
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute::Date do
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 }
8
- let(:attribute_default) { Date.today-1 }
9
- let(:attribute_default_proc) { lambda { |instance, attribute| attribute.name == :created_on } }
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
-
19
- subject { attribute.coerce(value) }
20
-
21
- shared_examples_for "a correct date" do
22
- it { should be_kind_of(Date) }
23
- its(:year) { should eql(year) }
24
- its(:month) { should eql(month) }
25
- its(:day) { should eql(day) }
26
- end
27
-
28
- context 'with a time' do
29
- it_should_behave_like "a correct date" do
30
- let(:value) { Time.local(year, month, day) }
31
- end
32
- end
33
-
34
- context 'with a date time' do
35
- it_should_behave_like "a correct date" do
36
- let(:value) { DateTime.new(year, month, day) }
37
- end
38
- end
39
-
40
- context 'with a hash' do
41
- it_should_behave_like "a correct date" do
42
- let(:value) do
43
- { :year => year, :month => month, :day => day }
44
- end
45
- end
46
- end
47
-
48
- context 'with a string' do
49
- it_should_behave_like "a correct date" do
50
- let(:value) { "April #{day}th, #{year}" }
51
- end
52
- end
53
-
54
- context 'with a non-date value' do
55
- let(:value) { 'non-date' }
56
- it { should equal(value) }
57
- end
58
- end
59
- end