Skip to content

Commit

Permalink
Include allowed_writer_methods only when mass-assignment is turned on
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Oct 3, 2013
1 parent 5bc6b09 commit a385ca2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
6 changes: 5 additions & 1 deletion lib/virtus/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ class ValueObjectBuilder < Builder

# @api private
def extensions
super << ValueObject::AllowedWriterMethods << ValueObject::InstanceMethods
super + [
Extensions::AllowedWriterMethods,
ValueObject::AllowedWriterMethods,
ValueObject::InstanceMethods
]
end

# @api private
Expand Down
1 change: 1 addition & 0 deletions lib/virtus/class_inclusions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module ClassInclusions
def self.included(descendant)
super
descendant.extend(ClassMethods)
descendant.extend(Extensions::AllowedWriterMethods)
descendant.class_eval { include Methods }
descendant.class_eval { include InstanceMethods }
descendant.class_eval { include InstanceMethods::Constructor }
Expand Down
32 changes: 20 additions & 12 deletions lib/virtus/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def self.extended(object)
object.instance_eval do
extend Methods
extend InstanceMethods
extend AllowedWriterMethods
extend InstanceMethods::MassAssignment
end
end
Expand Down Expand Up @@ -74,6 +75,23 @@ def values(&block)
include(::Equalizer.new(*attribute_set.map(&:name)))
end

private

# Return an attribute set for that instance
#
# @return [AttributeSet]
#
# @api private
def attribute_set
@attribute_set
end

end # Methods

module AllowedWriterMethods
WRITER_METHOD_REGEXP = /=\z/.freeze
INVALID_WRITER_METHODS = %w[ == != === []= attributes= ].to_set.freeze

# The list of writer methods that can be mass-assigned to in #attributes=
#
# @return [Set]
Expand All @@ -88,18 +106,8 @@ def allowed_writer_methods
end
end

private

# Return an attribute set for that instance
#
# @return [AttributeSet]
#
# @api private
def attribute_set
@attribute_set
end

end # Methods
end # AllowedWriterMethods

end # module Extensions

end # module Virtus
2 changes: 1 addition & 1 deletion lib/virtus/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Constructor
#
# @api private
def initialize(attributes = nil)
attribute_set.set(self, attributes) if attributes
self.class.attribute_set.set(self, attributes) if attributes
set_default_attributes
end

Expand Down
2 changes: 2 additions & 0 deletions lib/virtus/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ module MassAssignment
# @api private
def self.included(descendant)
super
descendant.extend(Extensions::AllowedWriterMethods)
descendant.send(:include, InstanceMethods::MassAssignment)
end
private_class_method :included

# @api private
def self.extended(descendant)
super
descendant.extend(Extensions::AllowedWriterMethods)
descendant.extend(InstanceMethods::MassAssignment)
end
private_class_method :included
Expand Down

0 comments on commit a385ca2

Please sign in to comment.