virtus 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/.pelusa.yml +7 -0
  2. data/.travis.yml +5 -3
  3. data/Changelog.md +17 -0
  4. data/Gemfile +4 -0
  5. data/README.md +35 -39
  6. data/TODO +12 -7
  7. data/config/flay.yml +1 -1
  8. data/config/flog.yml +1 -1
  9. data/lib/virtus.rb +8 -0
  10. data/lib/virtus/attribute.rb +8 -29
  11. data/lib/virtus/attribute/boolean.rb +1 -1
  12. data/lib/virtus/attribute/default_value.rb +15 -45
  13. data/lib/virtus/attribute/default_value/from_callable.rb +33 -0
  14. data/lib/virtus/attribute/default_value/from_clonable.rb +40 -0
  15. data/lib/virtus/attribute/default_value/from_symbol.rb +35 -0
  16. data/lib/virtus/attribute/embedded_value.rb +3 -14
  17. data/lib/virtus/class_methods.rb +17 -0
  18. data/lib/virtus/coercion/hash.rb +0 -11
  19. data/lib/virtus/coercion/object.rb +98 -0
  20. data/lib/virtus/coercion/string.rb +9 -2
  21. data/lib/virtus/coercion/time_coercions.rb +2 -5
  22. data/lib/virtus/instance_methods.rb +16 -37
  23. data/lib/virtus/support/type_lookup.rb +1 -2
  24. data/lib/virtus/value_object.rb +31 -8
  25. data/lib/virtus/value_object/equalizer.rb +21 -26
  26. data/lib/virtus/version.rb +1 -1
  27. data/spec/integration/custom_attributes_spec.rb +1 -1
  28. data/spec/integration/default_values_spec.rb +15 -3
  29. data/spec/integration/defining_attributes_spec.rb +1 -1
  30. data/spec/integration/mass_assignment_with_accessors_spec.rb +44 -0
  31. data/spec/integration/virtus/value_object_spec.rb +2 -2
  32. data/spec/spec_helper.rb +8 -1
  33. data/spec/unit/virtus/attribute/class_methods/determine_type_spec.rb +1 -1
  34. data/spec/unit/virtus/attribute/default_spec.rb +1 -1
  35. data/spec/unit/virtus/attribute/default_value/evaluate_spec.rb +25 -11
  36. data/spec/unit/virtus/attribute/embedded_value/class_methods/merge_options_spec.rb +1 -1
  37. data/spec/unit/virtus/attribute/embedded_value/coerce_spec.rb +1 -1
  38. data/spec/unit/virtus/class_methods/allowed_writer_methods_spec.rb +25 -0
  39. data/spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb +51 -0
  40. data/spec/unit/virtus/coercion/object/class_methods/to_hash_spec.rb +22 -0
  41. data/spec/unit/virtus/coercion/object/class_methods/to_integer_spec.rb +22 -0
  42. data/spec/unit/virtus/coercion/object/class_methods/to_string_spec.rb +22 -0
  43. data/spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb +37 -1
  44. data/spec/unit/virtus/instance_methods/attributes_spec.rb +14 -2
  45. data/spec/unit/virtus/value_object/class_methods/allowed_writer_methods_spec.rb +15 -0
  46. data/spec/unit/virtus/value_object/class_methods/equalizer_spec.rb +1 -1
  47. data/spec/unit/virtus/value_object/initialize_spec.rb +1 -1
  48. data/spec/unit/virtus/value_object/with_spec.rb +1 -1
  49. metadata +17 -10
  50. data/spec/unit/virtus/attribute/default_value/instance_methods/evaluate_spec.rb +0 -30
  51. data/spec/unit/virtus/attribute/instance_variable_name_spec.rb +0 -12
  52. data/spec/unit/virtus/attribute/reader_visibility_spec.rb +0 -24
  53. data/spec/unit/virtus/attribute/writer_visibility_spec.rb +0 -24
  54. data/spec/unit/virtus/coercion/hash/class_methods/to_array_spec.rb +0 -12
@@ -0,0 +1,22 @@
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
@@ -0,0 +1,22 @@
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) { stub('value') }
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
@@ -5,9 +5,45 @@ describe Virtus::Coercion::String, '.to_constant' do
5
5
 
6
6
  let(:object) { described_class }
7
7
 
8
- context 'with a String' do
8
+ context 'with a non-namespaced name' do
9
9
  let(:string) { 'String' }
10
10
 
11
11
  it { should be(String) }
12
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
13
49
  end
@@ -10,6 +10,14 @@ describe Virtus::InstanceMethods do
10
10
  attribute :email, String, :accessor => :private
11
11
 
12
12
  attr_accessor :non_attribute
13
+
14
+ def private_non_attribute_reader
15
+ @private_non_attribute
16
+ end
17
+
18
+ private
19
+
20
+ attr_accessor :private_non_attribute
13
21
  end
14
22
  end
15
23
 
@@ -77,8 +85,12 @@ describe Virtus::InstanceMethods do
77
85
  object.attributes.should eql(attributes)
78
86
  end
79
87
 
80
- it 'silently ignores publicly accessible non-Virtus attributes' do
81
- expect { subject }.to_not change { object.non_attribute }
88
+ it 'sets publicly accessible non-Virtus attributes' do
89
+ expect { subject }.to change { object.non_attribute }
90
+ end
91
+
92
+ it 'silently ignores private non-Virtus attributes' do
93
+ expect { subject }.to_not change { object.private_non_attribute_reader }
82
94
  end
83
95
  end
84
96
 
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Virtus::ValueObject::ClassMethods, '#allowed_writer_methods' do
4
+ subject { object.allowed_writer_methods }
5
+
6
+ let(:object) do
7
+ Class.new do
8
+ include Virtus::ValueObject
9
+ attribute :virtus_attribute, String
10
+ end
11
+ end
12
+
13
+ it { should include('virtus_attribute=') }
14
+ it { should be_frozen }
15
+ end
@@ -21,4 +21,4 @@ describe Virtus::ValueObject, '.equalizer' do
21
21
 
22
22
  specify { subject.should be(equalizer) }
23
23
  end
24
- end
24
+ end
@@ -16,4 +16,4 @@ describe Virtus::ValueObject, '#initialize' do
16
16
 
17
17
  its(:currency) { should eql('USD') }
18
18
  its(:amount) { should eql(1) }
19
- end
19
+ end
@@ -28,4 +28,4 @@ describe Virtus::ValueObject, '#with' do
28
28
 
29
29
  its(:first_name) { should eql('John') }
30
30
  its(:last_name) { should eql('Doe') }
31
- end
31
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: virtus
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Piotr Solnica
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-02-08 00:00:00 Z
18
+ date: 2012-02-25 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rake
@@ -78,6 +78,7 @@ extra_rdoc_files:
78
78
  - TODO
79
79
  files:
80
80
  - .gitignore
81
+ - .pelusa.yml
81
82
  - .rvmrc
82
83
  - .travis.yml
83
84
  - .yardopts
@@ -102,6 +103,9 @@ files:
102
103
  - lib/virtus/attribute/date_time.rb
103
104
  - lib/virtus/attribute/decimal.rb
104
105
  - lib/virtus/attribute/default_value.rb
106
+ - lib/virtus/attribute/default_value/from_callable.rb
107
+ - lib/virtus/attribute/default_value/from_clonable.rb
108
+ - lib/virtus/attribute/default_value/from_symbol.rb
105
109
  - lib/virtus/attribute/embedded_value.rb
106
110
  - lib/virtus/attribute/float.rb
107
111
  - lib/virtus/attribute/hash.rb
@@ -143,6 +147,7 @@ files:
143
147
  - spec/integration/default_values_spec.rb
144
148
  - spec/integration/defining_attributes_spec.rb
145
149
  - spec/integration/embedded_value_spec.rb
150
+ - spec/integration/mass_assignment_with_accessors_spec.rb
146
151
  - spec/integration/overriding_virtus_spec.rb
147
152
  - spec/integration/virtus/instance_level_attributes_spec.rb
148
153
  - spec/integration/virtus/value_object_spec.rb
@@ -178,7 +183,6 @@ files:
178
183
  - spec/unit/virtus/attribute/default_value/attribute_spec.rb
179
184
  - spec/unit/virtus/attribute/default_value/class_methods/new_spec.rb
180
185
  - spec/unit/virtus/attribute/default_value/evaluate_spec.rb
181
- - spec/unit/virtus/attribute/default_value/instance_methods/evaluate_spec.rb
182
186
  - spec/unit/virtus/attribute/default_value/value_spec.rb
183
187
  - spec/unit/virtus/attribute/define_accessor_methods_spec.rb
184
188
  - spec/unit/virtus/attribute/define_reader_method_spec.rb
@@ -188,7 +192,6 @@ files:
188
192
  - spec/unit/virtus/attribute/float/coerce_spec.rb
189
193
  - spec/unit/virtus/attribute/get_spec.rb
190
194
  - spec/unit/virtus/attribute/inspect_spec.rb
191
- - spec/unit/virtus/attribute/instance_variable_name_spec.rb
192
195
  - spec/unit/virtus/attribute/integer/coerce_spec.rb
193
196
  - spec/unit/virtus/attribute/name_spec.rb
194
197
  - spec/unit/virtus/attribute/numeric/class_methods/descendants_spec.rb
@@ -198,14 +201,12 @@ files:
198
201
  - spec/unit/virtus/attribute/options_spec.rb
199
202
  - spec/unit/virtus/attribute/public_reader_spec.rb
200
203
  - spec/unit/virtus/attribute/public_writer_spec.rb
201
- - spec/unit/virtus/attribute/reader_visibility_spec.rb
202
204
  - spec/unit/virtus/attribute/set/coerce_spec.rb
203
205
  - spec/unit/virtus/attribute/set_spec.rb
204
206
  - spec/unit/virtus/attribute/string/coerce_spec.rb
205
207
  - spec/unit/virtus/attribute/symbol/coerce_spec.rb
206
208
  - spec/unit/virtus/attribute/time/coerce_spec.rb
207
209
  - spec/unit/virtus/attribute/value_coerced_spec.rb
208
- - spec/unit/virtus/attribute/writer_visibility_spec.rb
209
210
  - spec/unit/virtus/attribute_set/append_spec.rb
210
211
  - spec/unit/virtus/attribute_set/each_spec.rb
211
212
  - spec/unit/virtus/attribute_set/element_reference_spec.rb
@@ -214,6 +215,7 @@ files:
214
215
  - spec/unit/virtus/attribute_set/parent_spec.rb
215
216
  - spec/unit/virtus/attribute_set/reset_spec.rb
216
217
  - spec/unit/virtus/attributes_accessor/inspect_spec.rb
218
+ - spec/unit/virtus/class_methods/allowed_writer_methods_spec.rb
217
219
  - spec/unit/virtus/class_methods/attribute_spec.rb
218
220
  - spec/unit/virtus/class_methods/attributes_spec.rb
219
221
  - spec/unit/virtus/class_methods/const_missing_spec.rb
@@ -235,7 +237,6 @@ files:
235
237
  - spec/unit/virtus/coercion/float/class_methods/to_decimal_spec.rb
236
238
  - spec/unit/virtus/coercion/float/class_methods/to_integer_spec.rb
237
239
  - spec/unit/virtus/coercion/float/class_methods/to_string_spec.rb
238
- - spec/unit/virtus/coercion/hash/class_methods/to_array_spec.rb
239
240
  - spec/unit/virtus/coercion/hash/class_methods/to_date_spec.rb
240
241
  - spec/unit/virtus/coercion/hash/class_methods/to_datetime_spec.rb
241
242
  - spec/unit/virtus/coercion/hash/class_methods/to_time_spec.rb
@@ -244,6 +245,10 @@ files:
244
245
  - spec/unit/virtus/coercion/integer/class_methods/to_float_spec.rb
245
246
  - spec/unit/virtus/coercion/integer/class_methods/to_string_spec.rb
246
247
  - spec/unit/virtus/coercion/object/class_methods/method_missing_spec.rb
248
+ - spec/unit/virtus/coercion/object/class_methods/to_array_spec.rb
249
+ - spec/unit/virtus/coercion/object/class_methods/to_hash_spec.rb
250
+ - spec/unit/virtus/coercion/object/class_methods/to_integer_spec.rb
251
+ - spec/unit/virtus/coercion/object/class_methods/to_string_spec.rb
247
252
  - spec/unit/virtus/coercion/string/class_methods/to_boolean_spec.rb
248
253
  - spec/unit/virtus/coercion/string/class_methods/to_constant_spec.rb
249
254
  - spec/unit/virtus/coercion/string/class_methods/to_date_spec.rb
@@ -266,6 +271,7 @@ files:
266
271
  - spec/unit/virtus/options/options_spec.rb
267
272
  - spec/unit/virtus/type_lookup/determine_type_spec.rb
268
273
  - spec/unit/virtus/type_lookup/primitive_spec.rb
274
+ - spec/unit/virtus/value_object/class_methods/allowed_writer_methods_spec.rb
269
275
  - spec/unit/virtus/value_object/class_methods/equalizer_spec.rb
270
276
  - spec/unit/virtus/value_object/initialize_spec.rb
271
277
  - spec/unit/virtus/value_object/with_spec.rb
@@ -309,9 +315,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
309
315
  requirements: []
310
316
 
311
317
  rubyforge_project:
312
- rubygems_version: 1.8.15
318
+ rubygems_version: 1.8.17
313
319
  signing_key:
314
320
  specification_version: 3
315
321
  summary: Attributes on Steroids for Plain Old Ruby Objects
316
322
  test_files: []
317
323
 
324
+ has_rdoc:
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute::DefaultValue, '#evaluate' do
4
- subject { object.evaluate(instance) }
5
-
6
- let(:object) { described_class.new(attribute, value) }
7
- let(:attribute) { Virtus::Attribute::String.new(:title) }
8
- let(:instance) { Class.new }
9
-
10
- context 'with a non-callable value' do
11
- context 'with a non-cloneable value' do
12
- let(:value) { nil }
13
-
14
- it { should eql(value) }
15
- end
16
-
17
- context 'with a cloneable value' do
18
- let(:value) { 'something' }
19
-
20
- it { should eql(value) }
21
- it { should_not equal(value) }
22
- end
23
- end
24
-
25
- context 'with a callable value' do
26
- let(:value) { lambda { |instance, attribute| attribute.name } }
27
-
28
- it { should be(:title) }
29
- end
30
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute, '#instance_variable_name' do
4
- subject { object.instance_variable_name }
5
-
6
- let(:object) { described_class.new(:name, options) }
7
- let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
8
- let(:primitive) { stub('primitive') }
9
- let(:coercion_method) { stub('coercion_method') }
10
-
11
- it { should be(:@name) }
12
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute, '#reader_visibility' do
4
- subject { object.reader_visibility }
5
-
6
- let(:object) { described_class.new(:name, options) }
7
- let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
8
- let(:primitive) { stub('primitive') }
9
- let(:coercion_method) { stub('coercion_method') }
10
-
11
- context 'when not specified' do
12
- it { should be(:public) }
13
- end
14
-
15
- context 'when specified' do
16
- let(:reader) { stub('reader') }
17
-
18
- before do
19
- options.update(:reader => reader)
20
- end
21
-
22
- it { should be(reader) }
23
- end
24
- end
@@ -1,24 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Attribute, '#writer_visibility' do
4
- subject { object.writer_visibility }
5
-
6
- let(:object) { described_class.new(:name, options) }
7
- let(:options) { { :primitive => primitive, :coercion_method => coercion_method } }
8
- let(:primitive) { stub('primitive') }
9
- let(:coercion_method) { stub('coercion_method') }
10
-
11
- context 'when not specified' do
12
- it { should be(:public) }
13
- end
14
-
15
- context 'when specified' do
16
- let(:writer) { stub('writer') }
17
-
18
- before do
19
- options.update(:writer => writer)
20
- end
21
-
22
- it { should be(writer) }
23
- end
24
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Virtus::Coercion::Hash, '.to_array' do
4
- subject { object.to_array(hash) }
5
-
6
- let(:object) { described_class }
7
- let(:hash) { { 'a' => 1, 'b' => 2 } }
8
-
9
- it { should be_instance_of(Array) }
10
-
11
- it { should =~ [ [ 'a', 1 ], [ 'b', 2 ] ] }
12
- end