virtus 0.5.5 → 1.0.0.beta3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -1
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -8
- data/Gemfile +8 -2
- data/Gemfile.devtools +24 -30
- data/Guardfile +17 -6
- data/README.md +215 -77
- data/TODO.md +6 -0
- data/config/flay.yml +2 -2
- data/config/flog.yml +1 -1
- data/config/mutant.yml +12 -1
- data/config/reek.yml +57 -66
- data/lib/virtus.rb +126 -42
- data/lib/virtus/attribute.rb +52 -240
- data/lib/virtus/attribute/accessor.rb +91 -0
- data/lib/virtus/attribute/boolean.rb +11 -8
- data/lib/virtus/attribute/builder.rb +120 -0
- data/lib/virtus/attribute/coercer.rb +51 -0
- data/lib/virtus/attribute/coercible.rb +14 -0
- data/lib/virtus/attribute/collection.rb +44 -90
- data/lib/virtus/attribute/default_value/from_callable.rb +1 -1
- data/lib/virtus/attribute/default_value/from_clonable.rb +1 -6
- data/lib/virtus/attribute/default_value/from_symbol.rb +3 -3
- data/lib/virtus/attribute/embedded_value.rb +37 -28
- data/lib/virtus/attribute/hash.rb +69 -71
- data/lib/virtus/attribute/lazy_default.rb +18 -0
- data/lib/virtus/attribute/strict.rb +16 -0
- data/lib/virtus/attribute_set.rb +81 -9
- data/lib/virtus/class_inclusions.rb +24 -17
- data/lib/virtus/class_methods.rb +13 -31
- data/lib/virtus/configuration.rb +77 -0
- data/lib/virtus/const_missing_extensions.rb +16 -0
- data/lib/virtus/extensions.rb +86 -61
- data/lib/virtus/instance_methods.rb +107 -128
- data/lib/virtus/model.rb +68 -0
- data/lib/virtus/module_builder.rb +184 -0
- data/lib/virtus/module_extensions.rb +27 -15
- data/lib/virtus/support/equalizer.rb +2 -3
- data/lib/virtus/support/type_lookup.rb +1 -1
- data/lib/virtus/value_object.rb +24 -31
- data/lib/virtus/version.rb +1 -1
- data/spec/integration/building_module_spec.rb +68 -0
- data/spec/integration/custom_attributes_spec.rb +8 -15
- data/spec/integration/custom_collection_attributes_spec.rb +101 -0
- data/spec/integration/default_values_spec.rb +9 -2
- data/spec/integration/defining_attributes_spec.rb +2 -2
- data/spec/integration/using_modules_spec.rb +5 -3
- data/spec/shared/freeze_method_behavior.rb +2 -2
- data/spec/shared/options_class_method.rb +1 -1
- data/spec/spec_helper.rb +25 -5
- data/spec/unit/virtus/attribute/boolean/value_coerced_predicate_spec.rb +25 -0
- data/spec/unit/virtus/attribute/class_methods/build_spec.rb +115 -22
- data/spec/unit/virtus/attribute/class_methods/coerce_spec.rb +32 -0
- data/spec/unit/virtus/attribute/coerce_spec.rb +38 -21
- data/spec/unit/virtus/attribute/coercible_predicate_spec.rb +20 -0
- data/spec/unit/virtus/attribute/collection/class_methods/build_spec.rb +84 -0
- data/spec/unit/virtus/attribute/collection/coerce_spec.rb +37 -24
- data/spec/unit/virtus/attribute/custom_collection_spec.rb +23 -0
- data/spec/unit/virtus/attribute/embedded_value/class_methods/build_spec.rb +37 -0
- data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +46 -10
- data/spec/unit/virtus/attribute/get_spec.rb +15 -63
- data/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb +80 -0
- data/spec/unit/virtus/attribute/hash/coerce_spec.rb +75 -13
- data/spec/unit/virtus/attribute/lazy_predicate_spec.rb +20 -0
- data/spec/unit/virtus/attribute/set_default_value_spec.rb +74 -0
- data/spec/unit/virtus/attribute/set_spec.rb +16 -36
- data/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb +19 -0
- data/spec/unit/virtus/attribute_set/append_spec.rb +3 -5
- data/spec/unit/virtus/attribute_set/define_reader_method_spec.rb +1 -1
- data/spec/unit/virtus/attribute_set/define_writer_method_spec.rb +1 -1
- data/spec/unit/virtus/attribute_set/each_spec.rb +3 -3
- data/spec/unit/virtus/attribute_set/element_reference_spec.rb +1 -1
- data/spec/unit/virtus/attribute_set/element_set_spec.rb +4 -4
- data/spec/unit/virtus/attribute_set/merge_spec.rb +10 -16
- data/spec/unit/virtus/attribute_set/reset_spec.rb +6 -10
- data/spec/unit/virtus/attribute_spec.rb +222 -0
- data/spec/unit/virtus/attributes_reader_spec.rb +41 -0
- data/spec/unit/virtus/attributes_writer_spec.rb +51 -0
- data/spec/unit/virtus/class_methods/new_spec.rb +39 -0
- data/spec/unit/virtus/config_spec.rb +13 -0
- data/spec/unit/virtus/element_reader_spec.rb +21 -0
- data/spec/unit/virtus/element_writer_spec.rb +19 -0
- data/spec/unit/virtus/freeze_spec.rb +21 -0
- data/spec/unit/virtus/model_spec.rb +134 -0
- data/spec/unit/virtus/module_spec.rb +76 -0
- data/spec/unit/virtus/set_default_attributes_spec.rb +25 -0
- data/spec/unit/virtus/value_object_spec.rb +105 -0
- data/virtus.gemspec +4 -2
- metadata +82 -191
- data/.rvmrc +0 -1
- data/TODO +0 -1
- data/config/roodi.yml +0 -17
- data/lib/virtus/attribute/array.rb +0 -24
- data/lib/virtus/attribute/class.rb +0 -21
- data/lib/virtus/attribute/date.rb +0 -34
- data/lib/virtus/attribute/date_time.rb +0 -36
- data/lib/virtus/attribute/decimal.rb +0 -21
- data/lib/virtus/attribute/embedded_value/from_open_struct.rb +0 -19
- data/lib/virtus/attribute/embedded_value/from_struct.rb +0 -19
- data/lib/virtus/attribute/float.rb +0 -30
- data/lib/virtus/attribute/integer.rb +0 -27
- data/lib/virtus/attribute/numeric.rb +0 -11
- data/lib/virtus/attribute/object.rb +0 -11
- data/lib/virtus/attribute/set.rb +0 -24
- data/lib/virtus/attribute/string.rb +0 -24
- data/lib/virtus/attribute/symbol.rb +0 -21
- data/lib/virtus/attribute/time.rb +0 -36
- data/lib/virtus/coercion.rb +0 -32
- data/lib/virtus/coercion/array.rb +0 -23
- data/lib/virtus/coercion/date.rb +0 -26
- data/lib/virtus/coercion/date_time.rb +0 -26
- data/lib/virtus/coercion/decimal.rb +0 -40
- data/lib/virtus/coercion/false_class.rb +0 -24
- data/lib/virtus/coercion/float.rb +0 -39
- data/lib/virtus/coercion/hash.rb +0 -71
- data/lib/virtus/coercion/integer.rb +0 -83
- data/lib/virtus/coercion/numeric.rb +0 -66
- data/lib/virtus/coercion/object.rb +0 -125
- data/lib/virtus/coercion/string.rb +0 -218
- data/lib/virtus/coercion/symbol.rb +0 -24
- data/lib/virtus/coercion/time.rb +0 -40
- data/lib/virtus/coercion/time_coercions.rb +0 -86
- data/lib/virtus/coercion/true_class.rb +0 -24
- data/spec/unit/virtus/attribute/array/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/boolean/coerce_spec.rb +0 -85
- data/spec/unit/virtus/attribute/boolean/define_reader_method_spec.rb +0 -15
- data/spec/unit/virtus/attribute/boolean/value_coerced_spec.rb +0 -97
- data/spec/unit/virtus/attribute/boolean_spec.rb +0 -19
- data/spec/unit/virtus/attribute/class/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/class_methods/accessor_spec.rb +0 -12
- data/spec/unit/virtus/attribute/class_methods/coercion_method_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/default_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +0 -117
- data/spec/unit/virtus/attribute/class_methods/merge_options_spec.rb +0 -11
- data/spec/unit/virtus/attribute/class_methods/primitive_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/reader_spec.rb +0 -9
- data/spec/unit/virtus/attribute/class_methods/writer_spec.rb +0 -9
- data/spec/unit/virtus/attribute/coercion_method_spec.rb +0 -12
- data/spec/unit/virtus/attribute/collection/class_methods/merge_options_spec.rb +0 -40
- data/spec/unit/virtus/attribute/collection/coerce_and_append_member_spec.rb +0 -16
- data/spec/unit/virtus/attribute/collection/member_coercion/coerce_and_append_member_spec.rb +0 -18
- data/spec/unit/virtus/attribute/collection/member_type_spec.rb +0 -17
- data/spec/unit/virtus/attribute/collection/new_collection_spec.rb +0 -9
- data/spec/unit/virtus/attribute/date/coerce_spec.rb +0 -47
- data/spec/unit/virtus/attribute/date/value_coerced_spec.rb +0 -46
- data/spec/unit/virtus/attribute/date_time/coerce_spec.rb +0 -80
- data/spec/unit/virtus/attribute/decimal/coerce_spec.rb +0 -123
- data/spec/unit/virtus/attribute/default_spec.rb +0 -28
- data/spec/unit/virtus/attribute/default_value/call_spec.rb +0 -21
- data/spec/unit/virtus/attribute/default_value/class_methods/build_spec.rb +0 -23
- data/spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb +0 -21
- data/spec/unit/virtus/attribute/default_value/from_callable/call_spec.rb +0 -19
- data/spec/unit/virtus/attribute/default_value/from_callable/class_methods/handle_spec.rb +0 -17
- data/spec/unit/virtus/attribute/default_value/from_clonable/call_spec.rb +0 -20
- data/spec/unit/virtus/attribute/default_value/from_clonable/class_methods/handle_spec.rb +0 -19
- data/spec/unit/virtus/attribute/default_value/from_symbol/call_spec.rb +0 -17
- data/spec/unit/virtus/attribute/default_value/from_symbol/class_methods/handle_spec.rb +0 -17
- data/spec/unit/virtus/attribute/default_value/value_spec.rb +0 -10
- data/spec/unit/virtus/attribute/define_accessor_methods_spec.rb +0 -26
- data/spec/unit/virtus/attribute/define_reader_method_spec.rb +0 -24
- data/spec/unit/virtus/attribute/define_writer_method_spec.rb +0 -24
- data/spec/unit/virtus/attribute/embedded_value/class_methods/determine_type_spec.rb +0 -23
- data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +0 -17
- data/spec/unit/virtus/attribute/embedded_value/from_open_struct/coerce_spec.rb +0 -34
- data/spec/unit/virtus/attribute/embedded_value/from_struct/coerce_spec.rb +0 -34
- data/spec/unit/virtus/attribute/float/coerce_spec.rb +0 -123
- data/spec/unit/virtus/attribute/hash/class_methods/merge_options_spec.rb +0 -33
- data/spec/unit/virtus/attribute/hash/key_type_spec.rb +0 -10
- data/spec/unit/virtus/attribute/hash/value_type_spec.rb +0 -10
- data/spec/unit/virtus/attribute/inspect_spec.rb +0 -27
- data/spec/unit/virtus/attribute/integer/coerce_spec.rb +0 -117
- data/spec/unit/virtus/attribute/name_spec.rb +0 -12
- data/spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb +0 -15
- data/spec/unit/virtus/attribute/numeric/class_methods/max_spec.rb +0 -9
- data/spec/unit/virtus/attribute/numeric/class_methods/min_spec.rb +0 -9
- data/spec/unit/virtus/attribute/options_spec.rb +0 -14
- data/spec/unit/virtus/attribute/public_reader_spec.rb +0 -24
- data/spec/unit/virtus/attribute/public_writer_spec.rb +0 -24
- data/spec/unit/virtus/attribute/set/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/string/coerce_spec.rb +0 -11
- data/spec/unit/virtus/attribute/symbol/coerce_spec.rb +0 -13
- data/spec/unit/virtus/attribute/time/coerce_spec.rb +0 -67
- data/spec/unit/virtus/attribute/value_coerced_spec.rb +0 -19
- data/spec/unit/virtus/class_methods/attribute_set_spec.rb +0 -9
- data/spec/unit/virtus/class_methods/attribute_spec.rb +0 -30
- data/spec/unit/virtus/class_methods/attributes_spec.rb +0 -22
- data/spec/unit/virtus/class_methods/const_missing_spec.rb +0 -27
- data/spec/unit/virtus/class_methods/inherited_spec.rb +0 -21
- data/spec/unit/virtus/coercion/array/class_methods/to_set_spec.rb +0 -12
- data/spec/unit/virtus/coercion/class_methods/element_reference_spec.rb +0 -17
- data/spec/unit/virtus/coercion/class_methods/primitive_spec.rb +0 -13
- data/spec/unit/virtus/coercion/date/class_methods/to_date_spec.rb +0 -10
- data/spec/unit/virtus/coercion/date/class_methods/to_datetime_spec.rb +0 -30
- data/spec/unit/virtus/coercion/date/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/date/class_methods/to_time_spec.rb +0 -12
- data/spec/unit/virtus/coercion/date_time/class_methods/to_date_spec.rb +0 -30
- data/spec/unit/virtus/coercion/date_time/class_methods/to_datetime_spec.rb +0 -10
- data/spec/unit/virtus/coercion/date_time/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/date_time/class_methods/to_time_spec.rb +0 -30
- data/spec/unit/virtus/coercion/decimal/class_methods/to_decimal_spec.rb +0 -9
- data/spec/unit/virtus/coercion/decimal/class_methods/to_float_spec.rb +0 -12
- data/spec/unit/virtus/coercion/decimal/class_methods/to_integer_spec.rb +0 -12
- data/spec/unit/virtus/coercion/decimal/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/false_class/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/float/class_methods/to_decimal_spec.rb +0 -12
- data/spec/unit/virtus/coercion/float/class_methods/to_float_spec.rb +0 -9
- data/spec/unit/virtus/coercion/float/class_methods/to_integer_spec.rb +0 -12
- data/spec/unit/virtus/coercion/float/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +0 -38
- data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +0 -38
- data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +0 -38
- data/spec/unit/virtus/coercion/integer/class_methods/to_boolean_spec.rb +0 -25
- data/spec/unit/virtus/coercion/integer/class_methods/to_decimal_spec.rb +0 -12
- data/spec/unit/virtus/coercion/integer/class_methods/to_float_spec.rb +0 -12
- data/spec/unit/virtus/coercion/integer/class_methods/to_integer_spec.rb +0 -9
- data/spec/unit/virtus/coercion/integer/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/numeric/class_methods/to_decimal_spec.rb +0 -10
- data/spec/unit/virtus/coercion/numeric/class_methods/to_float_spec.rb +0 -10
- data/spec/unit/virtus/coercion/numeric/class_methods/to_integer_spec.rb +0 -10
- data/spec/unit/virtus/coercion/numeric/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +0 -33
- data/spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb +0 -51
- data/spec/unit/virtus/coercion/object/class_methods/to_hash_spec.rb +0 -22
- data/spec/unit/virtus/coercion/object/class_methods/to_integer_spec.rb +0 -22
- data/spec/unit/virtus/coercion/object/class_methods/to_string_spec.rb +0 -22
- data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +0 -29
- data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +0 -49
- data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +0 -23
- data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +0 -50
- data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +0 -47
- data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +0 -57
- data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +0 -68
- data/spec/unit/virtus/coercion/string/class_methods/to_symbol_spec.rb +0 -9
- data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +0 -50
- data/spec/unit/virtus/coercion/symbol/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/coercion/time/class_methods/to_integer_spec.rb +0 -10
- data/spec/unit/virtus/coercion/time/class_methods/to_time_spec.rb +0 -10
- data/spec/unit/virtus/coercion/time_coercions/to_date_spec.rb +0 -33
- data/spec/unit/virtus/coercion/time_coercions/to_datetime_spec.rb +0 -33
- data/spec/unit/virtus/coercion/time_coercions/to_string_spec.rb +0 -18
- data/spec/unit/virtus/coercion/time_coercions/to_time_spec.rb +0 -33
- data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +0 -12
- data/spec/unit/virtus/equalizer/append_spec.rb +0 -82
- data/spec/unit/virtus/equalizer/class_method/new_spec.rb +0 -111
- data/spec/unit/virtus/equalizer/methods/eql_spec.rb +0 -47
- data/spec/unit/virtus/equalizer/methods/equal_value_spec.rb +0 -57
- data/spec/unit/virtus/extensions/allowed_writer_methods_spec.rb +0 -25
- data/spec/unit/virtus/extensions/attribute_spec.rb +0 -26
- data/spec/unit/virtus/instance_methods/attributes_spec.rb +0 -140
- data/spec/unit/virtus/instance_methods/element_reference_spec.rb +0 -24
- data/spec/unit/virtus/instance_methods/element_set_spec.rb +0 -28
- data/spec/unit/virtus/instance_methods/freeze_spec.rb +0 -64
- data/spec/unit/virtus/instance_methods/initialize_spec.rb +0 -48
- data/spec/unit/virtus/instance_methods/to_hash_spec.rb +0 -23
- data/spec/unit/virtus/module_extensions/attribute_spec.rb +0 -31
- data/spec/unit/virtus/options/accept_options_spec.rb +0 -37
- data/spec/unit/virtus/options/accepted_options_spec.rb +0 -21
- data/spec/unit/virtus/options/options_spec.rb +0 -34
- data/spec/unit/virtus/type_lookup/class_methods/extended_spec.rb +0 -9
- data/spec/unit/virtus/type_lookup/determine_type_spec.rb +0 -81
- data/spec/unit/virtus/type_lookup/primitive_spec.rb +0 -9
- data/spec/unit/virtus/value_object/class_methods/allowed_writer_methods_spec.rb +0 -15
- data/spec/unit/virtus/value_object/class_methods/attribute_spec.rb +0 -50
- data/spec/unit/virtus/value_object/class_methods/equalizer_spec.rb +0 -24
- data/spec/unit/virtus/value_object/initialize_spec.rb +0 -19
- data/spec/unit/virtus/value_object/instance_methods/clone_spec.rb +0 -21
- data/spec/unit/virtus/value_object/instance_methods/with_spec.rb +0 -31
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::DefaultValue::FromClonable, '#call' do
|
4
|
-
subject { object.call(instance) }
|
5
|
-
|
6
|
-
let(:object) { described_class.new(value) }
|
7
|
-
let(:value) { mock('value') }
|
8
|
-
let(:instance) { mock('instance') }
|
9
|
-
let(:response) { mock('response') }
|
10
|
-
let(:clone) { mock('clone') }
|
11
|
-
|
12
|
-
before { value.stub(:clone => clone) }
|
13
|
-
|
14
|
-
it { should be(clone) }
|
15
|
-
|
16
|
-
it 'clones the value' do
|
17
|
-
value.should_receive(:clone).with(no_args)
|
18
|
-
subject
|
19
|
-
end
|
20
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::DefaultValue::FromClonable, '.handle?' do
|
4
|
-
subject { described_class.handle?(default) }
|
5
|
-
|
6
|
-
let(:attribute) { mock('attribute') }
|
7
|
-
|
8
|
-
context 'with a clonable' do
|
9
|
-
let(:default) { 'clonable' }
|
10
|
-
|
11
|
-
it { should be(true) }
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'with a non-clonable' do
|
15
|
-
let(:default) { 1 }
|
16
|
-
|
17
|
-
it { should be(false) }
|
18
|
-
end
|
19
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::DefaultValue::FromSymbol, '#call' do
|
4
|
-
subject { object.call(instance) }
|
5
|
-
|
6
|
-
let(:object) { described_class.new(value) }
|
7
|
-
let(:instance) { mock('instance', value => retval) }
|
8
|
-
let(:value) { :set_default }
|
9
|
-
let(:retval) { mock('retval') }
|
10
|
-
|
11
|
-
it { should be(retval) }
|
12
|
-
|
13
|
-
it 'calls the method' do
|
14
|
-
instance.should_receive(value).with(no_args)
|
15
|
-
subject
|
16
|
-
end
|
17
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::DefaultValue::FromSymbol, '.handle?' do
|
4
|
-
subject { described_class.handle?(default) }
|
5
|
-
|
6
|
-
context 'with a symbol' do
|
7
|
-
let(:default) { :method_name }
|
8
|
-
|
9
|
-
it { should be(true) }
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'with a non-symbol' do
|
13
|
-
let(:default) { 'method_name' }
|
14
|
-
|
15
|
-
it { should be(false) }
|
16
|
-
end
|
17
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute, '#define_accessor_methods' do
|
4
|
-
subject { object.define_accessor_methods(mod) }
|
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(:mod) { stub('mod') }
|
11
|
-
|
12
|
-
before do
|
13
|
-
object.stub(:define_reader_method).with(mod)
|
14
|
-
object.stub(:define_writer_method).with(mod)
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'delegates to #define_reader_method' do
|
18
|
-
object.should_receive(:define_reader_method).with(mod)
|
19
|
-
subject
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'delegates to #define_writer_method' do
|
23
|
-
object.should_receive(:define_writer_method).with(mod)
|
24
|
-
subject
|
25
|
-
end
|
26
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute, '#define_reader_method' do
|
4
|
-
subject { object.define_reader_method(mod) }
|
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(:reader_visibility) { stub('reader_visibility') }
|
11
|
-
let(:mod) { mock('mod') }
|
12
|
-
|
13
|
-
before do
|
14
|
-
options.update(:reader => reader_visibility)
|
15
|
-
mod.stub(:define_reader_method).with(object, :name, reader_visibility)
|
16
|
-
end
|
17
|
-
|
18
|
-
it { should be(object) }
|
19
|
-
|
20
|
-
it 'calls #define_reader_method on the module' do
|
21
|
-
mod.should_receive(:define_reader_method).with(object, :name, reader_visibility)
|
22
|
-
subject
|
23
|
-
end
|
24
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute, '#define_writer_method' do
|
4
|
-
subject { object.define_writer_method(mod) }
|
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(:writer_visibility) { stub('writer_visibility') }
|
11
|
-
let(:mod) { mock('mod') }
|
12
|
-
|
13
|
-
before do
|
14
|
-
options.update(:writer => writer_visibility)
|
15
|
-
mod.stub(:define_writer_method).with(object, :name=, writer_visibility)
|
16
|
-
end
|
17
|
-
|
18
|
-
it { should be(object) }
|
19
|
-
|
20
|
-
it 'calls #define_writer_method on the module' do
|
21
|
-
mod.should_receive(:define_writer_method).with(object, :name=, writer_visibility)
|
22
|
-
subject
|
23
|
-
end
|
24
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::EmbeddedValue, '.determine_type' do
|
4
|
-
subject { described_class.determine_type(value) }
|
5
|
-
|
6
|
-
context "with Struct" do
|
7
|
-
let(:value) { Struct.new(:x) }
|
8
|
-
|
9
|
-
it { should be(Virtus::Attribute::EmbeddedValue::FromStruct) }
|
10
|
-
end
|
11
|
-
|
12
|
-
context "with OpenStruct" do
|
13
|
-
let(:value) { OpenStruct }
|
14
|
-
|
15
|
-
it { should be(Virtus::Attribute::EmbeddedValue::FromOpenStruct) }
|
16
|
-
end
|
17
|
-
|
18
|
-
context "with a Virtus descendant" do
|
19
|
-
let(:value) { Class.new { include Virtus; self } }
|
20
|
-
|
21
|
-
it { should be(Virtus::Attribute::EmbeddedValue::FromOpenStruct) }
|
22
|
-
end
|
23
|
-
end
|
@@ -1,17 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::EmbeddedValue, '.merge_options' do
|
4
|
-
subject { object.merge_options(type, options) }
|
5
|
-
|
6
|
-
let(:object) { described_class }
|
7
|
-
let(:type) { stub('type') }
|
8
|
-
let(:options) { {}.freeze }
|
9
|
-
|
10
|
-
it { should be_instance_of(Hash) }
|
11
|
-
|
12
|
-
it { should_not equal(options) }
|
13
|
-
|
14
|
-
it 'merges the type in as the model' do
|
15
|
-
should eql(:primitive => type)
|
16
|
-
end
|
17
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::EmbeddedValue::FromOpenStruct, '#coerce' do
|
4
|
-
subject { object.coerce(value) }
|
5
|
-
|
6
|
-
let(:primitive) { OpenStruct }
|
7
|
-
|
8
|
-
let(:object) do
|
9
|
-
described_class.new(:name, :primitive => primitive)
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'when the value is nil' do
|
13
|
-
let(:value) { nil }
|
14
|
-
|
15
|
-
it { should be_nil }
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'when the value is a primitive instance' do
|
19
|
-
let(:value) { primitive.new }
|
20
|
-
|
21
|
-
it { should be(value) }
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'when the value is a hash' do
|
25
|
-
let(:value) { Hash[:foo => 'bar'] }
|
26
|
-
let(:instance) { mock('instance') }
|
27
|
-
|
28
|
-
before do
|
29
|
-
primitive.should_receive(:new).with(value).and_return(instance)
|
30
|
-
end
|
31
|
-
|
32
|
-
it { should be(instance) }
|
33
|
-
end
|
34
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::EmbeddedValue::FromStruct, '#coerce' do
|
4
|
-
subject { object.coerce(value) }
|
5
|
-
|
6
|
-
let(:primitive) { Struct.new(:x, :y) }
|
7
|
-
|
8
|
-
let(:object) do
|
9
|
-
described_class.new(:name, :primitive => primitive)
|
10
|
-
end
|
11
|
-
|
12
|
-
context 'when the value is nil' do
|
13
|
-
let(:value) { nil }
|
14
|
-
|
15
|
-
it { should be_nil }
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'when the value is a primitive instance' do
|
19
|
-
let(:value) { primitive.new }
|
20
|
-
|
21
|
-
it { should be(value) }
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'when the value is an array' do
|
25
|
-
let(:value) { [ 1 ,2 ] }
|
26
|
-
let(:instance) { mock('instance') }
|
27
|
-
|
28
|
-
before do
|
29
|
-
primitive.should_receive(:new).with(*value).and_return(instance)
|
30
|
-
end
|
31
|
-
|
32
|
-
it { should be(instance) }
|
33
|
-
end
|
34
|
-
end
|
@@ -1,123 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::Float, '#coerce' do
|
4
|
-
subject { attribute.coerce(value) }
|
5
|
-
|
6
|
-
let(:attribute) { described_class.new(:score) }
|
7
|
-
|
8
|
-
context 'with a float' do
|
9
|
-
let(:value) { 24.0 }
|
10
|
-
|
11
|
-
it { should eql(value) }
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'with a zero string integer' do
|
15
|
-
let(:value) { '0' }
|
16
|
-
|
17
|
-
it { should eql(0.0) }
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with a positive string integer' do
|
21
|
-
let(:value) { '24' }
|
22
|
-
|
23
|
-
it { should eql(24.0) }
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with a negative string integer' do
|
27
|
-
let(:value) { '-24' }
|
28
|
-
|
29
|
-
it { should eql(-24.0) }
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'with a zero string float' do
|
33
|
-
let(:value) { '0.0' }
|
34
|
-
|
35
|
-
it { should eql(0.0) }
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'with a positive string float' do
|
39
|
-
let(:value) { '24.35' }
|
40
|
-
|
41
|
-
it { should eql(24.35) }
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with a negative string float' do
|
45
|
-
let(:value) { '-24.35' }
|
46
|
-
|
47
|
-
it { should eql(-24.35) }
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with a zero string float, with no leading digits' do
|
51
|
-
let(:value) { '.0' }
|
52
|
-
|
53
|
-
it { should eql(0.0) }
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'with a positive string float, with no leading digits' do
|
57
|
-
let(:value) { '.41' }
|
58
|
-
|
59
|
-
it { should eql(0.41) }
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'with a positive string float and leading non-significant digits' do
|
63
|
-
let (:value) { '00.0' }
|
64
|
-
|
65
|
-
it { should eql(0.0) }
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'with a zero integer' do
|
69
|
-
let(:value) { 0 }
|
70
|
-
|
71
|
-
it { should eql(0.0) }
|
72
|
-
end
|
73
|
-
|
74
|
-
context 'with a positive integer' do
|
75
|
-
let(:value) { 24 }
|
76
|
-
|
77
|
-
it { should eql(24.0) }
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'with a negative integer' do
|
81
|
-
let(:value) { -24 }
|
82
|
-
|
83
|
-
it { should eql(-24.0) }
|
84
|
-
end
|
85
|
-
|
86
|
-
context 'with a positive bignum' do
|
87
|
-
let(:value) { 1311935550 }
|
88
|
-
|
89
|
-
it { should eql(1311935550.0) }
|
90
|
-
end
|
91
|
-
|
92
|
-
context 'with a negative bignum' do
|
93
|
-
let(:value) { -1311935550 }
|
94
|
-
|
95
|
-
it { should eql(-1311935550.0) }
|
96
|
-
end
|
97
|
-
|
98
|
-
context 'with a zero decimal' do
|
99
|
-
let(:value) { BigDecimal('0.0') }
|
100
|
-
|
101
|
-
it { should eql(0.0) }
|
102
|
-
end
|
103
|
-
|
104
|
-
context 'with a positive decimal' do
|
105
|
-
let(:value) { BigDecimal('24.35') }
|
106
|
-
|
107
|
-
it { should eql(24.35) }
|
108
|
-
end
|
109
|
-
|
110
|
-
context 'with a negative decimal' do
|
111
|
-
let(:value) { BigDecimal('-24.35') }
|
112
|
-
|
113
|
-
it { should eql(-24.35) }
|
114
|
-
end
|
115
|
-
|
116
|
-
[ Object.new, true, '0.', '-.0', 'string' ].each do |non_num_value|
|
117
|
-
context 'does not coerce non-numeric value #{non_num_value.inspect}' do
|
118
|
-
let(:value) { non_num_value }
|
119
|
-
|
120
|
-
it { should equal(non_num_value) }
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::Hash, '.merge_options' do
|
4
|
-
subject { described_class.merge_options(type, options) }
|
5
|
-
|
6
|
-
let(:type) { { String => Integer } }
|
7
|
-
let(:options) { { :opt => 'val' } }
|
8
|
-
|
9
|
-
context 'when `type` responds to `size`' do
|
10
|
-
context 'when size is == 1' do
|
11
|
-
specify { subject[:opt].should == 'val' }
|
12
|
-
specify { subject[:key_type].should == String }
|
13
|
-
specify { subject[:value_type].should == Integer }
|
14
|
-
end
|
15
|
-
|
16
|
-
context 'when size is > 1' do
|
17
|
-
let(:type) { { :opt1 => 'val1', :opt2 => 'val2' } }
|
18
|
-
|
19
|
-
it 'should raise ArgumentError' do
|
20
|
-
message = "more than one [key => value] pair in `#{type.inspect}`"
|
21
|
-
expect { subject }.to raise_error(ArgumentError, message)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'when `type` does not respond to `size`' do
|
27
|
-
before do
|
28
|
-
type.should_receive(:respond_to?).with(:size).and_return(false)
|
29
|
-
end
|
30
|
-
|
31
|
-
it { should be_eql(options) }
|
32
|
-
end
|
33
|
-
end
|