module ActiveFedora::AttributeMethods
def [](attr_name)
person[:name] # => ActiveModel::MissingAttributeError: missing attribute: name
person = Person.select('id').first
person[:age] # => 22
person[:name] # => "Francesco"
person = Person.new(name: 'Francesco', age: '22')
end
belongs_to :organization
class Person < ActiveRecord::Base
Alias for the read_attribute method.
ActiveModel::MissingAttributeError if the identified attribute is missing.
"2004-12-12" in a date column is cast to a date object, like Date.new(2004, 12, 12)). It raises
Returns the value of the attribute identified by attr_name after it has been typecast (for example,
def [](attr_name) read_attribute(attr_name) { |n| missing_attribute(n, caller) } end
def []=(attr_name, value)
person[:age] # => 22
person[:age] = '22'
person = Person.new
end
class Person < ActiveFedora::Base
(Alias for the protected write_attribute method).
Updates the attribute identified by attr_name with the specified +value+.
def []=(attr_name, value) write_attribute(attr_name, value) end
def attribute_for_inspect(attr_name)
person.attribute_for_inspect(:tag_ids)
# => "\"2012-10-22 00:15:07\""
person.attribute_for_inspect(:created_at)
# => "\"David Heinemeier Hansson David Heinemeier Hansson ...\""
person.attribute_for_inspect(:name)
person = Person.create!(name: 'David Heinemeier Hansson ' * 3)
modification.
Other attributes return the value of #inspect without
:db format, Array attributes are truncated up to 10 values.
characters, Date and Time attributes are returned in the
attribute +attr_name+. String attributes are truncated up to 50
Returns an #inspect-like string for the value of the
def attribute_for_inspect(attr_name) value = self[attr_name] if value.is_a?(String) && value.length > 50 "#{value[0, 50]}...".inspect elsif value.is_a?(Date) || value.is_a?(Time) %("#{value.to_formatted_s(:db)}") elsif value.is_a?(Array) && value.size > 10 inspected = value.first(10).inspect %(#{inspected[0...-1]}, ...]) else value.inspect end end
def attribute_names
person.attribute_names
person = Person.new
end
class Person < ActiveFedora::Base
Returns an array of names for the attributes available on this object.
def attribute_names @local_attributes.keys end
def attribute_present?(attribute)
task.attribute_present?(:title) # => true
task.is_done = true
task.title = 'Buy milk'
task.attribute_present?(:is_done) # => true
task.attribute_present?(:title) # => false
task = Task.new(title: '', is_done: false)
end
class Task < ActiveRecord::Base
Note that it always returns +true+ with boolean attributes.
to objects that respond to empty?, most notably Strings). Otherwise, +false+.
database load and is neither +nil+ nor empty? (the latter only applies
Returns +true+ if the specified +attribute+ has been set by the user or by a
def attribute_present?(attribute) value = self[attribute] !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end
def attributes
person.attributes
person = Person.create(name: 'Francesco', age: 22)
end
class Person < ActiveFedora::Base
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
def attributes result = {} attribute_names.each do |name| result[name] = read_attribute(name) end result end
def has_attribute?(attr_name)
person.has_attribute?('age') # => true
person.has_attribute?(:name) # => true
person = Person.new
end
class Person < ActiveRecord::Base
Returns +true+ if the given attribute is in the attributes hash, otherwise +false+.
def has_attribute?(attr_name) attribute_names.include?(attr_name.to_s) end