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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.filter_attributesArray<#to_s, Regexp, Proc>

Specifies attributes which won’t be exposed while calling #inspect

global default filtered attributes

Returns:

  • (Array<#to_s, Regexp, Proc>)

    filter_attributes Configured

Since:

  • 0.14.0



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

def self.filter_attributes
  @filter_attributes ||= []
end

.filter_attributes=(new_filter_attributes) ⇒ Object

Configure the global default filtered attributes

global default filtered attributes

Parameters:

  • new_filter_attributes (Array<#to_s, Regexp, Proc>)

    The new

Since:

  • 0.14.0



61
62
63
# File 'lib/active_attr/attributes.rb', line 61

def self.filter_attributes=(new_filter_attributes)
  @filter_attributes = new_filter_attributes
end

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



84
85
86
87
# File 'lib/active_attr/attributes.rb', line 84

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



97
98
99
# File 'lib/active_attr/attributes.rb', line 97

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



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_attr/attributes.rb', line 110

def inspect
  inspection_filter = PARAMETER_FILTER.new(filter_attributes)
  original_attributes = attributes
  filtered_attributes = inspection_filter.filter(original_attributes)

  attribute_descriptions = filtered_attributes.sort.map { |key, value|
    inspect_value = case
    when original_attributes[key].nil? then nil.inspect
    when value == FILTERED then FILTERED
    else value.inspect
    end

    "#{key}: #{inspect_value}"
  }.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



143
144
145
146
147
148
149
# File 'lib/active_attr/attributes.rb', line 143

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



165
166
167
168
169
170
171
# File 'lib/active_attr/attributes.rb', line 165

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