Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 18c408fefa6f77977866b11e391b1d3c90b5eb02 1 parent 38c4355
@solnic authored
View
15 lib/virtus/attribute/accessor.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
View
6 spec/unit/virtus/class_methods/finalize_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.