lib/virtus/instance_methods.rb in virtus-0.5.1 vs lib/virtus/instance_methods.rb in virtus-0.5.2
- old
+ new
@@ -130,10 +130,36 @@
# @api public
def to_hash
attributes
end
+ # Freeze object
+ #
+ # @return [self]
+ #
+ # @api public
+ #
+ # @example
+ #
+ # class User
+ # include Virtus
+ #
+ # attribute :name, String
+ # attribute :age, Integer
+ # end
+ #
+ # user = User.new(:name => 'John', :age => 28)
+ # user.frozen? # => false
+ # user.freeze
+ # user.frozen? # => true
+ #
+ # @api public
+ def freeze
+ set_defaults
+ super
+ end
+
private
# Get values of all attributes defined for this class, ignoring privacy
#
# @return [Hash]
@@ -144,10 +170,21 @@
name = attribute.name
attributes[name] = get_attribute(name) if yield(attribute)
end
end
+ # Ensure all defaults are set
+ #
+ # @return [AttributeSet]
+ #
+ # @api private
+ def set_defaults
+ attribute_set.each do |attribute|
+ get_attribute(attribute.name)
+ end
+ end
+
# Mass-assign attribute values
#
# @see Virtus::InstanceMethods#attributes=
#
# @return [Hash]
@@ -179,12 +216,16 @@
# @api private
def set_attribute(name, value)
__send__("#{name}=", value)
end
+ # The list of allowed public methods
+ #
+ # @return [Array<String>]
+ #
# @api private
- def public_method_list
- public_methods
+ def allowed_methods
+ public_methods.map(&:to_s)
end
end # module InstanceMethods
end # module Virtus