Module: ActiveAttr::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
AttributeDefaults, QueryAttributes, Serialization, TypecastedAttributes
Defined in:
lib/active_attr/attributes.rb

Overview

Attributes provides a set of class methods for defining an attributes schema and instance methods for reading and writing attributes.

Examples:

Usage

class Person
  include ActiveAttr::Attributes
  attribute :name
end

person = Person.new
person.name = "Ben Poweski"

Since:

  • 0.2.0

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEPRECATED_OBJECT_METHODS =

Methods deprecated on the Object class which can be safely overridden

Since:

  • 0.3.0

%w(id type)

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ true, false

Performs equality checking on the result of attributes and its type.

Examples:

Compare for equality.

model == other

Parameters:

Returns:

  • (true, false)

    True if attributes are equal and other is instance of the same Class, false if not.

Since:

  • 0.2.0



46
47
48
49
# File 'lib/active_attr/attributes.rb', line 46

def ==(other)
  return false unless other.instance_of? self.class
  attributes == other.attributes
end

#attributesHash{String => Object}

Returns a Hash of all attributes

Examples:

Get attributes

person.attributes # => {"name"=>"Ben Poweski"}

Returns:

  • (Hash{String => Object})

    The Hash of all attributes

Since:

  • 0.2.0



59
60
61
# File 'lib/active_attr/attributes.rb', line 59

def attributes
  attributes_map { |name| send name }
end

#inspectString

Returns the class name plus its attributes

Examples:

Inspect the model.

person.inspect

Returns:

  • (String)

    Human-readable presentation of the attribute definitions

Since:

  • 0.2.0



72
73
74
75
76
# File 'lib/active_attr/attributes.rb', line 72

def inspect
  attribute_descriptions = attributes.sort.map { |key, value| "#{key}: #{value.inspect}" }.join(", ")
  separator = " " unless attribute_descriptions.empty?
  "#<#{self.class.name}#{separator}#{attribute_descriptions}>"
end

#read_attribute(name) ⇒ Object Also known as: []

Read a value from the model’s attributes.

Examples:

Read an attribute with read_attribute

person.read_attribute(:name)

Rean an attribute with bracket syntax

person[:name]

Parameters:

  • name (String, Symbol, #to_s)

    The name of the attribute to get.

Returns:

  • (Object)

    The value of the attribute.

Raises:

Since:

  • 0.2.0



92
93
94
95
96
97
98
# File 'lib/active_attr/attributes.rb', line 92

def read_attribute(name)
  if respond_to? name
    send name.to_s
  else
    raise UnknownAttributeError, "unknown attribute: #{name}"
  end
end

#write_attribute(name, value) ⇒ Object Also known as: []=

Write a single attribute to the model’s attribute hash.

Examples:

Write the attribute with write_attribute

person.write_attribute(:name, "Benjamin")

Write an attribute with bracket syntax

person[:name] = "Benjamin"

Parameters:

  • name (String, Symbol, #to_s)

    The name of the attribute to update.

  • value (Object)

    The value to set for the attribute.

Raises:

Since:

  • 0.2.0



114
115
116
117
118
119
120
# File 'lib/active_attr/attributes.rb', line 114

def write_attribute(name, value)
  if respond_to? "#{name}="
    send "#{name}=", value
  else
    raise UnknownAttributeError, "unknown attribute: #{name}"
  end
end