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
data/.rvmrc
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
rvm use @$(basename `pwd`) --create
|
data/TODO
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
* Make #to_time #to_date and #to_datetime work on Ruby 1.8.7 instead of typecasting to string and parsing the value
|
data/config/roodi.yml
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
---
|
2
|
-
AbcMetricMethodCheck: { score: 12.1 }
|
3
|
-
AssignmentInConditionalCheck: { }
|
4
|
-
CaseMissingElseCheck: { }
|
5
|
-
ClassLineCountCheck: { line_count: 325 }
|
6
|
-
ClassNameCheck: { pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/ }
|
7
|
-
ClassVariableCheck: { }
|
8
|
-
CyclomaticComplexityBlockCheck: { complexity: 4 }
|
9
|
-
CyclomaticComplexityMethodCheck: { complexity: 4 }
|
10
|
-
EmptyRescueBodyCheck: { }
|
11
|
-
ForLoopCheck: { }
|
12
|
-
MethodLineCountCheck: { line_count: 10 }
|
13
|
-
MethodNameCheck: { pattern: !ruby/regexp /\A(?:[a-z\d](?:_?[a-z\d])+[?!=]?|\[\]=?|==|<=>|<<|[+*&|-])\z/ }
|
14
|
-
ModuleLineCountCheck: { line_count: 331 }
|
15
|
-
ModuleNameCheck: { pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/ }
|
16
|
-
# TODO: decrease parameter_count to 2 or less
|
17
|
-
ParameterNumberCheck: { parameter_count: 3 }
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Array
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Post
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :tags, Array
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# post = Post.new(:tags => %w(red green blue))
|
14
|
-
#
|
15
|
-
class Array < Collection
|
16
|
-
primitive ::Array
|
17
|
-
coercion_method :to_array
|
18
|
-
default primitive.new
|
19
|
-
|
20
|
-
include Collection::MemberCoercion
|
21
|
-
|
22
|
-
end # class Array
|
23
|
-
end # class Attribute
|
24
|
-
end # module Virtus
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Class
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Entity
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :model, Class
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# post = Entity.new(:model => Model)
|
14
|
-
#
|
15
|
-
class Class < Object
|
16
|
-
primitive ::Class
|
17
|
-
coercion_method :to_constant
|
18
|
-
|
19
|
-
end # class Class
|
20
|
-
end # class Attribute
|
21
|
-
end # module Virtus
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Date
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Post
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :published_on, Date
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Post.new(:published_on => Date.today)
|
14
|
-
#
|
15
|
-
# # typecasting from a string
|
16
|
-
# Post.new(:published_on => '2011/06/09')
|
17
|
-
#
|
18
|
-
# # typecasting from a hash
|
19
|
-
# Post.new(:published_on => {
|
20
|
-
# :year => 2011,
|
21
|
-
# :month => 6,
|
22
|
-
# :day => 9,
|
23
|
-
# })
|
24
|
-
#
|
25
|
-
# # typecasting from an object which implements #to_date
|
26
|
-
# Post.new(:published_on => DateTime.now)
|
27
|
-
#
|
28
|
-
class Date < Object
|
29
|
-
primitive ::Date
|
30
|
-
coercion_method :to_date
|
31
|
-
|
32
|
-
end # class Date
|
33
|
-
end # class Attribute
|
34
|
-
end # module Virtus
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# DateTime
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Post
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :published_at, DateTime
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Post.new(:published_at => DateTime.now)
|
14
|
-
#
|
15
|
-
# # typecasting from a string
|
16
|
-
# Post.new(:published_on => '2011/06/09 10:48')
|
17
|
-
#
|
18
|
-
# # typecasting from a hash
|
19
|
-
# Post.new(:published_on => {
|
20
|
-
# :year => 2011,
|
21
|
-
# :month => 6,
|
22
|
-
# :day => 9,
|
23
|
-
# :hour => 10,
|
24
|
-
# :min => 48,
|
25
|
-
# })
|
26
|
-
#
|
27
|
-
# # typecasting from an object which implements #to_datetime
|
28
|
-
# Post.new(:published_on => Time.now)
|
29
|
-
#
|
30
|
-
class DateTime < Object
|
31
|
-
primitive ::DateTime
|
32
|
-
coercion_method :to_datetime
|
33
|
-
|
34
|
-
end # class DateTim
|
35
|
-
end # class Attribute
|
36
|
-
end # module Virtus
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Decimal
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class ExchangeRate
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :dollar, Decimal
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# ExchangeRate.new(:dollar => '2.6948')
|
14
|
-
#
|
15
|
-
class Decimal < Numeric
|
16
|
-
primitive ::BigDecimal
|
17
|
-
coercion_method :to_decimal
|
18
|
-
|
19
|
-
end # class Decimal
|
20
|
-
end # class Attribute
|
21
|
-
end # module Virtus
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
class EmbeddedValue < Object
|
4
|
-
|
5
|
-
# EmbeddedValue attribute handling OpenStruct primitive or Virtus object
|
6
|
-
#
|
7
|
-
class FromOpenStruct < EmbeddedValue
|
8
|
-
|
9
|
-
# @api private
|
10
|
-
def coerce(attributes)
|
11
|
-
unless attributes.nil?
|
12
|
-
super or @primitive.new(attributes)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
end # class FromOpenStruct
|
17
|
-
end # class EmbeddedValue
|
18
|
-
end # class Attribute
|
19
|
-
end # module Virtus
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
class EmbeddedValue < Object
|
4
|
-
|
5
|
-
# EmbeddedValue attribute handling Struct primitive
|
6
|
-
#
|
7
|
-
class FromStruct < EmbeddedValue
|
8
|
-
|
9
|
-
# @api private
|
10
|
-
def coerce(attributes)
|
11
|
-
unless attributes.nil?
|
12
|
-
super or @primitive.new(*attributes)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
end # class FromStruct
|
17
|
-
end # class EmbeddedValue
|
18
|
-
end # class Attribute
|
19
|
-
end # module Virtus
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Float
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class ExchangeRate
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :dollar, Float
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# ExchangeRate.new(:dollar => 2.69)
|
14
|
-
#
|
15
|
-
# # typecasting from a string
|
16
|
-
# ExchangeRate.new(:dollar => '2.69')
|
17
|
-
#
|
18
|
-
# # typecasting from an integer
|
19
|
-
# ExchangeRate.new(:dollar => 2)
|
20
|
-
#
|
21
|
-
# # typecasting from an object which implements #to_f
|
22
|
-
# ExchangeRate.new(:dollar => BigDecimal.new('2.69')
|
23
|
-
#
|
24
|
-
class Float < Numeric
|
25
|
-
primitive ::Float
|
26
|
-
coercion_method :to_float
|
27
|
-
|
28
|
-
end # class Float
|
29
|
-
end # class Attribute
|
30
|
-
end # module Virtus
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Integer
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Post
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :read_count, Integer
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Post.new(:read_count => 100)
|
14
|
-
#
|
15
|
-
# # typecasting from a string
|
16
|
-
# Post.new(:read_count => '100')
|
17
|
-
#
|
18
|
-
# # typecasting from an object that implements #to_i
|
19
|
-
# Post.new(:read_count => 100.0)
|
20
|
-
#
|
21
|
-
class Integer < Numeric
|
22
|
-
primitive ::Integer
|
23
|
-
coercion_method :to_integer
|
24
|
-
|
25
|
-
end # class Integer
|
26
|
-
end # class Attribute
|
27
|
-
end # module Virtus
|
data/lib/virtus/attribute/set.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Set
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Post
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :tags, Set
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# post = Post.new(:tags => %w(red green blue))
|
14
|
-
#
|
15
|
-
class Set < Collection
|
16
|
-
primitive ::Set
|
17
|
-
coercion_method :to_set
|
18
|
-
default primitive.new
|
19
|
-
|
20
|
-
include Collection::MemberCoercion
|
21
|
-
|
22
|
-
end # class Set
|
23
|
-
end # class Attribute
|
24
|
-
end # module Virtus
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# String
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class User
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :name, String
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# User.new(:name => 'John')
|
14
|
-
#
|
15
|
-
# # typecasting from an object which implements #to_s
|
16
|
-
# User.new(:name => :John)
|
17
|
-
#
|
18
|
-
class String < Object
|
19
|
-
primitive ::String
|
20
|
-
coercion_method :to_string
|
21
|
-
|
22
|
-
end # class String
|
23
|
-
end # class Attribute
|
24
|
-
end # module Virtus
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Symbol
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Product
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :code, Symbol
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# product = Product.new(:code => :red)
|
14
|
-
#
|
15
|
-
class Symbol < Object
|
16
|
-
primitive ::Symbol
|
17
|
-
coercion_method :to_symbol
|
18
|
-
|
19
|
-
end # class Symbol
|
20
|
-
end # class Attribute
|
21
|
-
end # module Virtus
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# Time
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# class Post
|
8
|
-
# include Virtus
|
9
|
-
#
|
10
|
-
# attribute :published_at, Time
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# Post.new(:published_at => Time.now)
|
14
|
-
#
|
15
|
-
# # typecasting from a string
|
16
|
-
# Post.new(:published_at => '2011/06/09 11:08')
|
17
|
-
#
|
18
|
-
# # typecasting from a hash
|
19
|
-
# Post.new(:published_at => {
|
20
|
-
# :year => 2011,
|
21
|
-
# :month => 6,
|
22
|
-
# :day => 9,
|
23
|
-
# :hour => 11,
|
24
|
-
# :min => 8,
|
25
|
-
# })
|
26
|
-
#
|
27
|
-
# # typecasting from an object which implements #to_time
|
28
|
-
# Post.new(:published_at => DateTime.now)
|
29
|
-
#
|
30
|
-
class Time < Object
|
31
|
-
primitive ::Time
|
32
|
-
coercion_method :to_time
|
33
|
-
|
34
|
-
end # class Time
|
35
|
-
end # class Attribute
|
36
|
-
end # module Virtus
|
data/lib/virtus/coercion.rb
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
|
3
|
-
# Coerce abstract class
|
4
|
-
#
|
5
|
-
# @abstract
|
6
|
-
#
|
7
|
-
class Coercion
|
8
|
-
extend DescendantsTracker
|
9
|
-
extend TypeLookup
|
10
|
-
extend Options
|
11
|
-
|
12
|
-
accept_options :primitive
|
13
|
-
|
14
|
-
# Return a class that matches given name
|
15
|
-
#
|
16
|
-
# Defaults to Virtus::Coercion::Object
|
17
|
-
#
|
18
|
-
# @example
|
19
|
-
# Virtus::Coercion['String'] # => Virtus::Coercion::String
|
20
|
-
# Virtus::Coercion[String] # => Virtus::Coercion::String
|
21
|
-
#
|
22
|
-
# @param [String]
|
23
|
-
#
|
24
|
-
# @return [Class]
|
25
|
-
#
|
26
|
-
# @api private
|
27
|
-
def self.[](name)
|
28
|
-
determine_type(name) or Coercion::Object
|
29
|
-
end
|
30
|
-
|
31
|
-
end # Coerce
|
32
|
-
end # Virtus
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Coercion
|
3
|
-
|
4
|
-
# Coerce Array values
|
5
|
-
class Array < Object
|
6
|
-
primitive ::Array
|
7
|
-
|
8
|
-
TIME_SEGMENTS = [ :year, :month, :day, :hour, :min, :sec ].freeze
|
9
|
-
|
10
|
-
# Creates a Set instance from an Array
|
11
|
-
#
|
12
|
-
# @param [Array] value
|
13
|
-
#
|
14
|
-
# @return [Array]
|
15
|
-
#
|
16
|
-
# @api private
|
17
|
-
def self.to_set(value)
|
18
|
-
value.to_set
|
19
|
-
end
|
20
|
-
|
21
|
-
end # class Array
|
22
|
-
end # class Coercion
|
23
|
-
end # module Virtus
|