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.
Files changed (268) hide show
  1. data/.rspec +1 -1
  2. data/.ruby-gemset +1 -0
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +6 -8
  5. data/Gemfile +8 -2
  6. data/Gemfile.devtools +24 -30
  7. data/Guardfile +17 -6
  8. data/README.md +215 -77
  9. data/TODO.md +6 -0
  10. data/config/flay.yml +2 -2
  11. data/config/flog.yml +1 -1
  12. data/config/mutant.yml +12 -1
  13. data/config/reek.yml +57 -66
  14. data/lib/virtus.rb +126 -42
  15. data/lib/virtus/attribute.rb +52 -240
  16. data/lib/virtus/attribute/accessor.rb +91 -0
  17. data/lib/virtus/attribute/boolean.rb +11 -8
  18. data/lib/virtus/attribute/builder.rb +120 -0
  19. data/lib/virtus/attribute/coercer.rb +51 -0
  20. data/lib/virtus/attribute/coercible.rb +14 -0
  21. data/lib/virtus/attribute/collection.rb +44 -90
  22. data/lib/virtus/attribute/default_value/from_callable.rb +1 -1
  23. data/lib/virtus/attribute/default_value/from_clonable.rb +1 -6
  24. data/lib/virtus/attribute/default_value/from_symbol.rb +3 -3
  25. data/lib/virtus/attribute/embedded_value.rb +37 -28
  26. data/lib/virtus/attribute/hash.rb +69 -71
  27. data/lib/virtus/attribute/lazy_default.rb +18 -0
  28. data/lib/virtus/attribute/strict.rb +16 -0
  29. data/lib/virtus/attribute_set.rb +81 -9
  30. data/lib/virtus/class_inclusions.rb +24 -17
  31. data/lib/virtus/class_methods.rb +13 -31
  32. data/lib/virtus/configuration.rb +77 -0
  33. data/lib/virtus/const_missing_extensions.rb +16 -0
  34. data/lib/virtus/extensions.rb +86 -61
  35. data/lib/virtus/instance_methods.rb +107 -128
  36. data/lib/virtus/model.rb +68 -0
  37. data/lib/virtus/module_builder.rb +184 -0
  38. data/lib/virtus/module_extensions.rb +27 -15
  39. data/lib/virtus/support/equalizer.rb +2 -3
  40. data/lib/virtus/support/type_lookup.rb +1 -1
  41. data/lib/virtus/value_object.rb +24 -31
  42. data/lib/virtus/version.rb +1 -1
  43. data/spec/integration/building_module_spec.rb +68 -0
  44. data/spec/integration/custom_attributes_spec.rb +8 -15
  45. data/spec/integration/custom_collection_attributes_spec.rb +101 -0
  46. data/spec/integration/default_values_spec.rb +9 -2
  47. data/spec/integration/defining_attributes_spec.rb +2 -2
  48. data/spec/integration/using_modules_spec.rb +5 -3
  49. data/spec/shared/freeze_method_behavior.rb +2 -2
  50. data/spec/shared/options_class_method.rb +1 -1
  51. data/spec/spec_helper.rb +25 -5
  52. data/spec/unit/virtus/attribute/boolean/value_coerced_predicate_spec.rb +25 -0
  53. data/spec/unit/virtus/attribute/class_methods/build_spec.rb +115 -22
  54. data/spec/unit/virtus/attribute/class_methods/coerce_spec.rb +32 -0
  55. data/spec/unit/virtus/attribute/coerce_spec.rb +38 -21
  56. data/spec/unit/virtus/attribute/coercible_predicate_spec.rb +20 -0
  57. data/spec/unit/virtus/attribute/collection/class_methods/build_spec.rb +84 -0
  58. data/spec/unit/virtus/attribute/collection/coerce_spec.rb +37 -24
  59. data/spec/unit/virtus/attribute/custom_collection_spec.rb +23 -0
  60. data/spec/unit/virtus/attribute/embedded_value/class_methods/build_spec.rb +37 -0
  61. data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +46 -10
  62. data/spec/unit/virtus/attribute/get_spec.rb +15 -63
  63. data/spec/unit/virtus/attribute/hash/class_methods/build_spec.rb +80 -0
  64. data/spec/unit/virtus/attribute/hash/coerce_spec.rb +75 -13
  65. data/spec/unit/virtus/attribute/lazy_predicate_spec.rb +20 -0
  66. data/spec/unit/virtus/attribute/set_default_value_spec.rb +74 -0
  67. data/spec/unit/virtus/attribute/set_spec.rb +16 -36
  68. data/spec/unit/virtus/attribute/value_coerced_predicate_spec.rb +19 -0
  69. data/spec/unit/virtus/attribute_set/append_spec.rb +3 -5
  70. data/spec/unit/virtus/attribute_set/define_reader_method_spec.rb +1 -1
  71. data/spec/unit/virtus/attribute_set/define_writer_method_spec.rb +1 -1
  72. data/spec/unit/virtus/attribute_set/each_spec.rb +3 -3
  73. data/spec/unit/virtus/attribute_set/element_reference_spec.rb +1 -1
  74. data/spec/unit/virtus/attribute_set/element_set_spec.rb +4 -4
  75. data/spec/unit/virtus/attribute_set/merge_spec.rb +10 -16
  76. data/spec/unit/virtus/attribute_set/reset_spec.rb +6 -10
  77. data/spec/unit/virtus/attribute_spec.rb +222 -0
  78. data/spec/unit/virtus/attributes_reader_spec.rb +41 -0
  79. data/spec/unit/virtus/attributes_writer_spec.rb +51 -0
  80. data/spec/unit/virtus/class_methods/new_spec.rb +39 -0
  81. data/spec/unit/virtus/config_spec.rb +13 -0
  82. data/spec/unit/virtus/element_reader_spec.rb +21 -0
  83. data/spec/unit/virtus/element_writer_spec.rb +19 -0
  84. data/spec/unit/virtus/freeze_spec.rb +21 -0
  85. data/spec/unit/virtus/model_spec.rb +134 -0
  86. data/spec/unit/virtus/module_spec.rb +76 -0
  87. data/spec/unit/virtus/set_default_attributes_spec.rb +25 -0
  88. data/spec/unit/virtus/value_object_spec.rb +105 -0
  89. data/virtus.gemspec +4 -2
  90. metadata +82 -191
  91. data/.rvmrc +0 -1
  92. data/TODO +0 -1
  93. data/config/roodi.yml +0 -17
  94. data/lib/virtus/attribute/array.rb +0 -24
  95. data/lib/virtus/attribute/class.rb +0 -21
  96. data/lib/virtus/attribute/date.rb +0 -34
  97. data/lib/virtus/attribute/date_time.rb +0 -36
  98. data/lib/virtus/attribute/decimal.rb +0 -21
  99. data/lib/virtus/attribute/embedded_value/from_open_struct.rb +0 -19
  100. data/lib/virtus/attribute/embedded_value/from_struct.rb +0 -19
  101. data/lib/virtus/attribute/float.rb +0 -30
  102. data/lib/virtus/attribute/integer.rb +0 -27
  103. data/lib/virtus/attribute/numeric.rb +0 -11
  104. data/lib/virtus/attribute/object.rb +0 -11
  105. data/lib/virtus/attribute/set.rb +0 -24
  106. data/lib/virtus/attribute/string.rb +0 -24
  107. data/lib/virtus/attribute/symbol.rb +0 -21
  108. data/lib/virtus/attribute/time.rb +0 -36
  109. data/lib/virtus/coercion.rb +0 -32
  110. data/lib/virtus/coercion/array.rb +0 -23
  111. data/lib/virtus/coercion/date.rb +0 -26
  112. data/lib/virtus/coercion/date_time.rb +0 -26
  113. data/lib/virtus/coercion/decimal.rb +0 -40
  114. data/lib/virtus/coercion/false_class.rb +0 -24
  115. data/lib/virtus/coercion/float.rb +0 -39
  116. data/lib/virtus/coercion/hash.rb +0 -71
  117. data/lib/virtus/coercion/integer.rb +0 -83
  118. data/lib/virtus/coercion/numeric.rb +0 -66
  119. data/lib/virtus/coercion/object.rb +0 -125
  120. data/lib/virtus/coercion/string.rb +0 -218
  121. data/lib/virtus/coercion/symbol.rb +0 -24
  122. data/lib/virtus/coercion/time.rb +0 -40
  123. data/lib/virtus/coercion/time_coercions.rb +0 -86
  124. data/lib/virtus/coercion/true_class.rb +0 -24
  125. data/spec/unit/virtus/attribute/array/coerce_spec.rb +0 -13
  126. data/spec/unit/virtus/attribute/boolean/coerce_spec.rb +0 -85
  127. data/spec/unit/virtus/attribute/boolean/define_reader_method_spec.rb +0 -15
  128. data/spec/unit/virtus/attribute/boolean/value_coerced_spec.rb +0 -97
  129. data/spec/unit/virtus/attribute/boolean_spec.rb +0 -19
  130. data/spec/unit/virtus/attribute/class/coerce_spec.rb +0 -13
  131. data/spec/unit/virtus/attribute/class_methods/accessor_spec.rb +0 -12
  132. data/spec/unit/virtus/attribute/class_methods/coercion_method_spec.rb +0 -9
  133. data/spec/unit/virtus/attribute/class_methods/default_spec.rb +0 -9
  134. data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +0 -117
  135. data/spec/unit/virtus/attribute/class_methods/merge_options_spec.rb +0 -11
  136. data/spec/unit/virtus/attribute/class_methods/primitive_spec.rb +0 -9
  137. data/spec/unit/virtus/attribute/class_methods/reader_spec.rb +0 -9
  138. data/spec/unit/virtus/attribute/class_methods/writer_spec.rb +0 -9
  139. data/spec/unit/virtus/attribute/coercion_method_spec.rb +0 -12
  140. data/spec/unit/virtus/attribute/collection/class_methods/merge_options_spec.rb +0 -40
  141. data/spec/unit/virtus/attribute/collection/coerce_and_append_member_spec.rb +0 -16
  142. data/spec/unit/virtus/attribute/collection/member_coercion/coerce_and_append_member_spec.rb +0 -18
  143. data/spec/unit/virtus/attribute/collection/member_type_spec.rb +0 -17
  144. data/spec/unit/virtus/attribute/collection/new_collection_spec.rb +0 -9
  145. data/spec/unit/virtus/attribute/date/coerce_spec.rb +0 -47
  146. data/spec/unit/virtus/attribute/date/value_coerced_spec.rb +0 -46
  147. data/spec/unit/virtus/attribute/date_time/coerce_spec.rb +0 -80
  148. data/spec/unit/virtus/attribute/decimal/coerce_spec.rb +0 -123
  149. data/spec/unit/virtus/attribute/default_spec.rb +0 -28
  150. data/spec/unit/virtus/attribute/default_value/call_spec.rb +0 -21
  151. data/spec/unit/virtus/attribute/default_value/class_methods/build_spec.rb +0 -23
  152. data/spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb +0 -21
  153. data/spec/unit/virtus/attribute/default_value/from_callable/call_spec.rb +0 -19
  154. data/spec/unit/virtus/attribute/default_value/from_callable/class_methods/handle_spec.rb +0 -17
  155. data/spec/unit/virtus/attribute/default_value/from_clonable/call_spec.rb +0 -20
  156. data/spec/unit/virtus/attribute/default_value/from_clonable/class_methods/handle_spec.rb +0 -19
  157. data/spec/unit/virtus/attribute/default_value/from_symbol/call_spec.rb +0 -17
  158. data/spec/unit/virtus/attribute/default_value/from_symbol/class_methods/handle_spec.rb +0 -17
  159. data/spec/unit/virtus/attribute/default_value/value_spec.rb +0 -10
  160. data/spec/unit/virtus/attribute/define_accessor_methods_spec.rb +0 -26
  161. data/spec/unit/virtus/attribute/define_reader_method_spec.rb +0 -24
  162. data/spec/unit/virtus/attribute/define_writer_method_spec.rb +0 -24
  163. data/spec/unit/virtus/attribute/embedded_value/class_methods/determine_type_spec.rb +0 -23
  164. data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +0 -17
  165. data/spec/unit/virtus/attribute/embedded_value/from_open_struct/coerce_spec.rb +0 -34
  166. data/spec/unit/virtus/attribute/embedded_value/from_struct/coerce_spec.rb +0 -34
  167. data/spec/unit/virtus/attribute/float/coerce_spec.rb +0 -123
  168. data/spec/unit/virtus/attribute/hash/class_methods/merge_options_spec.rb +0 -33
  169. data/spec/unit/virtus/attribute/hash/key_type_spec.rb +0 -10
  170. data/spec/unit/virtus/attribute/hash/value_type_spec.rb +0 -10
  171. data/spec/unit/virtus/attribute/inspect_spec.rb +0 -27
  172. data/spec/unit/virtus/attribute/integer/coerce_spec.rb +0 -117
  173. data/spec/unit/virtus/attribute/name_spec.rb +0 -12
  174. data/spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb +0 -15
  175. data/spec/unit/virtus/attribute/numeric/class_methods/max_spec.rb +0 -9
  176. data/spec/unit/virtus/attribute/numeric/class_methods/min_spec.rb +0 -9
  177. data/spec/unit/virtus/attribute/options_spec.rb +0 -14
  178. data/spec/unit/virtus/attribute/public_reader_spec.rb +0 -24
  179. data/spec/unit/virtus/attribute/public_writer_spec.rb +0 -24
  180. data/spec/unit/virtus/attribute/set/coerce_spec.rb +0 -13
  181. data/spec/unit/virtus/attribute/string/coerce_spec.rb +0 -11
  182. data/spec/unit/virtus/attribute/symbol/coerce_spec.rb +0 -13
  183. data/spec/unit/virtus/attribute/time/coerce_spec.rb +0 -67
  184. data/spec/unit/virtus/attribute/value_coerced_spec.rb +0 -19
  185. data/spec/unit/virtus/class_methods/attribute_set_spec.rb +0 -9
  186. data/spec/unit/virtus/class_methods/attribute_spec.rb +0 -30
  187. data/spec/unit/virtus/class_methods/attributes_spec.rb +0 -22
  188. data/spec/unit/virtus/class_methods/const_missing_spec.rb +0 -27
  189. data/spec/unit/virtus/class_methods/inherited_spec.rb +0 -21
  190. data/spec/unit/virtus/coercion/array/class_methods/to_set_spec.rb +0 -12
  191. data/spec/unit/virtus/coercion/class_methods/element_reference_spec.rb +0 -17
  192. data/spec/unit/virtus/coercion/class_methods/primitive_spec.rb +0 -13
  193. data/spec/unit/virtus/coercion/date/class_methods/to_date_spec.rb +0 -10
  194. data/spec/unit/virtus/coercion/date/class_methods/to_datetime_spec.rb +0 -30
  195. data/spec/unit/virtus/coercion/date/class_methods/to_string_spec.rb +0 -12
  196. data/spec/unit/virtus/coercion/date/class_methods/to_time_spec.rb +0 -12
  197. data/spec/unit/virtus/coercion/date_time/class_methods/to_date_spec.rb +0 -30
  198. data/spec/unit/virtus/coercion/date_time/class_methods/to_datetime_spec.rb +0 -10
  199. data/spec/unit/virtus/coercion/date_time/class_methods/to_string_spec.rb +0 -12
  200. data/spec/unit/virtus/coercion/date_time/class_methods/to_time_spec.rb +0 -30
  201. data/spec/unit/virtus/coercion/decimal/class_methods/to_decimal_spec.rb +0 -9
  202. data/spec/unit/virtus/coercion/decimal/class_methods/to_float_spec.rb +0 -12
  203. data/spec/unit/virtus/coercion/decimal/class_methods/to_integer_spec.rb +0 -12
  204. data/spec/unit/virtus/coercion/decimal/class_methods/to_string_spec.rb +0 -12
  205. data/spec/unit/virtus/coercion/false_class/class_methods/to_string_spec.rb +0 -12
  206. data/spec/unit/virtus/coercion/float/class_methods/to_decimal_spec.rb +0 -12
  207. data/spec/unit/virtus/coercion/float/class_methods/to_float_spec.rb +0 -9
  208. data/spec/unit/virtus/coercion/float/class_methods/to_integer_spec.rb +0 -12
  209. data/spec/unit/virtus/coercion/float/class_methods/to_string_spec.rb +0 -12
  210. data/spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb +0 -38
  211. data/spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb +0 -38
  212. data/spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb +0 -38
  213. data/spec/unit/virtus/coercion/integer/class_methods/to_boolean_spec.rb +0 -25
  214. data/spec/unit/virtus/coercion/integer/class_methods/to_decimal_spec.rb +0 -12
  215. data/spec/unit/virtus/coercion/integer/class_methods/to_float_spec.rb +0 -12
  216. data/spec/unit/virtus/coercion/integer/class_methods/to_integer_spec.rb +0 -9
  217. data/spec/unit/virtus/coercion/integer/class_methods/to_string_spec.rb +0 -12
  218. data/spec/unit/virtus/coercion/numeric/class_methods/to_decimal_spec.rb +0 -10
  219. data/spec/unit/virtus/coercion/numeric/class_methods/to_float_spec.rb +0 -10
  220. data/spec/unit/virtus/coercion/numeric/class_methods/to_integer_spec.rb +0 -10
  221. data/spec/unit/virtus/coercion/numeric/class_methods/to_string_spec.rb +0 -12
  222. data/spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb +0 -33
  223. data/spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb +0 -51
  224. data/spec/unit/virtus/coercion/object/class_methods/to_hash_spec.rb +0 -22
  225. data/spec/unit/virtus/coercion/object/class_methods/to_integer_spec.rb +0 -22
  226. data/spec/unit/virtus/coercion/object/class_methods/to_string_spec.rb +0 -22
  227. data/spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb +0 -29
  228. data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +0 -49
  229. data/spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb +0 -23
  230. data/spec/unit/virtus/coercion/string/class_methods/to_datetime_spec.rb +0 -50
  231. data/spec/unit/virtus/coercion/string/class_methods/to_decimal_spec.rb +0 -47
  232. data/spec/unit/virtus/coercion/string/class_methods/to_float_spec.rb +0 -57
  233. data/spec/unit/virtus/coercion/string/class_methods/to_integer_spec.rb +0 -68
  234. data/spec/unit/virtus/coercion/string/class_methods/to_symbol_spec.rb +0 -9
  235. data/spec/unit/virtus/coercion/string/class_methods/to_time_spec.rb +0 -50
  236. data/spec/unit/virtus/coercion/symbol/class_methods/to_string_spec.rb +0 -12
  237. data/spec/unit/virtus/coercion/time/class_methods/to_integer_spec.rb +0 -10
  238. data/spec/unit/virtus/coercion/time/class_methods/to_time_spec.rb +0 -10
  239. data/spec/unit/virtus/coercion/time_coercions/to_date_spec.rb +0 -33
  240. data/spec/unit/virtus/coercion/time_coercions/to_datetime_spec.rb +0 -33
  241. data/spec/unit/virtus/coercion/time_coercions/to_string_spec.rb +0 -18
  242. data/spec/unit/virtus/coercion/time_coercions/to_time_spec.rb +0 -33
  243. data/spec/unit/virtus/coercion/true_class/class_methods/to_string_spec.rb +0 -12
  244. data/spec/unit/virtus/equalizer/append_spec.rb +0 -82
  245. data/spec/unit/virtus/equalizer/class_method/new_spec.rb +0 -111
  246. data/spec/unit/virtus/equalizer/methods/eql_spec.rb +0 -47
  247. data/spec/unit/virtus/equalizer/methods/equal_value_spec.rb +0 -57
  248. data/spec/unit/virtus/extensions/allowed_writer_methods_spec.rb +0 -25
  249. data/spec/unit/virtus/extensions/attribute_spec.rb +0 -26
  250. data/spec/unit/virtus/instance_methods/attributes_spec.rb +0 -140
  251. data/spec/unit/virtus/instance_methods/element_reference_spec.rb +0 -24
  252. data/spec/unit/virtus/instance_methods/element_set_spec.rb +0 -28
  253. data/spec/unit/virtus/instance_methods/freeze_spec.rb +0 -64
  254. data/spec/unit/virtus/instance_methods/initialize_spec.rb +0 -48
  255. data/spec/unit/virtus/instance_methods/to_hash_spec.rb +0 -23
  256. data/spec/unit/virtus/module_extensions/attribute_spec.rb +0 -31
  257. data/spec/unit/virtus/options/accept_options_spec.rb +0 -37
  258. data/spec/unit/virtus/options/accepted_options_spec.rb +0 -21
  259. data/spec/unit/virtus/options/options_spec.rb +0 -34
  260. data/spec/unit/virtus/type_lookup/class_methods/extended_spec.rb +0 -9
  261. data/spec/unit/virtus/type_lookup/determine_type_spec.rb +0 -81
  262. data/spec/unit/virtus/type_lookup/primitive_spec.rb +0 -9
  263. data/spec/unit/virtus/value_object/class_methods/allowed_writer_methods_spec.rb +0 -15
  264. data/spec/unit/virtus/value_object/class_methods/attribute_spec.rb +0 -50
  265. data/spec/unit/virtus/value_object/class_methods/equalizer_spec.rb +0 -24
  266. data/spec/unit/virtus/value_object/initialize_spec.rb +0 -19
  267. data/spec/unit/virtus/value_object/instance_methods/clone_spec.rb +0 -21
  268. data/spec/unit/virtus/value_object/instance_methods/with_spec.rb +0 -31
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Hash, '.to_time' do
4
- subject { object.to_time(hash) }
5
-
6
- let(:object) { described_class }
7
-
8
- context 'when time segments are missing' do
9
- let(:time_now) { Time.local(2011, 1, 1) }
10
- let(:hash) { {} }
11
-
12
- before do
13
- Time.stub!(:now).and_return(time_now) # freeze time
14
- end
15
-
16
- it { should be_instance_of(Time) }
17
-
18
- it 'uses the Time now to populate the segments' do
19
- should eql(time_now)
20
- end
21
- end
22
-
23
- context 'when time segments are integers' do
24
- let(:hash) { { :year => 2011, :month => 1, :day => 1, :hour => 1, :min => 1, :sec => 1 } }
25
-
26
- it { should be_instance_of(Time) }
27
-
28
- it { should eql(Time.local(2011, 1, 1, 1, 1, 1)) }
29
- end
30
-
31
- context 'when time segments are strings' do
32
- let(:hash) { { :year => '2011', :month => '1', :day => '1', :hour => '1', :min => '1', :sec => '1' } }
33
-
34
- it { should be_instance_of(Time) }
35
-
36
- it { should eql(Time.local(2011, 1, 1, 1, 1, 1)) }
37
- end
38
- end
@@ -1,25 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Integer, '.to_boolean' do
4
- subject { object.to_boolean(fixnum) }
5
-
6
- let(:object) { described_class }
7
-
8
- context 'when the fixnum is 1' do
9
- let(:fixnum) { 1 }
10
-
11
- it { should be(true) }
12
- end
13
-
14
- context 'when the fixnum is 0' do
15
- let(:fixnum) { 0 }
16
-
17
- it { should be(false) }
18
- end
19
-
20
- context 'when the fixnum is not 1 or 0' do
21
- let(:fixnum) { -1 }
22
-
23
- it { should be(-1) }
24
- end
25
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Integer, '.to_decimal' do
4
- subject { object.to_decimal(fixnum) }
5
-
6
- let(:object) { described_class }
7
- let(:fixnum) { 1 }
8
-
9
- it { should be_instance_of(BigDecimal) }
10
-
11
- it { should eql(BigDecimal('1.0')) }
12
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Integer, '.to_float' do
4
- subject { object.to_float(fixnum) }
5
-
6
- let(:object) { described_class }
7
- let(:fixnum) { 1 }
8
-
9
- it { should be_instance_of(Float) }
10
-
11
- it { should eql(1.0) }
12
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Integer, '.to_integer' do
4
- subject { described_class.to_integer(value) }
5
-
6
- let(:value) { 1 }
7
-
8
- it { should be(value) }
9
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Integer, '.to_string' do
4
- subject { object.to_string(integer) }
5
-
6
- let(:object) { described_class }
7
- let(:integer) { 1 }
8
-
9
- it { should be_instance_of(String) }
10
-
11
- it { should eql('1') }
12
- end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Numeric, '.to_decimal' do
4
- subject { object.to_decimal(numeric) }
5
-
6
- let(:object) { described_class }
7
- let(:numeric) { Rational(2, 2) }
8
-
9
- it { should eql(BigDecimal('1.0')) }
10
- end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Numeric, '.to_float' do
4
- subject { object.to_float(numeric) }
5
-
6
- let(:object) { described_class }
7
- let(:numeric) { Rational(2, 2) }
8
-
9
- it { should eql(1.0) }
10
- end
@@ -1,10 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Numeric, '.to_integer' do
4
- subject { object.to_integer(numeric) }
5
-
6
- let(:object) { described_class }
7
- let(:numeric) { Rational(2, 2) }
8
-
9
- it { should eql(1) }
10
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Numeric, '.to_string' do
4
- subject { object.to_string(numeric) }
5
-
6
- let(:object) { described_class }
7
- let(:numeric) { Rational(2, 2) }
8
-
9
- let(:coerced_value) { RUBY_VERSION < '1.9' ? '1' : '1/1' }
10
-
11
- it { should eql(coerced_value) }
12
- end
@@ -1,33 +0,0 @@
1
- require 'spec_helper'
2
-
3
- shared_examples_for 'no method error' do
4
- specify do
5
- expect { subject }.to raise_error(NoMethodError)
6
- end
7
- end
8
-
9
- describe Virtus::Coercion::Object, '.method_missing' do
10
- subject { described_class.send(method_name, value) }
11
-
12
- let(:value) { '1' }
13
-
14
- context 'with a non-typecast method' do
15
- let(:method_name) { 'not_here' }
16
-
17
- it_should_behave_like 'no method error'
18
- end
19
-
20
- Virtus::Attribute::Object.descendants.each do |attribute|
21
- let(:method_name) { attribute.coercion_method }
22
-
23
- context "with #{attribute.coercion_method.inspect} and an input value" do
24
- it { should equal(value) }
25
- end
26
-
27
- context "with #{attribute.coercion_method.inspect} and without an input value" do
28
- subject { described_class.send(method_name) }
29
-
30
- it_should_behave_like 'no method error'
31
- end
32
- end
33
- end
@@ -1,51 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Object, '.to_array' do
4
- subject { object.to_array(value) }
5
-
6
- let(:object) { described_class }
7
- let(:value) { Object.new }
8
- let(:coerced) { [ value ] }
9
-
10
- context 'when the value responds to #to_ary' do
11
- before do
12
- value.should_receive(:to_ary).with().and_return(coerced)
13
- end
14
-
15
- it { should be(coerced) }
16
-
17
- it 'does not call #to_a if #to_ary is available' do
18
- value.should_not_receive(:to_a)
19
- subject
20
- end
21
- end
22
-
23
- context 'when the value responds to #to_a but not #to_ary' do
24
- before do
25
- value.should_receive(:to_a).with().and_return(coerced)
26
- end
27
-
28
- it { should be(coerced) }
29
- end
30
-
31
- context 'when the value does not respond to #to_ary or #to_a' do
32
- it { should be_instance_of(Array) }
33
-
34
- it { should == coerced }
35
- end
36
-
37
- context 'when the value returns nil from #to_ary' do
38
- before do
39
- value.should_receive(:to_ary).with().and_return(nil)
40
- end
41
-
42
- it 'calls #to_a as a fallback' do
43
- value.should_receive(:to_a).with().and_return(coerced)
44
- should be(coerced)
45
- end
46
-
47
- it 'wraps the value in an Array if #to_a is not available' do
48
- should == coerced
49
- end
50
- end
51
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Object, '.to_hash' do
4
- subject { object.to_hash(value) }
5
-
6
- let(:object) { described_class }
7
- let(:value) { stub('value') }
8
-
9
- context 'when the value responds to #to_hash' do
10
- let(:coerced) { stub('coerced') }
11
-
12
- before do
13
- value.should_receive(:to_hash).with().and_return(coerced)
14
- end
15
-
16
- it { should be(coerced) }
17
- end
18
-
19
- context 'when the value does not respond to #to_hash' do
20
- it { should be(value) }
21
- end
22
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Object, '.to_integer' do
4
- subject { object.to_integer(value) }
5
-
6
- let(:object) { described_class }
7
- let(:value) { stub('value') }
8
-
9
- context 'when the value responds to #to_int' do
10
- let(:coerced) { stub('coerced') }
11
-
12
- before do
13
- value.should_receive(:to_int).with().and_return(coerced)
14
- end
15
-
16
- it { should be(coerced) }
17
- end
18
-
19
- context 'when the value does not respond to #to_int' do
20
- it { should be(value) }
21
- end
22
- end
@@ -1,22 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Object, '.to_string' do
4
- subject { object.to_string(value) }
5
-
6
- let(:object) { described_class }
7
- let(:value) { Object.new }
8
-
9
- context 'when the value responds to #to_str' do
10
- let(:coerced) { stub('coerced') }
11
-
12
- before do
13
- value.should_receive(:to_str).with().and_return(coerced)
14
- end
15
-
16
- it { should be(coerced) }
17
- end
18
-
19
- context 'when the value does not respond to #to_str' do
20
- it { should be(value) }
21
- end
22
- end
@@ -1,29 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::String, '.to_boolean' do
4
- subject { object.to_boolean(string) }
5
-
6
- let(:object) { described_class }
7
-
8
- %w[ 1 on ON t true T TRUE y yes Y YES ].each do |value|
9
- context "with #{value.inspect}" do
10
- let(:string) { value }
11
-
12
- it { should be(true) }
13
- end
14
- end
15
-
16
- %w[ 0 off OFF f false F FALSE n no N NO ].each do |value|
17
- context "with #{value.inspect}" do
18
- let(:string) { value }
19
-
20
- it { should be(false) }
21
- end
22
- end
23
-
24
- context 'with an invalid boolean string' do
25
- let(:string) { 'non-boolean' }
26
-
27
- it { should equal(string) }
28
- end
29
- end
@@ -1,49 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::String, '.to_constant' do
4
- subject { object.to_constant(string) }
5
-
6
- let(:object) { described_class }
7
-
8
- context 'with a non-namespaced name' do
9
- let(:string) { 'String' }
10
-
11
- it { should be(String) }
12
- end
13
-
14
- context 'with a non-namespaced qualified name' do
15
- let(:string) { '::String' }
16
-
17
- it { should be(String) }
18
- end
19
-
20
- context 'with a namespaced name' do
21
- let(:string) { 'Virtus::Coercion::String' }
22
-
23
- it { should be(Virtus::Coercion::String) }
24
- end
25
-
26
- context 'with a namespaced qualified name' do
27
- let(:string) { '::Virtus::Coercion::String' }
28
-
29
- it { should be(Virtus::Coercion::String) }
30
- end
31
-
32
- context 'with a name outside of the namespace' do
33
- let(:string) { 'Virtus::Object' }
34
-
35
- specify { expect { subject }.to raise_error(NameError) }
36
- end
37
-
38
- context 'when the name is unknown' do
39
- let(:string) { 'Unknown' }
40
-
41
- specify { expect { subject }.to raise_error(NameError) }
42
- end
43
-
44
- context 'when the name is invalid' do
45
- let(:string) { 'invalid' }
46
-
47
- specify { expect { subject }.to raise_error(NameError) }
48
- end
49
- end
@@ -1,23 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::String, '.to_date' do
4
- subject { object.to_date(string) }
5
-
6
- let(:object) { described_class }
7
-
8
- context 'with a valid date string' do
9
- let(:string) { 'July, 22th, 2011' }
10
-
11
- it { should be_instance_of(Date) }
12
-
13
- its(:year) { should == 2011 }
14
- its(:month) { should == 7 }
15
- its(:day) { should == 22 }
16
- end
17
-
18
- context 'with an invalid date string' do
19
- let(:string) { 'non-date' }
20
-
21
- it { should equal(string) }
22
- end
23
- end
@@ -1,50 +0,0 @@
1
- require 'spec_helper'
2
-
3
- shared_examples_for 'a correct datetime object' do
4
- it { should be_instance_of(DateTime) }
5
-
6
- its(:year) { should == year }
7
- its(:month) { should == month }
8
- its(:day) { should == day }
9
- its(:hour) { should == hour }
10
- its(:min) { should == min }
11
- its(:sec) { should == sec }
12
- end
13
-
14
- describe Virtus::Coercion::String, '.to_datetime' do
15
- subject { object.to_datetime(string) }
16
-
17
- let(:object) { described_class }
18
-
19
- context 'with a valid date string' do
20
- let(:year) { 2011 }
21
- let(:month) { 7 }
22
- let(:day) { 22 }
23
-
24
- context 'not including time part' do
25
- let(:string) { "July, #{day}th, #{year}" }
26
-
27
- let(:hour) { 0 }
28
- let(:min) { 0 }
29
- let(:sec) { 0 }
30
-
31
- it_should_behave_like 'a correct datetime object'
32
- end
33
-
34
- context 'including time part' do
35
- let(:string) { "July, #{day}, #{year}, #{hour}:#{min}:#{sec}" }
36
-
37
- let(:hour) { 13 }
38
- let(:min) { 44 }
39
- let(:sec) { 50 }
40
-
41
- it_should_behave_like 'a correct datetime object'
42
- end
43
- end
44
-
45
- context 'with an invalid date time string' do
46
- let(:string) { 'non-datetime' }
47
-
48
- it { should equal(string) }
49
- end
50
- end