Skip to content

Commit

Permalink
Add Virtus::InstanceMethods#get_attributes.
Browse files Browse the repository at this point in the history
This ignores privacy, similarly to #set_attributes.
  • Loading branch information
emmanuel authored and solnic committed Feb 8, 2012
1 parent 6b369a0 commit 260574c
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions lib/virtus/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(attribute_values = {})
#
# @api public
def [](name)
__send__(name)
get_attribute(name)
end

# Sets a value of the attribute with the given name
Expand All @@ -62,7 +62,7 @@ def [](name)
#
# @api public
def []=(name, value)
__send__("#{name}=", value)
set_attribute(name, value)
end

# Returns a hash of all publicly accessible attributes
Expand Down Expand Up @@ -165,7 +165,41 @@ def to_hash
#
# @api private
def set_attributes(attribute_values)
attribute_values.each { |name, value| self[name] = value }
attribute_values.each { |pair| set_attribute(*pair) }
end

# Get values of all attributes defined for this class, ignoring privacy
#
# @return [Hash]
#
# @api private
def get_attributes
self.class.attributes.each_with_object({}) do |attributes, attribute|
attribute_name = attribute.name
attributes[attribute_name] = get_attribute(attribute_name)
end
end

# Returns a value of the attribute with the given name
#
# @see Virtus::InstanceMethods#[]
#
# @return [Object]
#
# @api private
def get_attribute(name)
__send__(name)
end

# Sets a value of the attribute with the given name
#
# @see Virtus::InstanceMethods#[]=
#
# @return [Object]
#
# @api private
def set_attribute(name, value)
__send__("#{name}=", value)
end

end # module InstanceMethods
Expand Down

0 comments on commit 260574c

Please sign in to comment.