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
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'custom attribtues' do
|
|
4
|
+
|
|
5
|
+
module Virtus
|
|
6
|
+
class Coercion
|
|
7
|
+
class String < Virtus::Coercion::Object
|
|
8
|
+
def self.to_upcase(value)
|
|
9
|
+
value.upcase
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
before do
|
|
16
|
+
module Examples
|
|
17
|
+
class UpperCase < Virtus::Attribute::Object
|
|
18
|
+
primitive String
|
|
19
|
+
coercion_method :to_upcase
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class RegularExpression < Virtus::Attribute::Object
|
|
23
|
+
primitive Regexp
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
class User
|
|
27
|
+
include Virtus
|
|
28
|
+
|
|
29
|
+
attribute :name, String
|
|
30
|
+
attribute :scream, UpperCase
|
|
31
|
+
attribute :expression, RegularExpression
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
subject { Examples::User.new }
|
|
37
|
+
|
|
38
|
+
specify 'allows you to define custom attributes' do
|
|
39
|
+
regexp = /awesome/
|
|
40
|
+
subject.expression = regexp
|
|
41
|
+
subject.expression.should == regexp
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
specify 'allows you to define coercion methods' do
|
|
45
|
+
subject.scream = 'welcome'
|
|
46
|
+
subject.scream.should == 'WELCOME'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "default values" do
|
|
4
|
+
|
|
5
|
+
before do
|
|
6
|
+
module Examples
|
|
7
|
+
class Page
|
|
8
|
+
include Virtus
|
|
9
|
+
|
|
10
|
+
attribute :title, String
|
|
11
|
+
attribute :slug, String, :default => lambda { |post, attribute| post.title.downcase.gsub(' ', '-') }
|
|
12
|
+
attribute :view_count, Integer, :default => 0
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
subject { Examples::Page.new }
|
|
18
|
+
|
|
19
|
+
specify 'without a default the value is nil' do
|
|
20
|
+
subject.title.should be_nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
specify 'can be supplied with the :default option' do
|
|
24
|
+
subject.view_count.should == 0
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
specify "you can pass a 'callable-object' to the :default option" do
|
|
28
|
+
subject.title = 'Example Blog Post'
|
|
29
|
+
subject.slug.should == 'example-blog-post'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe "virtus attribute definitions" do
|
|
4
|
+
|
|
5
|
+
before do
|
|
6
|
+
module Examples
|
|
7
|
+
class Person
|
|
8
|
+
include Virtus
|
|
9
|
+
|
|
10
|
+
attribute :name, String
|
|
11
|
+
attribute :age, Integer
|
|
12
|
+
attribute :doctor, Boolean
|
|
13
|
+
attribute :salery, Decimal
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class Manager < Person
|
|
17
|
+
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
subject { Examples::Person.new }
|
|
23
|
+
|
|
24
|
+
specify 'virtus creates accessor methods' do
|
|
25
|
+
subject.name = 'Peter'
|
|
26
|
+
subject.name.should == 'Peter'
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
specify 'the constructor accepts a hash for mass-assignment' do
|
|
30
|
+
john = Examples::Person.new(:name => 'John', :age => 13)
|
|
31
|
+
john.name.should == 'John'
|
|
32
|
+
john.age.should == 13
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
specify 'Boolean attributes have a getter with questionmark' do
|
|
36
|
+
subject.doctor?.should be_false
|
|
37
|
+
subject.doctor = true
|
|
38
|
+
subject.doctor?.should be_true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
context 'with attributes' do
|
|
42
|
+
let(:attributes) { {:name => 'Jane', :age => 45, :doctor => true, :salery => 4500} }
|
|
43
|
+
subject { Examples::Person.new(attributes) }
|
|
44
|
+
|
|
45
|
+
specify "#attributes returns the object's attributes as a hash" do
|
|
46
|
+
subject.attributes.should == attributes
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
specify "#to_hash returns the object's attributes as a hash" do
|
|
50
|
+
subject.to_hash.should == attributes
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
context 'inheritance' do
|
|
55
|
+
specify 'inherits all the attribtues from the base class' do
|
|
56
|
+
fred = Examples::Manager.new(:name => 'Fred', :age => 29)
|
|
57
|
+
fred.name.should == 'Fred'
|
|
58
|
+
fred.age.should == 29
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
specify 'lets you add attributes to the base class at runtime' do
|
|
62
|
+
frank = Examples::Manager.new(:name => 'Frank')
|
|
63
|
+
Examples::Person.attribute :just_added, String
|
|
64
|
+
frank.just_added = 'it works!'
|
|
65
|
+
frank.just_added.should == 'it works!'
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
specify 'lets you add attributes to the subclass at runtime' do
|
|
69
|
+
person_jack = Examples::Person.new(:name => 'Jack')
|
|
70
|
+
manager_frank = Examples::Manager.new(:name => 'Frank')
|
|
71
|
+
Examples::Manager.attribute :just_added, String
|
|
72
|
+
|
|
73
|
+
manager_frank.just_added = 'awesome!'
|
|
74
|
+
manager_frank.just_added.should == 'awesome!'
|
|
75
|
+
person_jack.should_not respond_to(:just_added)
|
|
76
|
+
person_jack.should_not respond_to(:just_added=)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'embedded values' do
|
|
4
|
+
before do
|
|
5
|
+
module Examples
|
|
6
|
+
class City
|
|
7
|
+
include Virtus
|
|
8
|
+
|
|
9
|
+
attribute :name, String
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class Address
|
|
13
|
+
include Virtus
|
|
14
|
+
|
|
15
|
+
attribute :street, String
|
|
16
|
+
attribute :zipcode, String
|
|
17
|
+
attribute :city, City
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
class User
|
|
21
|
+
include Virtus
|
|
22
|
+
|
|
23
|
+
attribute :name, String
|
|
24
|
+
attribute :address, Address
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
subject { Examples::User.new(:name => 'the guy',
|
|
30
|
+
:address => address_attributes) }
|
|
31
|
+
let(:address_attributes) do
|
|
32
|
+
{ :street => 'Street 1/2', :zipcode => '12345', :city => { :name => 'NYC' } }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
specify '#attributes returns instances of the embedded values' do
|
|
36
|
+
subject.attributes.should == {
|
|
37
|
+
:name => 'the guy',
|
|
38
|
+
:address => subject.address
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
specify 'allows you to pass a hash for the embedded value' do
|
|
43
|
+
user = Examples::User.new
|
|
44
|
+
user.address = address_attributes
|
|
45
|
+
user.address.street.should == 'Street 1/2'
|
|
46
|
+
user.address.zipcode.should == '12345'
|
|
47
|
+
user.address.city.name.should == 'NYC'
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'overriding virtus behavior' do
|
|
4
|
+
|
|
5
|
+
before do
|
|
6
|
+
module Examples
|
|
7
|
+
class Article
|
|
8
|
+
include Virtus
|
|
9
|
+
|
|
10
|
+
attribute :title, String
|
|
11
|
+
|
|
12
|
+
def title
|
|
13
|
+
super || '<unknown>'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def title=(name)
|
|
17
|
+
super unless self.title == "can't be changed"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
describe 'overriding an attribute getter' do
|
|
24
|
+
specify 'calls the defined getter' do
|
|
25
|
+
Examples::Article.new.title.should == '<unknown>'
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
specify 'super can be used to access the getter defined by virtus' do
|
|
29
|
+
Examples::Article.new(:title => 'example article').title.should == 'example article'
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
describe 'overriding an attribute setter' do
|
|
34
|
+
specify 'calls the defined setter' do
|
|
35
|
+
article = Examples::Article.new(:title => "can't be changed")
|
|
36
|
+
article.title = 'this will never be assigned'
|
|
37
|
+
article.title.should == "can't be changed"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
specify 'super can be used to access the setter defined by virtus' do
|
|
41
|
+
article = Examples::Article.new(:title => 'example article')
|
|
42
|
+
article.title = 'my new title'
|
|
43
|
+
article.title.should == 'my new title'
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Virtus, 'instance level attributes' do
|
|
4
|
+
subject do
|
|
5
|
+
subject = Object.new
|
|
6
|
+
subject.singleton_class.send(:include, Virtus)
|
|
7
|
+
subject
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
let(:attribute) { subject.singleton_class.attribute(:name, String) }
|
|
11
|
+
|
|
12
|
+
before do
|
|
13
|
+
pending if RUBY_VERSION < '1.9'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
context 'adding an attribute' do
|
|
17
|
+
it 'allows setting the attribute value on the instance' do
|
|
18
|
+
attribute
|
|
19
|
+
subject.name = 'foo'
|
|
20
|
+
subject.name.should eql('foo')
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
data/spec/rcov.opts
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
shared_examples_for 'an options class method' do
|
|
2
|
+
context 'with no argument' do
|
|
3
|
+
subject { object.send(method) }
|
|
4
|
+
|
|
5
|
+
it { should be(default) }
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
context 'with a default value' do
|
|
9
|
+
subject { object.send(method, value) }
|
|
10
|
+
|
|
11
|
+
let(:value) { stub('value') }
|
|
12
|
+
|
|
13
|
+
it { should equal(object) }
|
|
14
|
+
|
|
15
|
+
it 'sets the default value for the class method' do
|
|
16
|
+
expect { subject }.to change { object.send(method) }.from(nil).to(value)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -1,27 +1,40 @@
|
|
|
1
1
|
require 'backports'
|
|
2
|
-
require 'pathname'
|
|
3
2
|
require 'rubygems'
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
begin
|
|
5
|
+
require 'rspec' # try for RSpec 2
|
|
6
|
+
rescue LoadError
|
|
7
|
+
require 'spec' # try for RSpec 1
|
|
8
|
+
RSpec = Spec::Runner
|
|
9
|
+
end
|
|
5
10
|
|
|
6
11
|
require 'virtus'
|
|
7
12
|
|
|
8
13
|
ENV['TZ'] = 'UTC'
|
|
9
14
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
Pathname.glob((SPEC_ROOT + '**/shared/**/*.rb').to_s).each { |file| require file }
|
|
15
|
+
# require spec support files and shared behavior
|
|
16
|
+
Dir[File.expand_path('../**/shared/**/*.rb', __FILE__)].each { |file| require file }
|
|
13
17
|
|
|
14
18
|
RSpec.configure do |config|
|
|
15
19
|
|
|
16
|
-
# Remove anonymous Attribute classes from Attribute descendants
|
|
20
|
+
# Remove anonymous- and example- Attribute classes from Attribute descendants
|
|
17
21
|
config.after :all do
|
|
18
22
|
stack = [ Virtus::Attribute ]
|
|
19
23
|
while klass = stack.pop
|
|
20
24
|
klass.descendants.delete_if do |descendant|
|
|
21
|
-
descendant.name.nil? || descendant.name.empty?
|
|
25
|
+
descendant.name.nil? || descendant.name.empty? || descendant.name.start_with?('Examples::')
|
|
22
26
|
end
|
|
23
27
|
stack.concat(klass.descendants)
|
|
24
28
|
end
|
|
25
29
|
end
|
|
26
30
|
|
|
31
|
+
# Remove constants in the Example-Module
|
|
32
|
+
config.after :each do
|
|
33
|
+
if defined?(Examples)
|
|
34
|
+
Examples.constants.each do |const_name|
|
|
35
|
+
ConstantsHelpers.undef_constant(Examples, const_name)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
27
40
|
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Virtus::Attribute::Array, '#coerce' do
|
|
4
|
+
subject { attribute.coerce(value) }
|
|
5
|
+
|
|
6
|
+
let(:attribute) { described_class.new(:colors) }
|
|
7
|
+
|
|
8
|
+
context 'with a Hash' do
|
|
9
|
+
let(:value) { { :foo => 'bar' } }
|
|
10
|
+
|
|
11
|
+
it { should eql([ [ :foo, 'bar' ] ]) }
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Virtus::Attribute::Boolean, '#coerce' do
|
|
4
|
+
subject { attribute.coerce(value) }
|
|
5
|
+
|
|
6
|
+
let(:attribute) { described_class.new(:is_admin) }
|
|
7
|
+
|
|
8
|
+
context 'with 1' do
|
|
9
|
+
let(:value) { 1 }
|
|
10
|
+
|
|
11
|
+
it { should be(true) }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context 'with "1"' do
|
|
15
|
+
let(:value) { '1' }
|
|
16
|
+
|
|
17
|
+
it { should be(true) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
context 'with "true"' do
|
|
21
|
+
let(:value) { 'true' }
|
|
22
|
+
|
|
23
|
+
it { should be(true) }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context 'with "TRUE"' do
|
|
27
|
+
let(:value) { 'TRUE' }
|
|
28
|
+
|
|
29
|
+
it { should be(true) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context 'with "t"' do
|
|
33
|
+
let(:value) { 't' }
|
|
34
|
+
|
|
35
|
+
it { should be(true) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
context 'with "T"' do
|
|
39
|
+
let(:value) { 'T' }
|
|
40
|
+
|
|
41
|
+
it { should be(true) }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context 'with 0' do
|
|
45
|
+
let(:value) { 0 }
|
|
46
|
+
|
|
47
|
+
it { should be(false) }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context 'with "0"' do
|
|
51
|
+
let(:value) { '0' }
|
|
52
|
+
|
|
53
|
+
it { should be(false) }
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
context 'with "false"' do
|
|
57
|
+
let(:value) { 'false' }
|
|
58
|
+
|
|
59
|
+
it { should be(false) }
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context 'with "FALSE"' do
|
|
63
|
+
let(:value) { 'FALSE' }
|
|
64
|
+
|
|
65
|
+
it { should be(false) }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
context 'with "f"' do
|
|
69
|
+
let(:value) { 'f' }
|
|
70
|
+
|
|
71
|
+
it { should be(false) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
context 'with "F"' do
|
|
75
|
+
let(:value) { 'F' }
|
|
76
|
+
|
|
77
|
+
it { should be(false) }
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
context 'with "Foo"' do
|
|
81
|
+
let(:value) { 'Foo' }
|
|
82
|
+
|
|
83
|
+
it { should equal(value) }
|
|
84
|
+
end
|
|
85
|
+
end
|