Module: ActiveGraph::Shared::Attributes

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
TypecastedAttributes
Defined in:
lib/active_graph/shared/attributes.rb

Overview

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

Originally part of ActiveAttr, github.com/cgriego/active_attr

Examples:

Usage

class Person
  include ActiveGraph::Shared::Attributes
  attribute :name
end

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DEPRECATED_OBJECT_METHODS =

Methods deprecated on the Object class which can be safely overridden

%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:

  • other (ActiveAttr::Attributes, Object)

    The other model to compare

Returns:

  • (true, false)

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



37
38
39
40
# File 'lib/active_graph/shared/attributes.rb', line 37

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



48
49
50
# File 'lib/active_graph/shared/attributes.rb', line 48

def attributes
  attributes_map { |name| send name }
end

#query_attribute(name) ⇒ Object



70
71
72
73
74
# File 'lib/active_graph/shared/attributes.rb', line 70

def query_attribute(name)
  fail ActiveGraph::UnknownAttributeError, "unknown attribute: #{name}" if !respond_to? "#{name}?"

  send "#{name}?"
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:



63
64
65
66
67
# File 'lib/active_graph/shared/attributes.rb', line 63

def write_attribute(name, value)
  fail ActiveGraph::UnknownAttributeError, "unknown attribute: #{name}" if !respond_to? "#{name}="

  send "#{name}=", value
end