module ActiveAttr::Attributes

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

@example Usage

class Person
  include ActiveAttr::Attributes
  attribute :name
end

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

@since 0.2.0

Constants

FILTERED

@private @since 0.14.0

PARAMETER_FILTER

@private @since 0.14.0

Public Class Methods

filter_attributes() click to toggle source

Specifies attributes which won't be exposed while calling inspect

@return [Array<#to_s, Regexp, Proc>] filter_attributes Configured global default filtered attributes

@since 0.14.0

# File lib/active_attr/attributes.rb, line 51
def self.filter_attributes
  @filter_attributes ||= []
end
filter_attributes=(new_filter_attributes) click to toggle source

Configure the global default filtered attributes

@param [Array<#to_s, Regexp, Proc>] new_filter_attributes The new global default filtered attributes

@since 0.14.0

# File lib/active_attr/attributes.rb, line 61
def self.filter_attributes=(new_filter_attributes)
  @filter_attributes = new_filter_attributes
end

Public Instance Methods

==(other) click to toggle source

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

@example Compare for equality.

model == other

@param [ActiveAttr::Attributes, Object] other The other model to compare

@return [true, false] True if attributes are equal and other is instance

of the same Class, false if not.

@since 0.2.0

# File lib/active_attr/attributes.rb, line 84
def ==(other)
  return false unless other.instance_of? self.class
  attributes == other.attributes
end
[](name)
Alias for: read_attribute
[]=(name, value)
Alias for: write_attribute
attributes() click to toggle source

Returns a Hash of all attributes

@example Get attributes

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

@return [Hash{String => Object}] The Hash of all attributes

@since 0.2.0

# File lib/active_attr/attributes.rb, line 97
def attributes
  attributes_map { |name| send name }
end
inspect() click to toggle source

Returns the class name plus its attributes

@example Inspect the model.

person.inspect

@return [String] Human-readable presentation of the attribute

definitions

@since 0.2.0

# 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) click to toggle source

Read a value from the model's attributes.

@example Read an attribute with read_attribute

person.read_attribute(:name)

@example Rean an attribute with bracket syntax

person[:name]

@param [String, Symbol, to_s] name The name of the attribute to get.

@return [Object] The value of the attribute.

@raise [UnknownAttributeError] if the attribute is unknown

@since 0.2.0

# 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
Also aliased as: []
write_attribute(name, value) click to toggle source

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

@example Write the attribute with write_attribute

person.write_attribute(:name, "Benjamin")

@example Write an attribute with bracket syntax

person[:name] = "Benjamin"

@param [String, Symbol, to_s] name The name of the attribute to update. @param [Object] value The value to set for the attribute.

@raise [UnknownAttributeError] if the attribute is unknown

@since 0.2.0

# 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
Also aliased as: []=

Private Instance Methods

attribute(name) click to toggle source

Read an attribute from the attributes hash

@since 0.2.1

# File lib/active_attr/attributes.rb, line 179
def attribute(name)
  @attributes ||= {}
  @attributes[name]
end
attribute=(name, value) click to toggle source

Write an attribute to the attributes hash

@since 0.2.1

# File lib/active_attr/attributes.rb, line 187
def attribute=(name, value)
  @attributes ||= {}
  @attributes[name] = value
end
attributes_map() { |name| ... } click to toggle source

Maps all attributes using the given block

@example Stringify attributes

person.attributes_map { |name| send(name).to_s }

@yield [name] block called to return hash value @yieldparam [String] name The name of the attribute to map.

@return [Hash{String => Object}] The Hash of mapped attributes

@since 0.7.0

# File lib/active_attr/attributes.rb, line 203
def attributes_map
  self.class.attribute_names.each_with_object({}) do |name, hash|
    hash[name] = yield(name)
  end
end