lib/aspect/has_attributes.rb
# Update attributes on this object.
#
# @example
#   class User
#     include Aspect::HasAttributes
#
#     attribute(:name) { |value| value.to_s.strip }
#     attribute(:moderator, query: true)
#     attribute(:admin, query: true) { |value| @moderator ? value : false }
#   end
#
#   user = User.new
#
#   user.name # => nil
#   user.moderator? # => false
#   user.admin? # => false
#
#   user.update_attributes(name: "  Ezio Auditore  ", moderator: true)
#
#   user.name # => "Ezio Auditore"
#   user.moderator? # => true
#   user.admin? # => false
# @example In `#initialize`
#   class User
#     include Aspect::HasAttributes
#
#     def initialize(attributes={})
#       update_attributes(attributes)
#     end
#
#     attribute(:name) { |value| value.to_s.strip }
#     attribute(:moderator, query: true)
#     attribute(:admin, query: true) { |value| @moderator ? value : false }
#   end
#
#   user = User.new(name: "  Ezio Auditore  ", moderator: true)
#
#   user.name # => "Ezio Auditore"
#   user.moderator? # => true
#   user.admin? # => false
# @param [Hash, #to_h] attributes
# @return [Object]
Update attributes on this object.
Update attributes on this object .
def update_attributes(attributes={})
      attributes.to_h.each { |name, value| send("#{name}=", value) }
      self
    end
def update_attributes ( attributes = { } ) attributes . to_h . each { | name , value | send ( "#{name}=" , value ) } self end
Update attributes on this object.
Update attributes on this object .
None
Object
attributes
<not_specific>
<not_specific>
None
example
class User include Aspect::HasAttributes attribute(:name) { |value| value.to_s.strip } attribute(:moderator, query: true) attribute(:admin, query: true) { |value| @moderator . value : false } end user = User.new user.name # => nil user.moderator. # => false user.admin. # => false user.update_attributes(name: " Ezio Auditore ", moderator: true) user.name # => "Ezio Auditore" user.moderator. # => true user.admin.
class User include Aspect :: HasAttributes attribute ( : name ) { |value| value . to_s . strip } attribute ( : moderator query : true ) attribute ( : admin query : true ) { |value| @moderator . value : false } end user = User . new user . name # = > nil user . moderator . # = > false user . admin . # = > false user . update_attributes ( name : " Ezio Auditore " moderator : true ) user . name # = > " Ezio Auditore " user . moderator . # = > true user . admin .
example
In `#initialize` class User include Aspect::HasAttributes def initialize(attributes={}) update_attributes(attributes) end attribute(:name) { |value| value.to_s.strip } attribute(:moderator, query: true) attribute(:admin, query: true) { |value| @moderator . value : false } end user = User.new(name: " Ezio Auditore ", moderator: true) user.name # => "Ezio Auditore" user.moderator. # => true user.admin.
In ` #initialize ` class User include Aspect :: HasAttributes def initialize ( attributes = {} ) update_attributes ( attributes ) end attribute ( : name ) { |value| value . to_s . strip } attribute ( : moderator query : true ) attribute ( : admin query : true ) { |value| @moderator . value : false } end user = User . new ( name : " Ezio Auditore " moderator : true ) user . name # = > " Ezio Auditore " user . moderator . # = > true user . admin .