virtus 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. data/.travis.yml +9 -2
  2. data/.yardopts +1 -0
  3. data/History.md +51 -0
  4. data/{README.markdown → README.md} +63 -7
  5. data/TODO +2 -4
  6. data/VERSION +1 -1
  7. data/config/flay.yml +2 -2
  8. data/config/flog.yml +1 -1
  9. data/config/roodi.yml +5 -6
  10. data/config/site.reek +3 -3
  11. data/examples/custom_coercion_spec.rb +50 -0
  12. data/examples/default_values_spec.rb +21 -0
  13. data/lib/virtus.rb +21 -6
  14. data/lib/virtus/attribute.rb +113 -253
  15. data/lib/virtus/attribute/array.rb +6 -3
  16. data/lib/virtus/attribute/boolean.rb +9 -28
  17. data/lib/virtus/attribute/date.rb +9 -12
  18. data/lib/virtus/attribute/date_time.rb +10 -12
  19. data/lib/virtus/attribute/decimal.rb +4 -11
  20. data/lib/virtus/attribute/float.rb +4 -11
  21. data/lib/virtus/attribute/hash.rb +5 -3
  22. data/lib/virtus/attribute/integer.rb +4 -11
  23. data/lib/virtus/attribute/numeric.rb +1 -0
  24. data/lib/virtus/attribute/object.rb +1 -0
  25. data/lib/virtus/attribute/string.rb +4 -11
  26. data/lib/virtus/attribute/time.rb +9 -16
  27. data/lib/virtus/class_methods.rb +42 -7
  28. data/lib/virtus/coercion.rb +32 -0
  29. data/lib/virtus/coercion/date.rb +26 -0
  30. data/lib/virtus/coercion/date_time.rb +26 -0
  31. data/lib/virtus/coercion/decimal.rb +40 -0
  32. data/lib/virtus/coercion/false_class.rb +24 -0
  33. data/lib/virtus/coercion/float.rb +24 -0
  34. data/lib/virtus/coercion/hash.rb +82 -0
  35. data/lib/virtus/coercion/integer.rb +60 -0
  36. data/lib/virtus/coercion/numeric.rb +66 -0
  37. data/lib/virtus/coercion/object.rb +25 -0
  38. data/lib/virtus/coercion/string.rb +155 -0
  39. data/lib/virtus/coercion/symbol.rb +24 -0
  40. data/lib/virtus/coercion/time.rb +26 -0
  41. data/lib/virtus/coercion/time_coercions.rb +85 -0
  42. data/lib/virtus/coercion/true_class.rb +24 -0
  43. data/lib/virtus/instance_methods.rb +7 -0
  44. data/lib/virtus/support/descendants_tracker.rb +1 -1
  45. data/lib/virtus/support/options.rb +114 -0
  46. data/lib/virtus/support/type_lookup.rb +95 -0
  47. data/spec/integration/virtus/attributes/attribute/{typecast_spec.rb → set_spec.rb} +7 -7
  48. data/spec/unit/shared/attribute.rb +3 -3
  49. data/spec/unit/shared/attribute/accept_options.rb +0 -18
  50. data/spec/unit/shared/attribute/accepted_options.rb +0 -6
  51. data/spec/unit/shared/attribute/get.rb +32 -17
  52. data/spec/unit/shared/attribute/inspect.rb +7 -0
  53. data/spec/unit/shared/attribute/primitive.rb +15 -0
  54. data/spec/unit/shared/attribute/set.rb +16 -21
  55. data/spec/unit/virtus/attribute/array_spec.rb +18 -3
  56. data/spec/unit/virtus/attribute/boolean_spec.rb +8 -6
  57. data/spec/unit/virtus/attribute/date_spec.rb +8 -6
  58. data/spec/unit/virtus/attribute/date_time_spec.rb +8 -6
  59. data/spec/unit/virtus/attribute/decimal_spec.rb +18 -6
  60. data/spec/unit/virtus/attribute/float_spec.rb +19 -7
  61. data/spec/unit/virtus/attribute/hash_spec.rb +5 -3
  62. data/spec/unit/virtus/attribute/integer_spec.rb +10 -8
  63. data/spec/unit/virtus/attribute/string_spec.rb +10 -8
  64. data/spec/unit/virtus/attribute/time_spec.rb +8 -6
  65. data/spec/unit/virtus/class_methods/attributes_spec.rb +11 -0
  66. data/spec/unit/virtus/coercion/class_name_reference_spec.rb +17 -0
  67. data/spec/unit/virtus/coercion/date/class_methods/to_datetime_spec.rb +30 -0
  68. data/spec/unit/virtus/coercion/date/class_methods/to_string_spec.rb +12 -0
  69. data/spec/unit/virtus/coercion/date/class_methods/to_time_spec.rb +12 -0
  70. data/spec/unit/virtus/coercion/date_time/class_methods/to_date_spec.rb +30 -0
  71. data/spec/unit/virtus/coercion/date_time/class_methods/to_string_spec.rb +12 -0
  72. data/spec/unit/virtus/coercion/date_time/class_methods/to_time_spec.rb +30 -0
  73. data/spec/unit/virtus/coercion/decimal/class_methods/to_float_spec.rb +12 -0
  74. data/spec/unit/virtus/coercion/decimal/class_methods/to_integer_spec.rb +12 -0
  75. data/spec/unit/virtus/coercion/decimal/class_methods/to_string_spec.rb +12 -0
  76. data/spec/unit/virtus/coercion/false_class/class_methods/to_string_spec.rb +12 -0
  77. data/spec/unit/virtus/coercion/float/class_methods/to_decimal_spec.rb +12 -0
  78. data/spec/unit/virtus/coercion/float/class_methods/to_integer_spec.rb +12 -0
  79. data/spec/unit/virtus/coercion/float/class_methods/to_string_spec.rb +12 -0
  80. data/spec/unit/virtus/coercion/hash/class_methods/to_array_spec.rb +12 -0
  81. data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +31 -0
  82. data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +31 -0
  83. data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +31 -0
  84. data/spec/unit/virtus/coercion/integer/class_methods/to_boolean_spec.rb +25 -0
  85. data/spec/unit/virtus/coercion/integer/class_methods/to_decimal_spec.rb +12 -0
  86. data/spec/unit/virtus/coercion/integer/class_methods/to_float_spec.rb +12 -0
  87. data/spec/unit/virtus/coercion/integer/class_methods/to_string_spec.rb +12 -0
  88. data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +33 -0
  89. data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +29 -0
  90. data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +23 -0
  91. data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +50 -0
  92. data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +23 -0
  93. data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +21 -0
  94. data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +21 -0
  95. data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +50 -0
  96. data/spec/unit/virtus/coercion/symbol/class_methods/to_string_spec.rb +12 -0
  97. data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +12 -0
  98. data/spec/unit/virtus/instance_methods/attributes_spec.rb +7 -0
  99. data/spec/unit/virtus/options/accept_options_spec.rb +38 -0
  100. data/spec/unit/virtus/options/accepted_options_spec.rb +21 -0
  101. data/spec/unit/virtus/options/options_spec.rb +11 -0
  102. data/spec/unit/virtus/type_lookup/determine_type_spec.rb +68 -0
  103. data/spec/unit/virtus/type_lookup/primitive_spec.rb +9 -0
  104. data/virtus.gemspec +70 -17
  105. metadata +78 -27
  106. data/History.txt +0 -38
  107. data/lib/virtus/typecast/boolean.rb +0 -29
  108. data/lib/virtus/typecast/numeric.rb +0 -87
  109. data/lib/virtus/typecast/string.rb +0 -24
  110. data/lib/virtus/typecast/time.rb +0 -192
  111. data/spec/unit/shared/attribute/complex.rb +0 -15
  112. data/spec/unit/shared/attribute/options.rb +0 -7
  113. data/spec/unit/virtus/attribute/attribute_spec.rb +0 -12
@@ -1,24 +0,0 @@
1
- module Virtus
2
- module Typecast
3
-
4
- # Typecast any object to a string
5
- class String
6
-
7
- # Typecast value to a string
8
- #
9
- # @example
10
- # Virtus::Typecast::String.call(1) # => "1"
11
- # Virtus::Typecast::String.call([]) # => "[]"
12
- #
13
- # @param [Object] value
14
- #
15
- # @return [String]
16
- #
17
- # @api public
18
- def self.call(value)
19
- value.to_s
20
- end
21
-
22
- end # class String
23
- end # module Typecast
24
- end # module Virtus
@@ -1,192 +0,0 @@
1
- module Virtus
2
- module Typecast
3
-
4
- # Typecast various values into Date, DateTime or Time
5
- class Time
6
- SEGMENTS = [ :year, :month, :day, :hour, :min, :sec ].freeze
7
-
8
- METHOD_TO_CLASS = {
9
- :to_time => ::Time,
10
- :to_date => ::Date,
11
- :to_datetime => ::DateTime
12
- }.freeze
13
-
14
- # Typecasts an arbitrary value to a Time
15
- #
16
- # Handles both Hashes and Time instances
17
- #
18
- # @example
19
- # Virtus::Typecast::Time.to_time('2011/06/09 12:01')
20
- # # => Thu Jun 09 12:01:00 +0200 2011
21
- #
22
- # @param [#to_hash, #to_s] value
23
- # value to be typecast
24
- #
25
- # @return [Time]
26
- # Time constructed from value
27
- #
28
- # @api public
29
- def self.to_time(value)
30
- call(value, :to_time)
31
- end
32
-
33
- # Typecasts an arbitrary value to a Date
34
- #
35
- # Handles both Hashes and Date instances
36
- #
37
- # @example
38
- # Virtus::Typecast::Time.to_date('2011/06/09')
39
- # # => #<Date: 4911443/2,0,2299161>
40
- #
41
- # @param [#to_hash, #to_s] value
42
- # value to be typecast
43
- #
44
- # @return [Date]
45
- # Date constructed from value
46
- #
47
- # @api public
48
- def self.to_date(value)
49
- call(value, :to_date)
50
- end
51
-
52
- # Typecasts an arbitrary value to a DateTime
53
- #
54
- # Handles both Hashes and DateTime instances
55
- #
56
- # @example
57
- # Virtus::Typecast::Time.to_datetime('2011/06/09 12:01')
58
- # # => #<DateTime: 3536239681/1440,0,2299161>
59
- #
60
- # @param [#to_hash, #to_s] value
61
- # value to be typecast
62
- #
63
- # @return [DateTime]
64
- # DateTime constructed from value
65
- #
66
- # @api public
67
- def self.to_datetime(value)
68
- call(value, :to_datetime)
69
- end
70
-
71
- # Coerce the value into a Date, Time or DateTime object
72
- #
73
- # @param [#to_hash, #to_s] value
74
- #
75
- # @param [Symbol] method
76
- #
77
- # @return [Object]
78
- #
79
- # @api private
80
- def self.call(value, method)
81
- return value.send(method) if value.respond_to?(method)
82
-
83
- begin
84
- if value.respond_to?(:to_hash)
85
- from_hash(value.to_hash, method)
86
- else
87
- from_string(value.to_s, method)
88
- end
89
- rescue ArgumentError
90
- return value
91
- end
92
- end
93
-
94
- private_class_method :call
95
-
96
- # Coerce the string into a Date, Time or DateTime object
97
- #
98
- # @param [String] value
99
- #
100
- # @param [Symbol] method
101
- #
102
- # @return [Object]
103
- #
104
- # @api private
105
- def self.from_string(value, method)
106
- METHOD_TO_CLASS[method].parse(value)
107
- end
108
-
109
- private_class_method :from_string
110
-
111
- # Coerce the Hash into a Date, Time or DateTime object
112
- #
113
- # @param [Hash] value
114
- #
115
- # @param [Symbol] method
116
- #
117
- # @return [Object]
118
- #
119
- # @api private
120
- def self.from_hash(value, method)
121
- send("hash_#{method}", value)
122
- end
123
-
124
- private_class_method :from_hash
125
-
126
- # Creates a Time instance from a Hash
127
- #
128
- # Valid keys are: :year, :month, :day, :hour, :min, :sec
129
- #
130
- # @param [Hash] value
131
- #
132
- # @return [Time]
133
- #
134
- # @api private
135
- def self.hash_to_time(value)
136
- ::Time.local(*extract(value))
137
- end
138
-
139
- private_class_method :hash_to_time
140
-
141
- # Creates a Date instance from a Hash
142
- #
143
- # Valid keys are: :year, :month, :day, :hour
144
- #
145
- # @param [Hash] value
146
- #
147
- # @return [Date]
148
- #
149
- # @api private
150
- def self.hash_to_date(value)
151
- ::Date.new(*extract(value).first(3))
152
- end
153
-
154
- private_class_method :hash_to_date
155
-
156
- # Creates a DateTime instance from a Hash
157
- #
158
- # Valid keys are: :year, :month, :day, :hour, :min, :sec
159
- #
160
- # @param [Hash] value
161
- #
162
- # @return [DateTime]
163
- #
164
- # @api private
165
- def self.hash_to_datetime(value)
166
- ::DateTime.new(*extract(value))
167
- end
168
-
169
- private_class_method :hash_to_datetime
170
-
171
- # Extracts the given args from a Hash
172
- #
173
- # If a value does not exist, it uses the value of Time.now
174
- #
175
- # @param [Hash] value
176
- #
177
- # @return [Array]
178
- #
179
- # @api private
180
- def self.extract(value)
181
- now = ::Time.now
182
-
183
- SEGMENTS.map do |segment|
184
- Numeric.to_i(value.fetch(segment, now.send(segment)))
185
- end
186
- end
187
-
188
- private_class_method :extract
189
-
190
- end # class Time
191
- end # module Typecast
192
- end # module Virtus
@@ -1,15 +0,0 @@
1
- shared_examples_for 'Attribute#complex?' do
2
- let(:attribute) { described_class.new(attribute_name, :complex => complex) }
3
-
4
- subject { attribute.complex? }
5
-
6
- context "when set to true" do
7
- let(:complex) { true }
8
- it { should be(true) }
9
- end
10
-
11
- context "when set to false" do
12
- let(:complex) { false }
13
- it { should be(false) }
14
- end
15
- end
@@ -1,7 +0,0 @@
1
- shared_examples_for 'Attribute.options' do
2
- specify { described_class.should respond_to(:options) }
3
-
4
- it 'returns a hash with options' do
5
- described_class.options.should be_instance_of(Hash)
6
- end
7
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute do
4
- describe '#typecast_to_primitive' do
5
- let(:attribute) { Virtus::Attribute.new(:name) }
6
- let(:value) { 'value' }
7
-
8
- it "returns original value" do
9
- attribute.typecast_to_primitive(value).should eql(value)
10
- end
11
- end
12
- end