module Virtus # Instance methods that are added when you include Virtus module InstanceMethods # Set attributes during initialization of an object # # @param [#to_hash] attributes # the attributes hash to be set # # @return [undefined] # # @api private def initialize(attribute_values = {}) self.attributes = attribute_values end # Returns a value of the attribute with the given name # # @example # class User # include Virtus # # attribute :name, String # end # # user = User.new(:name => 'John') # user[:name] # => "John" # # @param [Symbol] name # a name of an attribute # # @return [Object] # a value of an attribute # # @api public def [](name) __send__(name) end # Sets a value of the attribute with the given name # # @example # class User # include Virtus # # attribute :name, String # end # # user = User.new # user[:name] = "John" # => "John" # user.name # => "John" # # @param [Symbol] name # a name of an attribute # # @param [Object] value # a value to be set # # @return [Object] # the value set on an object # # @api public def []=(name, value) __send__("#{name}=", value) end # Returns a hash of all publicly accessible attributes # # @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 } # # @return [Hash] # # @api public def attributes self.class.attributes.each_with_object({}) do |attribute, attributes| name = attribute.name attributes[name] = self[name] if attribute.public_reader? end end # Mass-assign attribute values # # Keys in the +attribute_values+ param can be symbols or strings. # Only non-private referenced Attribute writer methods will be called. # Non-attribute setter methods on the receiver will not be called. # # @example # class User # include Virtus # # attribute :name, String # attribute :age, Integer # end # # user = User.new # user.attributes = { :name => 'John', 'age' => 28 } # # @param [#to_hash] attribute_values # a hash of attribute names and values to set on the receiver # # @return [Hash] # # @api public def attributes=(attribute_values) attributes = self.class.attributes set_attributes(attribute_values.select { |name,| attribute = attributes[name] attribute && attribute.public_writer? }) end # Returns a hash of all publicly accessible attributes # # @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 } # # @return [Hash] # # @api public def to_hash attributes end private # Mass-assign attribute values # # Keys in the +attribute_values+ param can be symbols or strings. # All referenced Attribute writer methods *will* be called. # Non-attribute setter methods on the receiver *will* be called. # # @example # class User # include Virtus # # attribute :name, String # attribute :age, Integer # end # # user = User.new # user.attributes = { :name => 'John', 'age' => 28 } # # @param [#to_hash] attribute_values # a hash of attribute names and values to set on the receiver # # @return [Hash] # # @api private def set_attributes(attribute_values) attribute_values.each { |name, value| self[name] = value } end end # module InstanceMethods end # module Virtus