Module: ActiveAttr::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
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

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



38
39
40
41
# File 'lib/active_attr/attributes.rb', line 38

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

#attributesHash

Returns the raw attributes Hash

Examples:

Get attributes

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

Returns:

  • (Hash)

    The Hash of all attributes

Since:

  • 0.2.0



51
52
53
54
# File 'lib/active_attr/attributes.rb', line 51

def attributes
  attribute_names = self.class.attributes.map { |definition| definition.name.to_s }
  Hash[attribute_names.map { |key| [key, send(key)] }]
end

#initialize(*args) ⇒ Object

Since:

  • 0.2.1



57
58
59
# File 'lib/active_attr/attributes.rb', line 57

def initialize(*args)
  @attributes ||= {}
end

#inspectString

Returns the class name plus its attributes

Examples:

Inspect the model.

person.inspect

Returns:

  • (String)

    A nice pretty string to look at.

Since:

  • 0.2.0



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

def inspect
  attribute_descriptions = self.class.attributes.sort.map do |attribute|
    "#{attribute.name.to_s}: #{read_attribute(attribute.name).inspect}"
  end.join(", ")

  attribute_descriptions = " " + attribute_descriptions unless attribute_descriptions.empty?

  "#<#{self.class.name}#{attribute_descriptions}>"
end

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

Read a value from the model’s attributes. If the value does not exist it will return nil.

Examples:

Read an attribute.

person.read_attribute(:name)

Parameters:

  • name (String, Symbol)

    The name of the attribute to get.

Returns:

  • (Object)

    The value of the attribute.

Since:

  • 0.2.0



90
91
92
# File 'lib/active_attr/attributes.rb', line 90

def read_attribute(name)
  @attributes[name.to_s]
end

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

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

Examples:

Write the attribute.

person.write_attribute(:name, "Benjamin")

Parameters:

  • name (String, Symbol)

    The name of the attribute to update.

  • value (Object)

    The value to set for the attribute.

Since:

  • 0.2.0



106
107
108
# File 'lib/active_attr/attributes.rb', line 106

def write_attribute(name, value)
  @attributes[name.to_s] = value
end