module FedoraLens::AttributeMethods

Constants

AttrNames

Public Instance Methods

[](attr_name) click to toggle source

Returns the value of the attribute identified by attr_name after it has been typecast (for example, “2004-12-12” in a date column is cast to a date object, like Date.new(2004, 12, 12)). It raises ActiveModel::MissingAttributeError if the identified attribute is missing.

Alias for the read_attribute method.

class Person < ActiveRecord::Base
  belongs_to :organization
end

person = Person.new(name: 'Francesco', age: '22')
person[:name] # => "Francesco"
person[:age]  # => 22

person = Person.select('id').first
person[:name]            # => ActiveModel::MissingAttributeError: missing attribute: name
person[:organization_id] # => ActiveModel::MissingAttributeError: missing attribute: organization_id
# File lib/fedora_lens/attribute_methods.rb, line 92
def [](attr_name)
  read_attribute(attr_name) { |n| missing_attribute(n, caller) }
end
[]=(attr_name, value) click to toggle source

Updates the attribute identified by attr_name with the specified value. (Alias for the protected write_attribute method).

class Person
  include FedoraLens
end

person = Person.new
person[:age] = '22'
person[:age] # => 22
person[:age] # => Fixnum
# File lib/fedora_lens/attribute_methods.rb, line 107
def []=(attr_name, value)
  write_attribute(attr_name, value)
end
attribute_names() click to toggle source

Returns an array of names for the attributes available on this object.

class Person
  include FedoraLens
end

person = Person.new
person.attribute_names
# => ["id", "created_at", "updated_at", "name", "age"]
# File lib/fedora_lens/attribute_methods.rb, line 55
def attribute_names
  @attributes.keys
end
attributes() click to toggle source

Returns a hash of all the attributes with their names as keys and the values of the attributes as values.

class Person
  include FedoraLens
end

person = Person.create(name: 'Francesco', age: 22)
person.attributes
# => {"id"=>3, "created_at"=>Sun, 21 Oct 2012 04:53:04, "updated_at"=>Sun, 21 Oct 2012 04:53:04, "name"=>"Francesco", "age"=>22}
# File lib/fedora_lens/attribute_methods.rb, line 68
def attributes
  attribute_names.each_with_object({}) { |name, attrs|
    attrs[name] = read_attribute(name)
  }
end