Module: ActiveAttr::TypecastedAttributes

Extended by:
ActiveSupport::Concern
Includes:
Attributes, Typecasting
Included in:
Model
Defined in:
lib/active_attr/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.

Examples:

Usage

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

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

Since:

  • 0.5.0

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Typecasting

#typecast_attribute, #typecaster_for

Methods included from Attributes

#==, #attributes, filter_attributes, filter_attributes=, #inspect, #read_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

Since:

  • 0.5.0



48
49
50
51
# File 'lib/active_attr/typecasted_attributes.rb', line 48

def attribute_before_type_cast(name)
  @attributes ||= {}
  @attributes[name.to_s]
end