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)
ATTRIBUTES_METHOD_PATTERNS =
Gem::Requirement.create('>= 7.1').satisfied_by?(Gem.loaded_specs["activesupport"].version) ? :attribute_method_patterns : :attribute_method_matchers

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.



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

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



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

def attributes
  attributes_map { |name| send name }
end

#query_attribute(name) ⇒ Object



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

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:



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

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

  send "#{name}=", value
end