virtus 0.0.10 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- 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,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#name' do
|
4
|
+
subject { object.name }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
|
11
|
+
it { should be(:name) }
|
12
|
+
end
|
@@ -4,15 +4,17 @@ describe Virtus::Attribute::Object, '.descendants' do
|
|
4
4
|
subject { described_class.descendants }
|
5
5
|
|
6
6
|
let(:known_descendants) do
|
7
|
-
[ Virtus::Attribute::
|
8
|
-
Virtus::Attribute::
|
9
|
-
Virtus::Attribute::
|
10
|
-
Virtus::Attribute::
|
11
|
-
Virtus::Attribute::
|
12
|
-
Virtus::Attribute::
|
7
|
+
[ Virtus::Attribute::EmbeddedValue,
|
8
|
+
Virtus::Attribute::Time, Virtus::Attribute::String,
|
9
|
+
Virtus::Attribute::Integer, Virtus::Attribute::Hash,
|
10
|
+
Virtus::Attribute::Float, Virtus::Attribute::Decimal,
|
11
|
+
Virtus::Attribute::Numeric, Virtus::Attribute::DateTime,
|
12
|
+
Virtus::Attribute::Date, Virtus::Attribute::Boolean,
|
13
|
+
Virtus::Attribute::Set, Virtus::Attribute::Array,
|
14
|
+
Virtus::Attribute::Collection, Virtus::Attribute::Class ]
|
13
15
|
end
|
14
16
|
|
15
|
-
it
|
17
|
+
it 'should return all known attribute classes' do
|
16
18
|
subject.should eql(known_descendants)
|
17
19
|
end
|
18
20
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#options' do
|
4
|
+
subject { object.options }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
|
11
|
+
it { should be_instance_of(Hash) }
|
12
|
+
|
13
|
+
it { should eql(options.merge(:accessor => :public)) }
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#public_reader?' do
|
4
|
+
subject { object.public_reader? }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
|
11
|
+
context 'when :reader is not not specified' do
|
12
|
+
it { should be(true) }
|
13
|
+
end
|
14
|
+
|
15
|
+
{ :public => true, :protected => false, :private => false }.each do |value, expected|
|
16
|
+
context "when :reader is #{value.inspect}" do
|
17
|
+
before do
|
18
|
+
options.update(:reader => value)
|
19
|
+
end
|
20
|
+
|
21
|
+
it { should be(expected) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#public_writer?' do
|
4
|
+
subject { object.public_writer? }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
|
11
|
+
context 'when :writer is not not specified' do
|
12
|
+
it { should be(true) }
|
13
|
+
end
|
14
|
+
|
15
|
+
{ :public => true, :protected => false, :private => false }.each do |value, expected|
|
16
|
+
context "when :writer is #{value.inspect}" do
|
17
|
+
before do
|
18
|
+
options.update(:writer => value)
|
19
|
+
end
|
20
|
+
|
21
|
+
it { should be(expected) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#reader_visibility' do
|
4
|
+
subject { object.reader_visibility }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
|
11
|
+
context 'when not specified' do
|
12
|
+
it { should be(:public) }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when specified' do
|
16
|
+
let(:reader) { stub('reader') }
|
17
|
+
|
18
|
+
before do
|
19
|
+
options.update(:reader => reader)
|
20
|
+
end
|
21
|
+
|
22
|
+
it { should be(reader) }
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute::Set, '#coerce' do
|
4
|
+
subject { attribute.coerce(value) }
|
5
|
+
|
6
|
+
let(:attribute) { described_class.new(:colors) }
|
7
|
+
|
8
|
+
context 'with an Array' do
|
9
|
+
let(:value) { [ :foo, 'bar', 'bar', :foo ] }
|
10
|
+
|
11
|
+
it { should eql(Set[:foo, 'bar']) }
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#set' do
|
4
|
+
subject { object.set(instance, value) }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
let(:instance) { Object.new }
|
11
|
+
let(:value) { stub('value') }
|
12
|
+
let(:coerced) { stub('coerced') }
|
13
|
+
|
14
|
+
before do
|
15
|
+
object.stub(:coerce).with(value).and_return(coerced)
|
16
|
+
object.stub(:set!).with(instance, coerced).and_return(object)
|
17
|
+
end
|
18
|
+
|
19
|
+
it { should be(object) }
|
20
|
+
|
21
|
+
it 'coerces the value' do
|
22
|
+
object.should_receive(:coerce).with(value)
|
23
|
+
subject
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'sets the instance variable to the coerced value' do
|
27
|
+
object.should_receive(:set!).with(instance, coerced)
|
28
|
+
subject
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Virtus::Attribute, '#set!' do
|
33
|
+
subject { object.set!(instance, value) }
|
34
|
+
|
35
|
+
let(:object) { described_class.new(:name, options) }
|
36
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
37
|
+
let(:primitive) { stub('primitive') }
|
38
|
+
let(:coercion_method) { stub('coercion_method') }
|
39
|
+
let(:instance) { Object.new }
|
40
|
+
let(:value) { stub('value') }
|
41
|
+
|
42
|
+
it { should be(object) }
|
43
|
+
|
44
|
+
it 'sets the instance variable to the value' do
|
45
|
+
instance.instance_variable_get(:@name).should be_nil
|
46
|
+
subject
|
47
|
+
instance.instance_variable_get(:@name).should be(value)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples_for 'a correct time' do
|
4
|
+
it { should be_kind_of(Time) }
|
5
|
+
its(:year) { should eql(year) }
|
6
|
+
its(:month) { should eql(month) }
|
7
|
+
its(:day) { should eql(day) }
|
8
|
+
its(:hour) { should eql(hour) }
|
9
|
+
its(:min) { should eql(min) }
|
10
|
+
its(:sec) { should eql(sec) }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Virtus::Attribute::Time, '#coerce' do
|
14
|
+
subject { attribute.coerce(value) }
|
15
|
+
|
16
|
+
let(:attribute) { described_class.new(:bday) }
|
17
|
+
let(:year) { 1983 }
|
18
|
+
let(:month) { 11 }
|
19
|
+
let(:day) { 18 }
|
20
|
+
let(:hour) { 8 }
|
21
|
+
let(:min) { 16 }
|
22
|
+
let(:sec) { 32 }
|
23
|
+
|
24
|
+
context 'with a date' do
|
25
|
+
let(:hour) { 0 }
|
26
|
+
let(:min) { 0 }
|
27
|
+
let(:sec) { 0 }
|
28
|
+
let(:value) { DateTime.new(year, month, day, hour, min, sec) }
|
29
|
+
|
30
|
+
it_should_behave_like 'a correct time'
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with a date time' do
|
34
|
+
let(:value) { DateTime.new(year, month, day, hour, min, sec) }
|
35
|
+
|
36
|
+
it_should_behave_like 'a correct time'
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'with a hash' do
|
40
|
+
let(:value) { { :year => year, :month => month, :day => day, :hour => hour, :min => min, :sec => sec } }
|
41
|
+
|
42
|
+
it_should_behave_like 'a correct time'
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'with a string' do
|
46
|
+
context 'without hour, min and sec' do
|
47
|
+
let(:hour) { 0 }
|
48
|
+
let(:min) { 0 }
|
49
|
+
let(:sec) { 0 }
|
50
|
+
let(:value) { "November #{day}th, #{year}" }
|
51
|
+
|
52
|
+
it_should_behave_like 'a correct time'
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'with hour, min and sec' do
|
56
|
+
let(:value) { "November #{day}th, #{year}, #{hour}:#{min}:#{sec}" }
|
57
|
+
|
58
|
+
it_should_behave_like 'a correct time'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with a non-date value' do
|
63
|
+
let(:value) { '2999' }
|
64
|
+
|
65
|
+
it { should equal(value) }
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#value_coerced?' do
|
4
|
+
subject { object.value_coerced?(value) }
|
5
|
+
|
6
|
+
let(:object) { described_class::String.new(:name) }
|
7
|
+
|
8
|
+
context 'when the value matches the primitive' do
|
9
|
+
let(:value) { 'string' }
|
10
|
+
|
11
|
+
it { should be(true) }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when the value does not match the primitive' do
|
15
|
+
let(:value) { 1 }
|
16
|
+
|
17
|
+
it { should be(false) }
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#writer_visibility' do
|
4
|
+
subject { object.writer_visibility }
|
5
|
+
|
6
|
+
let(:object) { described_class.new(:name, options) }
|
7
|
+
let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
|
8
|
+
let(:primitive) { stub('primitive') }
|
9
|
+
let(:coercion_method) { stub('coercion_method') }
|
10
|
+
|
11
|
+
context 'when not specified' do
|
12
|
+
it { should be(:public) }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when specified' do
|
16
|
+
let(:writer) { stub('writer') }
|
17
|
+
|
18
|
+
before do
|
19
|
+
options.update(:writer => writer)
|
20
|
+
end
|
21
|
+
|
22
|
+
it { should be(writer) }
|
23
|
+
end
|
24
|
+
end
|
@@ -18,6 +18,18 @@ describe Virtus::AttributeSet, '#<<' do
|
|
18
18
|
from(attributes).
|
19
19
|
to([ attribute ])
|
20
20
|
end
|
21
|
+
|
22
|
+
it 'indexes the new attribute under its #name property' do
|
23
|
+
expect { subject }.to change { object[name] }.
|
24
|
+
from(nil).
|
25
|
+
to(attribute)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'indexes the new attribute under the string version of its #name property' do
|
29
|
+
expect { subject }.to change { object[name.to_s] }.
|
30
|
+
from(nil).
|
31
|
+
to(attribute)
|
32
|
+
end
|
21
33
|
end
|
22
34
|
|
23
35
|
context 'with a duplicate attribute' do
|
@@ -10,4 +10,8 @@ describe Virtus::AttributeSet, '#[]' do
|
|
10
10
|
let(:object) { described_class.new(parent, attributes) }
|
11
11
|
|
12
12
|
it { should equal(attribute) }
|
13
|
+
|
14
|
+
it 'allows indexed access to attributes by the string representation of their name' do
|
15
|
+
object[name.to_s].should equal(attribute)
|
16
|
+
end
|
13
17
|
end
|
@@ -9,27 +9,47 @@ describe Virtus::AttributeSet, '#[]=' do
|
|
9
9
|
let(:name) { :name }
|
10
10
|
|
11
11
|
context 'with a new attribute' do
|
12
|
-
let(:attribute) { mock('
|
12
|
+
let(:attribute) { mock('attribute', :name => name) }
|
13
13
|
|
14
14
|
it { should equal(attribute) }
|
15
15
|
|
16
16
|
it 'adds an attribute' do
|
17
|
-
expect { subject }.to change { object.to_a }.
|
18
|
-
|
19
|
-
|
17
|
+
expect { subject }.to change { object.to_a }.from(attributes).to([ attribute ])
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'allows #[] to access the attribute with a symbol' do
|
21
|
+
expect { subject }.to change { object['name'] }.from(nil).to(attribute)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'allows #[] to access the attribute with a string' do
|
25
|
+
expect { subject }.to change { object[:name] }.from(nil).to(attribute)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'allows #reset to track overridden attributes' do
|
29
|
+
expect { subject }.to change { object.reset.to_a }.from(attributes).to([ attribute ])
|
20
30
|
end
|
21
31
|
end
|
22
32
|
|
23
33
|
context 'with a duplicate attribute' do
|
24
|
-
let(:attributes) { [ mock('
|
25
|
-
let(:attribute) { mock('
|
34
|
+
let(:attributes) { [ mock('original', :name => name) ] }
|
35
|
+
let(:attribute) { mock('attribute', :name => name) }
|
26
36
|
|
27
37
|
it { should equal(attribute) }
|
28
38
|
|
29
39
|
it 'replaces the original attribute' do
|
30
|
-
expect { subject }.to change { object.to_a }.
|
31
|
-
|
32
|
-
|
40
|
+
expect { subject }.to change { object.to_a }.from(attributes).to([ attribute ])
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'allows #[] to access the attribute with a symbol' do
|
44
|
+
expect { subject }.to change { object['name'] }.from(nil).to(attribute)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'allows #[] to access the attribute with a string' do
|
48
|
+
expect { subject }.to change { object[:name] }.from(nil).to(attribute)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'allows #reset to track overridden attributes' do
|
52
|
+
expect { subject }.to change { object.reset.to_a }.from(attributes).to([ attribute ])
|
33
53
|
end
|
34
54
|
end
|
35
55
|
end
|