Module: ActiveGraph::Shared::TypecastedAttributes

Extended by:
ActiveSupport::Concern
Includes:
Attributes
Included in:
Property
Defined in:
lib/active_graph/shared/typecasted_attributes.rb

Overview

TypecastedAttributes allows types to be declared for your attributes

Types are declared by passing the :type option to the attribute class method. After a type is declared, attribute readers will convert any assigned attribute value to the declared type. If the assigned value cannot be cast, nil will be returned instead. You can access the original assigned value using the before_type_cast methods.

See Typecasting for the currently supported types.

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

Examples:

Usage

class Person
  include ActiveGraph::Shared::TypecastedAttributes
  attribute :age, :type => Integer
end

person = Person.new
person.age = "29"
person.age #=> 29
person.age_before_type_cast #=> "29"

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Attributes

Attributes::ATTRIBUTES_METHOD_PATTERNS, Attributes::DEPRECATED_OBJECT_METHODS

Instance Method Summary collapse

Methods included from Attributes

#==, #attributes, #query_attribute, #write_attribute

Instance Method Details

#attribute_before_type_cast(name) ⇒ Object?

Read the raw attribute value

Examples:

Reading a raw age value

person.age = "29"
person.attribute_before_type_cast(:age) #=> "29"

Parameters:

  • name (String, Symbol, #to_s)

    Attribute name

Returns:

  • (Object, nil)

    The attribute value before typecasting



41
42
43
44
# File 'lib/active_graph/shared/typecasted_attributes.rb', line 41

def attribute_before_type_cast(name)
  @attributes ||= ActiveGraph::AttributeSet.new({}, self.class.attributes.keys)
  @attributes.fetch_value(name.to_s)
end