Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[#89] Change attribute() to use Object as a default type

* Change ValueObejct#attribute to pass-through some arguments
  • Loading branch information...
commit 9e1bd9de7b94da03085b3a96f727a0c8000b65f8 1 parent f75c1f7
@dkubb dkubb authored
View
3  config/site.reek
@@ -31,7 +31,8 @@ FeatureEnvy:
Virtus::ClassMethods#build_attribute,
Virtus::Coercion::TimeCoercions#to_string,
Virtus::Coercion::TimeCoercions#coerce_with_method,
- Virtus::TypeLookup#determine_type_from_primitive
+ Virtus::TypeLookup#determine_type_from_primitive,
+ Virtus::ValueObject::ClassMethods#attribute
]
enabled: true
ClassVariable:
View
6 lib/virtus/attribute.rb
@@ -50,15 +50,15 @@ class Attribute
# the name of an attribute
#
# @param [Class] type
- # the type class of an attribute
+ # optional type class of an attribute
#
# @param [#to_hash] options
- # the extra options hash
+ # optional extra options hash
#
# @return [Attribute]
#
# @api private
- def self.build(name, type, options = {})
+ def self.build(name, type = Object, options = {})
attribute_class = determine_type(type) or
raise ArgumentError, "#{type.inspect} does not map to an attribute type"
attribute_options = attribute_class.merge_options(type, options)
View
1  lib/virtus/extensions.rb
@@ -31,6 +31,7 @@ def self.extended(object)
# attribute :author, String
# attribute :published_at, DateTime
# attribute :page_count, Integer
+ # attribute :index # defaults to Object
# end
#
# @param [Symbol] name
View
6 lib/virtus/value_object.rb
@@ -93,10 +93,10 @@ module ClassMethods
# @return [self]
#
# @api public
- def attribute(name, type, options = {})
+ def attribute(name, *args)
equalizer << name
- options[:writer] = :private
- super
+ options = args.last.kind_of?(Hash) ? args.pop : {}
+ super name, *args << options.merge(:writer => :private)
end
# Define and include a module that provides Value Object semantics
View
10 spec/unit/virtus/attribute/class_methods/build_spec.rb
@@ -27,6 +27,16 @@
its(:options) { should == Virtus::Attribute::String.options }
end
+ context 'without a type' do
+ subject { object.build(name) }
+
+ it { should be_instance_of(Virtus::Attribute::Object) }
+
+ its(:name) { should be(name) }
+
+ its(:options) { should == Virtus::Attribute::Object.options }
+ end
+
context 'with an invalid type' do
subject { object.build(name, type) }
View
47 spec/unit/virtus/value_object/class_methods/attribute_spec.rb
@@ -1,23 +1,50 @@
require 'spec_helper'
describe Virtus::ValueObject, '.attribute' do
- subject { object.attribute(name, type) }
-
let(:object) { Class.new { include Virtus::ValueObject } }
let(:name) { :latitude }
let(:type) { Float }
let(:attribute) { object.attribute_set[name] }
- it { should be(object) }
+ context 'without options' do
+ subject { object.attribute(name, type) }
+
+ it { should be(object) }
- it 'adds the attribute to the equalizer' do
- object.new.inspect.should_not match(/\b#{name}=\b/)
- subject
- object.new.inspect.should match(/\b#{name}=\b/)
+ it 'adds the attribute to the equalizer' do
+ object.new.inspect.should_not match(/\b#{name}=\b/)
+ subject
+ object.new.inspect.should match(/\b#{name}=\b/)
+ end
+
+ it 'sets the writer to be private' do
+ subject
+ attribute.options[:writer].should be(:private)
+ end
end
- it 'sets the writer to be private' do
- subject
- attribute.options[:writer].should be(:private)
+ context 'with options' do
+ subject { object.attribute(name, type, options) }
+
+ let(:options) { { :default => default } }
+ let(:default) { 1.0 }
+
+ it { should be(object) }
+
+ it 'adds the attribute to the equalizer' do
+ object.new.inspect.should_not match(/\b#{name}=\b/)
+ subject
+ object.new.inspect.should match(/\b#{name}=\b/)
+ end
+
+ it 'sets the writer to be private' do
+ subject
+ attribute.options[:writer].should be(:private)
+ end
+
+ it 'sets the default' do
+ subject
+ attribute.default.value.should eql(1.0)
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.