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,218 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Coercion
|
3
|
-
|
4
|
-
# Coerce String values
|
5
|
-
class String < Object
|
6
|
-
primitive ::String
|
7
|
-
|
8
|
-
TRUE_VALUES = %w[ 1 on t true y yes ].freeze
|
9
|
-
FALSE_VALUES = %w[ 0 off f false n no ].freeze
|
10
|
-
BOOLEAN_MAP = ::Hash[ TRUE_VALUES.product([ true ]) + FALSE_VALUES.product([ false ]) ].freeze
|
11
|
-
|
12
|
-
INTEGER_REGEXP = /[-+]?(?:[0-9]\d*)/.freeze
|
13
|
-
EXPONENT_REGEXP = /(?:[eE][-+]?\d+)/.freeze
|
14
|
-
FRACTIONAL_REGEXP = /(?:\.\d+)/.freeze
|
15
|
-
|
16
|
-
NUMERIC_REGEXP = /\A(
|
17
|
-
#{INTEGER_REGEXP}#{FRACTIONAL_REGEXP}?#{EXPONENT_REGEXP}? |
|
18
|
-
#{FRACTIONAL_REGEXP}#{EXPONENT_REGEXP}?
|
19
|
-
)\z/x.freeze
|
20
|
-
|
21
|
-
# Coerce give value to a constant
|
22
|
-
#
|
23
|
-
# @example
|
24
|
-
# Virtus::Coercion::String.to_constant('String') # => String
|
25
|
-
#
|
26
|
-
# @param [String] value
|
27
|
-
#
|
28
|
-
# @return [Object]
|
29
|
-
#
|
30
|
-
# @api public
|
31
|
-
def self.to_constant(value)
|
32
|
-
names = value.split('::')
|
33
|
-
names.shift if names.first.empty?
|
34
|
-
names.inject(::Object) { |*args| constant_lookup(*args) }
|
35
|
-
end
|
36
|
-
|
37
|
-
# Lookup a constant within a module
|
38
|
-
#
|
39
|
-
# @param [Module] mod
|
40
|
-
#
|
41
|
-
# @param [String] name
|
42
|
-
#
|
43
|
-
# @return [Object]
|
44
|
-
#
|
45
|
-
# @api private
|
46
|
-
def self.constant_lookup(mod, name)
|
47
|
-
if mod.const_defined?(name, *EXTRA_CONST_ARGS)
|
48
|
-
mod.const_get(name, *EXTRA_CONST_ARGS)
|
49
|
-
else
|
50
|
-
mod.const_missing(name)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
private_class_method :constant_lookup
|
55
|
-
|
56
|
-
# Coerce give value to a symbol
|
57
|
-
#
|
58
|
-
# @example
|
59
|
-
# Virtus::Coercion::String.to_symbol('string') # => :string
|
60
|
-
#
|
61
|
-
# @param [String] value
|
62
|
-
#
|
63
|
-
# @return [Symbol]
|
64
|
-
#
|
65
|
-
# @api public
|
66
|
-
def self.to_symbol(value)
|
67
|
-
value.to_sym
|
68
|
-
end
|
69
|
-
|
70
|
-
# Coerce given value to Time
|
71
|
-
#
|
72
|
-
# @example
|
73
|
-
# Virtus::Coercion::String.to_time(string) # => Time object
|
74
|
-
#
|
75
|
-
# @param [String] value
|
76
|
-
#
|
77
|
-
# @return [Time]
|
78
|
-
#
|
79
|
-
# @api public
|
80
|
-
def self.to_time(value)
|
81
|
-
parse_value(::Time, value)
|
82
|
-
end
|
83
|
-
|
84
|
-
# Coerce given value to Date
|
85
|
-
#
|
86
|
-
# @example
|
87
|
-
# Virtus::Coercion::String.to_date(string) # => Date object
|
88
|
-
#
|
89
|
-
# @param [String] value
|
90
|
-
#
|
91
|
-
# @return [Date]
|
92
|
-
#
|
93
|
-
# @api public
|
94
|
-
def self.to_date(value)
|
95
|
-
parse_value(::Date, value)
|
96
|
-
end
|
97
|
-
|
98
|
-
# Coerce given value to DateTime
|
99
|
-
#
|
100
|
-
# @example
|
101
|
-
# Virtus::Coercion::String.to_datetime(string) # => DateTime object
|
102
|
-
#
|
103
|
-
# @param [String] value
|
104
|
-
#
|
105
|
-
# @return [DateTime]
|
106
|
-
#
|
107
|
-
# @api public
|
108
|
-
def self.to_datetime(value)
|
109
|
-
parse_value(::DateTime, value)
|
110
|
-
end
|
111
|
-
|
112
|
-
# Coerce value to TrueClass or FalseClass
|
113
|
-
#
|
114
|
-
# @example with "T"
|
115
|
-
# Virtus::Coercion::String.to_boolean('T') # => true
|
116
|
-
#
|
117
|
-
# @example with "F"
|
118
|
-
# Virtus::Coercion::String.to_boolean('F') # => false
|
119
|
-
#
|
120
|
-
# @param [#to_s]
|
121
|
-
#
|
122
|
-
# @return [Boolean]
|
123
|
-
#
|
124
|
-
# @api public
|
125
|
-
def self.to_boolean(value)
|
126
|
-
BOOLEAN_MAP.fetch(value.downcase, value)
|
127
|
-
end
|
128
|
-
|
129
|
-
# Coerce value to integer
|
130
|
-
#
|
131
|
-
# @example
|
132
|
-
# Virtus::Coercion::String.to_integer('1') # => 1
|
133
|
-
#
|
134
|
-
# @param [Object] value
|
135
|
-
#
|
136
|
-
# @return [Integer]
|
137
|
-
#
|
138
|
-
# @api public
|
139
|
-
def self.to_integer(value)
|
140
|
-
if value =~ /\A#{INTEGER_REGEXP}\z/
|
141
|
-
value.to_i
|
142
|
-
else
|
143
|
-
# coerce to a Float first to evaluate scientific notation (if any)
|
144
|
-
# that may change the integer part, then convert to an integer
|
145
|
-
coerced = to_float(value)
|
146
|
-
::Float === coerced ? coerced.to_i : coerced
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
# Coerce value to float
|
151
|
-
#
|
152
|
-
# @example
|
153
|
-
# Virtus::Coercion::String.to_float('1.2') # => 1.2
|
154
|
-
#
|
155
|
-
# @param [Object] value
|
156
|
-
#
|
157
|
-
# @return [Float]
|
158
|
-
#
|
159
|
-
# @api public
|
160
|
-
def self.to_float(value)
|
161
|
-
to_numeric(value, :to_f)
|
162
|
-
end
|
163
|
-
|
164
|
-
# Coerce value to decimal
|
165
|
-
#
|
166
|
-
# @example
|
167
|
-
# Virtus::Coercion::String.to_decimal('1.2') # => #<BigDecimal:b72157d4,'0.12E1',8(8)>
|
168
|
-
#
|
169
|
-
# @param [Object] value
|
170
|
-
#
|
171
|
-
# @return [BigDecimal]
|
172
|
-
#
|
173
|
-
# @api public
|
174
|
-
def self.to_decimal(value)
|
175
|
-
to_numeric(value, :to_d)
|
176
|
-
end
|
177
|
-
|
178
|
-
# Match numeric string
|
179
|
-
#
|
180
|
-
# @param [String] value
|
181
|
-
# value to typecast
|
182
|
-
# @param [Symbol] method
|
183
|
-
# method to typecast with
|
184
|
-
#
|
185
|
-
# @return [Numeric]
|
186
|
-
# number if matched, value if no match
|
187
|
-
#
|
188
|
-
# @api private
|
189
|
-
def self.to_numeric(value, method)
|
190
|
-
if value =~ NUMERIC_REGEXP
|
191
|
-
$1.public_send(method)
|
192
|
-
else
|
193
|
-
value
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
private_class_method :to_numeric
|
198
|
-
|
199
|
-
# Parse the value or return it as-is if it is invalid
|
200
|
-
#
|
201
|
-
# @param [#parse] parser
|
202
|
-
#
|
203
|
-
# @param [String] value
|
204
|
-
#
|
205
|
-
# @return [Time]
|
206
|
-
#
|
207
|
-
# @api private
|
208
|
-
def self.parse_value(parser, value)
|
209
|
-
parser.parse(value)
|
210
|
-
rescue ArgumentError
|
211
|
-
return value
|
212
|
-
end
|
213
|
-
|
214
|
-
private_class_method :parse_value
|
215
|
-
|
216
|
-
end # class String
|
217
|
-
end # class Coercion
|
218
|
-
end # module Virtus
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Coercion
|
3
|
-
|
4
|
-
# Coerce Symbol values
|
5
|
-
class Symbol < Object
|
6
|
-
primitive ::Symbol
|
7
|
-
|
8
|
-
# Coerce given value to String
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# Virtus::Coercion::Symbol.to_string(:name) # => "name"
|
12
|
-
#
|
13
|
-
# @param [Symbol] value
|
14
|
-
#
|
15
|
-
# @return [String]
|
16
|
-
#
|
17
|
-
# @api public
|
18
|
-
def self.to_string(value)
|
19
|
-
value.to_s
|
20
|
-
end
|
21
|
-
|
22
|
-
end # class Symbol
|
23
|
-
end # class Coercion
|
24
|
-
end # module Virtus
|
data/lib/virtus/coercion/time.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Coercion
|
3
|
-
|
4
|
-
# Coerce Time values
|
5
|
-
class Time < Object
|
6
|
-
extend TimeCoercions
|
7
|
-
|
8
|
-
primitive ::Time
|
9
|
-
|
10
|
-
# Passthrough the value
|
11
|
-
#
|
12
|
-
# @example
|
13
|
-
# Virtus::Coercion::DateTime.to_time(time) # => Time object
|
14
|
-
#
|
15
|
-
# @param [DateTime] value
|
16
|
-
#
|
17
|
-
# @return [Date]
|
18
|
-
#
|
19
|
-
# @api public
|
20
|
-
def self.to_time(value)
|
21
|
-
value
|
22
|
-
end
|
23
|
-
|
24
|
-
# Creates a Fixnum instance from a Time object
|
25
|
-
#
|
26
|
-
# @example
|
27
|
-
# Virtus::Coercion::Time.to_integer(time) # => Fixnum object
|
28
|
-
#
|
29
|
-
# @param [Time] value
|
30
|
-
#
|
31
|
-
# @return [Fixnum]
|
32
|
-
#
|
33
|
-
# @api public
|
34
|
-
def self.to_integer(value)
|
35
|
-
value.to_i
|
36
|
-
end
|
37
|
-
|
38
|
-
end # class Time
|
39
|
-
end # class Coercion
|
40
|
-
end # module Virtus
|
@@ -1,86 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Coercion
|
3
|
-
|
4
|
-
# Common time coercion methods
|
5
|
-
module TimeCoercions
|
6
|
-
|
7
|
-
# Coerce given value to String
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# Virtus::Coercion::Time.to_string(time) # => "Wed Jul 20 10:30:41 -0700 2011"
|
11
|
-
#
|
12
|
-
# @param [Date,Time,DateTime] value
|
13
|
-
#
|
14
|
-
# @return [String]
|
15
|
-
#
|
16
|
-
# @api public
|
17
|
-
def to_string(value)
|
18
|
-
value.to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
# Coerce given value to Time
|
22
|
-
#
|
23
|
-
# @example
|
24
|
-
# Virtus::Coercion::DateTime.to_time(datetime) # => Time object
|
25
|
-
#
|
26
|
-
# @param [Date,DateTime] value
|
27
|
-
#
|
28
|
-
# @return [Time]
|
29
|
-
#
|
30
|
-
# @api public
|
31
|
-
def to_time(value)
|
32
|
-
coerce_with_method(value, :to_time)
|
33
|
-
end
|
34
|
-
|
35
|
-
# Coerce given value to DateTime
|
36
|
-
#
|
37
|
-
# @example
|
38
|
-
# Virtus::Coercion::Time.to_datetime(time) # => DateTime object
|
39
|
-
#
|
40
|
-
# @param [Date,Time] value
|
41
|
-
#
|
42
|
-
# @return [DateTime]
|
43
|
-
#
|
44
|
-
# @api public
|
45
|
-
def to_datetime(value)
|
46
|
-
coerce_with_method(value, :to_datetime)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Coerce given value to Date
|
50
|
-
#
|
51
|
-
# @example
|
52
|
-
# Virtus::Coercion::Time.to_date(time) # => Date object
|
53
|
-
#
|
54
|
-
# @param [Time,DateTime] value
|
55
|
-
#
|
56
|
-
# @return [Date]
|
57
|
-
#
|
58
|
-
# @api public
|
59
|
-
def to_date(value)
|
60
|
-
coerce_with_method(value, :to_date)
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
# Try to use native coercion method on the given value
|
66
|
-
#
|
67
|
-
# Falls back to String-based parsing
|
68
|
-
#
|
69
|
-
# @param [Date,DateTime,Time] value
|
70
|
-
# @param [Symbol] method
|
71
|
-
#
|
72
|
-
# @return [Date,DateTime,Time]
|
73
|
-
#
|
74
|
-
# @api private
|
75
|
-
def coerce_with_method(value, method)
|
76
|
-
coerced = super
|
77
|
-
if coerced.equal?(value)
|
78
|
-
String.public_send(method, to_string(value))
|
79
|
-
else
|
80
|
-
coerced
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
end # module TimeCoercions
|
85
|
-
end # class Coercion
|
86
|
-
end # module Virtus
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Virtus
|
2
|
-
class Coercion
|
3
|
-
|
4
|
-
# Coerce true values
|
5
|
-
class TrueClass < Object
|
6
|
-
primitive ::TrueClass
|
7
|
-
|
8
|
-
# Coerce given value to String
|
9
|
-
#
|
10
|
-
# @example
|
11
|
-
# Virtus::Coercion::TrueClass.to_string(true) # => "true"
|
12
|
-
#
|
13
|
-
# @param [TrueClass] value
|
14
|
-
#
|
15
|
-
# @return [String]
|
16
|
-
#
|
17
|
-
# @api public
|
18
|
-
def self.to_string(value)
|
19
|
-
value.to_s
|
20
|
-
end
|
21
|
-
|
22
|
-
end # class TrueClass
|
23
|
-
end # class Coercion
|
24
|
-
end # module Virtus
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::Array, '#coerce' do
|
4
|
-
subject { attribute.coerce(value) }
|
5
|
-
|
6
|
-
let(:attribute) { described_class.new(:colors) }
|
7
|
-
|
8
|
-
context 'with a Hash' do
|
9
|
-
let(:value) { { :foo => 'bar' } }
|
10
|
-
|
11
|
-
it { should eql([ [ :foo, 'bar' ] ]) }
|
12
|
-
end
|
13
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Virtus::Attribute::Boolean, '#coerce' do
|
4
|
-
subject { attribute.coerce(value) }
|
5
|
-
|
6
|
-
let(:attribute) { described_class.new(:is_admin) }
|
7
|
-
|
8
|
-
context 'with 1' do
|
9
|
-
let(:value) { 1 }
|
10
|
-
|
11
|
-
it { should be(true) }
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'with "1"' do
|
15
|
-
let(:value) { '1' }
|
16
|
-
|
17
|
-
it { should be(true) }
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'with "true"' do
|
21
|
-
let(:value) { 'true' }
|
22
|
-
|
23
|
-
it { should be(true) }
|
24
|
-
end
|
25
|
-
|
26
|
-
context 'with "TRUE"' do
|
27
|
-
let(:value) { 'TRUE' }
|
28
|
-
|
29
|
-
it { should be(true) }
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'with "t"' do
|
33
|
-
let(:value) { 't' }
|
34
|
-
|
35
|
-
it { should be(true) }
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'with "T"' do
|
39
|
-
let(:value) { 'T' }
|
40
|
-
|
41
|
-
it { should be(true) }
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with 0' do
|
45
|
-
let(:value) { 0 }
|
46
|
-
|
47
|
-
it { should be(false) }
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'with "0"' do
|
51
|
-
let(:value) { '0' }
|
52
|
-
|
53
|
-
it { should be(false) }
|
54
|
-
end
|
55
|
-
|
56
|
-
context 'with "false"' do
|
57
|
-
let(:value) { 'false' }
|
58
|
-
|
59
|
-
it { should be(false) }
|
60
|
-
end
|
61
|
-
|
62
|
-
context 'with "FALSE"' do
|
63
|
-
let(:value) { 'FALSE' }
|
64
|
-
|
65
|
-
it { should be(false) }
|
66
|
-
end
|
67
|
-
|
68
|
-
context 'with "f"' do
|
69
|
-
let(:value) { 'f' }
|
70
|
-
|
71
|
-
it { should be(false) }
|
72
|
-
end
|
73
|
-
|
74
|
-
context 'with "F"' do
|
75
|
-
let(:value) { 'F' }
|
76
|
-
|
77
|
-
it { should be(false) }
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'with "Foo"' do
|
81
|
-
let(:value) { 'Foo' }
|
82
|
-
|
83
|
-
it { should equal(value) }
|
84
|
-
end
|
85
|
-
end
|