lib/virtus/instance_methods.rb in virtus-0.0.3 vs lib/virtus/instance_methods.rb in virtus-0.0.4
- old
+ new
@@ -1,11 +1,13 @@
module Virtus
+
# Instance methods that are added when you include Virtus
module InstanceMethods
+
# Set attributes during initialization of an object
#
- # @param [Hash] attributes
+ # @param [#to_hash] attributes
# the attributes hash to be set
#
# @return [Object]
#
# @api private
@@ -21,21 +23,21 @@
#
# attribute :name, String
# end
#
# user = User.new(:name => 'John')
- # user.attribute_get(:name) # => "john"
+ # user[:name] # => "john"
#
# @param [Symbol] name
# a name of an attribute
#
# @return [Object]
# a value of an attribute
#
# @api public
- def attribute_get(name)
- __send__(name)
+ def [](name)
+ attribute_get(name)
end
# Sets a value of the attribute with the given name
#
# @example
@@ -44,11 +46,11 @@
#
# attribute :name, String
# end
#
# user = User.new
- # user.attribute_set(:name) # => "john"
+ # user[:name] = "john" # => "john"
# user.name # => "john"
#
# @param [Symbol] name
# a name of an attribute
#
@@ -57,63 +59,85 @@
#
# @return [Object]
# the value set on an object
#
# @api public
- def attribute_set(name, value)
- __send__("#{name}=", value)
+ def []=(name, value)
+ attribute_set(name, value)
end
- # Mass-assign of attribute values
+ # Returns a hash of all publicly accessible attributes
#
# @example
# class User
# include Virtus
#
# attribute :name, String
# attribute :age, Integer
# end
#
- # user = User.new
- # user.attributes = { :name => 'John', :age => 28 }
+ # user = User.new(:name => 'John', :age => 28)
+ # user.attributes # => { :name => 'John', :age => 28 }
#
- # @param [Hash] attributes
- # a hash of attribute values to be set on an object
- #
# @return [Hash]
# the attributes
#
# @api public
- def attributes=(attributes)
- attributes.each do |name, value|
- attribute_set(name, value) if respond_to?("#{name}=")
+ def attributes
+ attributes = {}
+
+ self.class.attributes.each do |attribute|
+ name = attribute.name
+ attributes[name] = attribute_get(name) if respond_to?(name)
end
+
+ attributes
end
- # Returns a hash of all publicly accessible attributes
+ # Mass-assign of attribute values
#
# @example
# class User
# include Virtus
#
# attribute :name, String
# attribute :age, Integer
# end
#
- # user = User.new(:name => 'John', :age => 28)
- # user.attributes # => { :name => 'John', :age => 28 }
+ # user = User.new
+ # user.attributes = { :name => 'John', :age => 28 }
#
+ # @param [#to_hash] attributes
+ # a hash of attribute values to be set on an object
+ #
# @return [Hash]
# the attributes
#
# @api public
- def attributes
- attributes = {}
-
- self.class.attributes.each_key do |name|
- attributes[name] = attribute_get(name) if respond_to?(name)
+ def attributes=(attributes)
+ attributes.to_hash.each do |name, value|
+ attribute_set(name, value) if respond_to?("#{name}=")
end
+ end
- attributes
+ private
+
+ # Returns a value of the attribute with the given name
+ #
+ # @see Virtus::InstanceMethods#[]
+ #
+ # @api private
+ def attribute_get(name)
+ __send__(name)
end
- end # InstanceMethods
-end # Virtus
+
+ # Sets a value of the attribute with the given name
+ #
+ # @see Virtus::InstanceMethods#[]=
+ #
+ # @api private
+ def attribute_set(name, value)
+ __send__("#{name}=", value)
+ end
+
+ end # module InstanceMethods
+end # module Virtus