Permalink
Browse files

Set name in the extended hook rather than during finalization

Otherwise name won't be available for attributes in a model with
:finalize set to false
  • Loading branch information...
solnic committed Oct 8, 2013
1 parent 38c4355 commit 18c408fefa6f77977866b11e391b1d3c90b5eb02
Showing with 14 additions and 7 deletions.
  1. +8 −7 lib/virtus/attribute/accessor.rb
  2. +6 −0 spec/unit/virtus/class_methods/finalize_spec.rb
@@ -26,6 +26,14 @@ module Accessor
# @api private
attr_reader :instance_variable_name
+ # @api private
+ def self.extended(descendant)
+ super
+ name = descendant.options.fetch(:name).to_sym
+ descendant.instance_variable_set('@name', name)
+ descendant.instance_variable_set('@instance_variable_name', "@#{name}")
+ end
+
# Return value of the attribute
#
# @param [Object] instance
@@ -78,13 +86,6 @@ def public_writer?
options[:writer] == :public
end
- # @api private
- def finalize
- @name = options.fetch(:name).to_sym
- @instance_variable_name = "@#{@name}"
- super
- end
-
end # Accessor
end # Attribute
@@ -6,6 +6,7 @@ module Examples
class Person
include Virtus.model(:finalize => false)
+ attribute :name, String
attribute :articles, Array['Examples::Article']
attribute :address, :'Examples::Address'
end
@@ -39,6 +40,11 @@ class Address
Virtus.finalize
end
+ it "sets attributes that don't require finalization" do
+ expect(Examples::Person.attribute_set[:name]).to be_instance_of(Virtus::Attribute)
+ expect(Examples::Person.attribute_set[:name].primitive).to be(String)
+ end
+
it 'it finalizes member type for a collection attribute' do
expect(Examples::Person.attribute_set[:address].primitive).to be(Examples::Address)
end

0 comments on commit 18c408f

Please sign in to comment.