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
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute::Hash, '.build' do
|
4
|
+
subject { described_class.build(type) }
|
5
|
+
|
6
|
+
share_examples_for 'a valid hash attribute instance' do
|
7
|
+
it { should be_instance_of(Virtus::Attribute::Hash) }
|
8
|
+
|
9
|
+
it { should be_frozen }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when type is Hash' do
|
13
|
+
let(:type) { Hash }
|
14
|
+
|
15
|
+
it { should be_instance_of(Virtus::Attribute::Hash) }
|
16
|
+
|
17
|
+
it 'sets default key type' do
|
18
|
+
expect(subject.type.key_type).to be(Axiom::Types::Object)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'sets default value type' do
|
22
|
+
expect(subject.type.value_type).to be(Axiom::Types::Object)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when type is Hash[String => Integer]' do
|
27
|
+
let(:type) { Hash[String => Integer] }
|
28
|
+
|
29
|
+
it { should be_instance_of(Virtus::Attribute::Hash) }
|
30
|
+
|
31
|
+
it 'sets key type' do
|
32
|
+
expect(subject.type.key_type).to be(Axiom::Types::String)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'sets value type' do
|
36
|
+
expect(subject.type.value_type).to be(Axiom::Types::Integer)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when type is Hash[Struct.new(:id) => Integer]' do
|
41
|
+
let(:type) { Hash[key_type => Integer] }
|
42
|
+
let(:key_type) { Struct.new(:id) }
|
43
|
+
|
44
|
+
it { should be_instance_of(Virtus::Attribute::Hash) }
|
45
|
+
|
46
|
+
it 'sets key type' do
|
47
|
+
expect(subject.type.key_type).to be(key_type)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sets value type' do
|
51
|
+
expect(subject.type.value_type).to be(Axiom::Types::Integer)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'when type is Hash[String => Struct.new(:id)]' do
|
56
|
+
let(:type) { Hash[String => value_type] }
|
57
|
+
let(:value_type) { Struct.new(:id) }
|
58
|
+
|
59
|
+
it { should be_instance_of(Virtus::Attribute::Hash) }
|
60
|
+
|
61
|
+
it 'sets key type' do
|
62
|
+
expect(subject.type.key_type).to be(Axiom::Types::String)
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'sets value type' do
|
66
|
+
expect(subject.type.value_type).to be(value_type)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when type is Hash[String => Integer, Integer => String]' do
|
71
|
+
let(:type) { Hash[String => Integer, :Integer => :String] }
|
72
|
+
|
73
|
+
specify do
|
74
|
+
expect { subject }.to raise_error(
|
75
|
+
ArgumentError,
|
76
|
+
"more than one [key => value] pair in `#{type}`"
|
77
|
+
)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -1,28 +1,90 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Virtus::Attribute::Hash, '#coerce' do
|
4
|
-
subject { object.coerce(
|
4
|
+
subject { object.coerce(input) }
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
fake(:coercer) { Virtus::Attribute::Coercer }
|
7
|
+
fake(:key_type) { Virtus::Attribute }
|
8
|
+
fake(:value_type) { Virtus::Attribute }
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
let(:object) {
|
11
|
+
described_class.build(Hash[key_primitive => value_primitive], options)
|
12
|
+
}
|
11
13
|
|
12
|
-
|
14
|
+
let(:options) { {} }
|
15
|
+
|
16
|
+
context 'when input is coercible to hash' do
|
17
|
+
let(:input) { Class.new { def to_hash; { :hello => 'World' }; end }.new }
|
18
|
+
let(:object) { described_class.build(Hash) }
|
19
|
+
|
20
|
+
it { should eq(:hello => 'World') }
|
13
21
|
end
|
14
22
|
|
15
|
-
context '
|
16
|
-
let(:
|
23
|
+
context 'when input is not coercible to hash' do
|
24
|
+
let(:input) { 'not really a hash' }
|
25
|
+
let(:object) { described_class.build(Hash) }
|
17
26
|
|
18
|
-
it { should be(
|
27
|
+
it { should be(input) }
|
19
28
|
end
|
20
29
|
|
21
|
-
context
|
22
|
-
|
23
|
-
|
30
|
+
context 'when input is a hash' do
|
31
|
+
context 'when key/value types are primitives' do
|
32
|
+
let(:options) {
|
33
|
+
{ :coercer => coercer, :key_type => key_type, :value_type => value_type }
|
34
|
+
}
|
35
|
+
|
36
|
+
let(:key_primitive) { String }
|
37
|
+
let(:value_primitive) { Integer }
|
38
|
+
|
39
|
+
let(:input) { Hash[1 => '1', 2 => '2'] }
|
40
|
+
|
41
|
+
it 'uses coercer to coerce key and value' do
|
42
|
+
stub(coercer).call(input) { input }
|
43
|
+
|
44
|
+
stub(key_type).coerce(1) { '1' }
|
45
|
+
stub(key_type).coerce(2) { '2' }
|
46
|
+
|
47
|
+
stub(value_type).coerce('1') { 1 }
|
48
|
+
stub(value_type).coerce('2') { 2 }
|
49
|
+
|
50
|
+
expect(subject).to eq(Hash['1' => 1, '2' => 2])
|
51
|
+
|
52
|
+
expect(key_type).to have_received.coerce(1)
|
53
|
+
expect(key_type).to have_received.coerce(2)
|
54
|
+
|
55
|
+
expect(value_type).to have_received.coerce('1')
|
56
|
+
expect(value_type).to have_received.coerce('2')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'when key/value types are EVs' do
|
61
|
+
let(:key_primitive) { OpenStruct }
|
62
|
+
let(:value_primitive) { Struct.new(:id) }
|
63
|
+
|
64
|
+
let(:input) { Hash[{:name => 'Test'} => [1]] }
|
65
|
+
let(:output) { Hash[key_primitive.new(:name => 'Test') => value_primitive.new(1)] }
|
66
|
+
|
67
|
+
it 'coerces keys and values' do
|
68
|
+
# FIXME: expect(subject).to eq(output) crashes in rspec
|
69
|
+
expect(subject.keys.first).to eq(output.keys.first)
|
70
|
+
expect(subject.values.first).to eq(output.values.first)
|
71
|
+
expect(subject.size).to be(1)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'when key type is an array and value type is another hash' do
|
76
|
+
let(:key_primitive) { Array[String] }
|
77
|
+
let(:value_primitive) { Hash[String => Integer] }
|
78
|
+
|
79
|
+
let(:key_attribute) { Virtus::Attribute.build(key_primitive) }
|
80
|
+
let(:value_attribute) { Virtus::Attribute.build(value_primitive) }
|
24
81
|
|
25
|
-
|
82
|
+
let(:input) { Hash[[1, 2], {:one => '1', :two => '2'}] }
|
83
|
+
let(:output) { Hash[key_attribute.coerce(input.keys.first) => value_attribute.coerce(input.values.first)] }
|
26
84
|
|
85
|
+
it 'coerces keys and values' do
|
86
|
+
expect(subject).to eq(output)
|
87
|
+
end
|
88
|
+
end
|
27
89
|
end
|
28
90
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#lazy?' do
|
4
|
+
subject { object.lazy? }
|
5
|
+
|
6
|
+
let(:object) { described_class.build(String, options) }
|
7
|
+
let(:options) { Hash[:lazy => lazy] }
|
8
|
+
|
9
|
+
context 'when :lazy is set to true' do
|
10
|
+
let(:lazy) { true }
|
11
|
+
|
12
|
+
it { should be(true) }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when :lazy is set to false' do
|
16
|
+
let(:lazy) { false }
|
17
|
+
|
18
|
+
it { should be(false) }
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#set_default_value' do
|
4
|
+
let(:object) { described_class.build(String, options.merge(:name => name, :default => default)) }
|
5
|
+
|
6
|
+
let(:model) { Class.new { def name; 'model'; end; attr_reader :test } }
|
7
|
+
let(:name) { :test }
|
8
|
+
let(:instance) { model.new }
|
9
|
+
let(:options) { {} }
|
10
|
+
|
11
|
+
before { object.set_default_value(instance) }
|
12
|
+
|
13
|
+
context 'with a nil' do
|
14
|
+
subject { instance }
|
15
|
+
|
16
|
+
let(:default) { nil }
|
17
|
+
|
18
|
+
its(:test) { should be(nil) }
|
19
|
+
its(:instance_variables) { should include(:'@test') }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with a non-clonable object' do
|
23
|
+
subject { instance }
|
24
|
+
|
25
|
+
let(:object) { described_class.build('Boolean', options.merge(:name => name, :default => default)) }
|
26
|
+
let(:default) { true }
|
27
|
+
|
28
|
+
its(:test) { should be(true) }
|
29
|
+
its(:instance_variables) { should include(:'@test') }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with a clonable' do
|
33
|
+
subject { instance }
|
34
|
+
|
35
|
+
let(:default) { [] }
|
36
|
+
|
37
|
+
its(:test) { should eq(default) }
|
38
|
+
its(:test) { should_not be(default) }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with a callable' do
|
42
|
+
subject { instance }
|
43
|
+
|
44
|
+
let(:default) { lambda { |model, attribute| "#{model.name}-#{attribute.name}" } }
|
45
|
+
|
46
|
+
its(:test) { should eq('model-test') }
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with a symbol' do
|
50
|
+
subject { instance }
|
51
|
+
|
52
|
+
context 'when it is a method name' do
|
53
|
+
let(:default) { :set_test }
|
54
|
+
|
55
|
+
context 'when method is public' do
|
56
|
+
let(:model) { Class.new { attr_reader :test; def set_test; @test = 'hello world'; end } }
|
57
|
+
|
58
|
+
its(:test) { should eq('hello world') }
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when method is private' do
|
62
|
+
let(:model) { Class.new { attr_reader :test; private; def set_test; @test = 'hello world'; end } }
|
63
|
+
|
64
|
+
its(:test) { should eq('hello world') }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when it is not a method name' do
|
69
|
+
let(:default) { :hello_world }
|
70
|
+
|
71
|
+
its(:test) { should eq('hello_world') }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -3,47 +3,27 @@ require 'spec_helper'
|
|
3
3
|
describe Virtus::Attribute, '#set' do
|
4
4
|
subject { object.set(instance, value) }
|
5
5
|
|
6
|
-
let(:object)
|
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') }
|
6
|
+
let(:object) { described_class.build(String, options.merge(:name => name)) }
|
13
7
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
it { should be(object) }
|
8
|
+
let(:model) { Class.new { attr_reader :test } }
|
9
|
+
let(:name) { :test }
|
10
|
+
let(:instance) { model.new }
|
11
|
+
let(:value) { 'Jane Doe' }
|
12
|
+
let(:options) { {} }
|
20
13
|
|
21
|
-
it
|
22
|
-
object.should_receive(:coerce).with(value)
|
23
|
-
subject
|
24
|
-
end
|
14
|
+
it { should be(value) }
|
25
15
|
|
26
|
-
|
27
|
-
|
28
|
-
|
16
|
+
context 'without coercion' do
|
17
|
+
specify do
|
18
|
+
expect { subject }.to change { instance.test }.to(value)
|
19
|
+
end
|
29
20
|
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
21
|
|
42
|
-
|
22
|
+
context 'with coercion' do
|
23
|
+
let(:value) { :'Jane Doe' }
|
43
24
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
instance.instance_variable_get(:@name).should be(value)
|
25
|
+
specify do
|
26
|
+
expect { subject }.to change { instance.test }.to('Jane Doe')
|
27
|
+
end
|
48
28
|
end
|
49
29
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Virtus::Attribute, '#value_coerced?' do
|
4
|
+
subject { object.value_coerced?(input) }
|
5
|
+
|
6
|
+
let(:object) { described_class.build(String) }
|
7
|
+
|
8
|
+
context 'when input is coerced' do
|
9
|
+
let(:input) { '1' }
|
10
|
+
|
11
|
+
it { should be(true) }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when input is not coerced' do
|
15
|
+
let(:input) { 1 }
|
16
|
+
|
17
|
+
it { should be(false) }
|
18
|
+
end
|
19
|
+
end
|
@@ -8,10 +8,8 @@ describe Virtus::AttributeSet, '#<<' do
|
|
8
8
|
let(:object) { described_class.new(parent, attributes) }
|
9
9
|
let(:name) { :name }
|
10
10
|
|
11
|
-
before { attribute.stub(:define_accessor_methods) }
|
12
|
-
|
13
11
|
context 'with a new attribute' do
|
14
|
-
let(:attribute) {
|
12
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => name) }
|
15
13
|
|
16
14
|
it { should equal(object) }
|
17
15
|
|
@@ -35,8 +33,8 @@ describe Virtus::AttributeSet, '#<<' do
|
|
35
33
|
end
|
36
34
|
|
37
35
|
context 'with a duplicate attribute' do
|
38
|
-
let(:attributes) { [
|
39
|
-
let(:attribute) {
|
36
|
+
let(:attributes) { [Virtus::Attribute.build(String, :name => name)] }
|
37
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => name) }
|
40
38
|
|
41
39
|
it { should equal(object) }
|
42
40
|
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Virtus::AttributeSet, '#define_reader_method' do
|
4
4
|
subject { described_class.new }
|
5
5
|
|
6
|
-
let(:attribute) {
|
6
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => method_name) }
|
7
7
|
|
8
8
|
if RUBY_VERSION < '1.9'
|
9
9
|
let(:method_name) { 'foo_bar' }
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Virtus::AttributeSet, '#define_writer_method' do
|
4
4
|
subject { described_class.new }
|
5
5
|
|
6
|
-
let(:attribute) {
|
6
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => :foo_bar) }
|
7
7
|
|
8
8
|
if RUBY_VERSION < '1.9'
|
9
9
|
let(:method_name) { 'foo_bar=' }
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Virtus::AttributeSet, '#each' do
|
4
4
|
let(:name) { :name }
|
5
|
-
let(:attribute) {
|
5
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => :name) }
|
6
6
|
let(:attributes) { [ attribute ] }
|
7
7
|
let(:parent) { described_class.new }
|
8
8
|
let(:object) { described_class.new(parent, attributes) }
|
@@ -32,7 +32,7 @@ describe Virtus::AttributeSet, '#each' do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
context 'when the parent has attributes that are not duplicates' do
|
35
|
-
let(:parent_attribute) {
|
35
|
+
let(:parent_attribute) { Virtus::Attribute.build(String, :name => :parent_name) }
|
36
36
|
let(:parent) { described_class.new([ parent_attribute ]) }
|
37
37
|
|
38
38
|
it { should equal(object) }
|
@@ -45,7 +45,7 @@ describe Virtus::AttributeSet, '#each' do
|
|
45
45
|
end
|
46
46
|
|
47
47
|
context 'when the parent has attributes that are duplicates' do
|
48
|
-
let(:parent_attribute) {
|
48
|
+
let(:parent_attribute) { Virtus::Attribute.build(String, :name => name) }
|
49
49
|
let(:parent) { described_class.new([ parent_attribute ]) }
|
50
50
|
|
51
51
|
it { should equal(object) }
|
@@ -4,7 +4,7 @@ describe Virtus::AttributeSet, '#[]' do
|
|
4
4
|
subject { object[name] }
|
5
5
|
|
6
6
|
let(:name) { :name }
|
7
|
-
let(:attribute) {
|
7
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => :name) }
|
8
8
|
let(:attributes) { [ attribute ] }
|
9
9
|
let(:parent) { described_class.new }
|
10
10
|
let(:object) { described_class.new(parent, attributes) }
|
@@ -9,7 +9,7 @@ describe Virtus::AttributeSet, '#[]=' do
|
|
9
9
|
let(:name) { :name }
|
10
10
|
|
11
11
|
context 'with a new attribute' do
|
12
|
-
let(:attribute) {
|
12
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => name) }
|
13
13
|
|
14
14
|
it { should equal(attribute) }
|
15
15
|
|
@@ -31,9 +31,9 @@ describe Virtus::AttributeSet, '#[]=' do
|
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'with a duplicate attribute' do
|
34
|
-
let(:original) {
|
35
|
-
let(:attributes) { [ original ]
|
36
|
-
let(:attribute) {
|
34
|
+
let(:original) { Virtus::Attribute.build(String, :name => name) }
|
35
|
+
let(:attributes) { [ original ] }
|
36
|
+
let(:attribute) { Virtus::Attribute.build(String, :name => name) }
|
37
37
|
|
38
38
|
it { should equal(attribute) }
|
39
39
|
|