module Scrivener::Types

Provides a way to define attributes so they are cast into a corresponding type (defaults to String) when getting the attributes.

Any object that supports a .call method can be used as a “type”. The following are implemented out of the box:

@example

class CreateProduct < Scrivener
  attribute :name
  attribute :description
  attribute :price,           Types::Decimal
  attribute :avaliable_after, Types::Date
  attribute :stock,           Types::Integer
end

p = CreateProduct.new(name: "Foo", price: "10.0", available_after: "2012-07-10")
p.cleaned_price #=> BigDecimal.new("10.0")
p.cleaned_available_after #=> Date.new(2012, 7, 10)
p.cleaned_name #=> "Foo"

Constants

Boolean
Date
DateTime
Decimal
Float
Integer
String
Symbol
Time

Public Instance Methods

attribute(name, type=Types::String) click to toggle source

Define an attribute with its corresponding type. This is similar to attr_accessor, except the reader method will cast the object into the proper type.

If the casting results in a TypeError or ArgumentError, then an error on :typecast will be added to this attribute and the raw attribute will be returned instead.

@param [Symbol] name The name of the attribute. @param [#call] type The type of this attribute (defaults to String).

@example

attribute :foo
attribute :foo, Types::String
attribute :foo, Types::Date

@!macro [attach] attribute

@return [$2] the $1 attribute.
# File lib/scrivener/types.rb, line 73
def attribute(name, type=Types::String)
  attr_writer name

  define_method name do
    begin
      val = instance_variable_get(:"@#{name}")
      val && type.call(val)
    rescue TypeError, ArgumentError
      errors[name].push(:typecast)
      val
    end
  end
end