class ActiveEntity::Attribute
@example Cast with given type option.
attr = ActiveEntity::Attribute.new(:user_id, type: Integer) attr.cast!('12') #=> 12 attr.cast!('a') #=> raises ActiveEntity::CastError
@example Cast with given casting procedure.
attr = ActiveEntity::Attribute.new( :created_at, cast_by: -> (value) do case value when Fixnum, Float Time.at(value) when String Time.parse(value) else raise ActiveEntity::CastError.build(:Time, value) end end ) attr.cast!(1420081200) #=> a Time object attr.cast!('2015/01/01 12:00:00') #=> a Time object
Attributes
name[R]
options[R]
Public Class Methods
new(name, options = {})
click to toggle source
@param [Symbol] name @param [Hash{Symbol => Object}] options
# File lib/active_entity/attribute.rb, line 28 def initialize(name, options = {}) raise ArgumentError unless options.is_a?(Hash) @name, @options = name, options end
Public Instance Methods
cast!(value)
click to toggle source
@param [Object] value @return [Object] casted value @raise [ActiveEntity::CastError]
# File lib/active_entity/attribute.rb, line 46 def cast!(value) case when type cast_by_defined_type(value) when cast_by cast_by.call(value) else value end end
cast_by()
click to toggle source
@return [Proc]
# File lib/active_entity/attribute.rb, line 39 def cast_by @options[:cast_by] end
type()
click to toggle source
@return [Class, Symbol, String]
# File lib/active_entity/attribute.rb, line 34 def type @options[:type] end
Private Instance Methods
cast_by_defined_type(value)
click to toggle source
# File lib/active_entity/attribute.rb, line 59 def cast_by_defined_type(value) procedure = ActiveEntity::ConversionDefinitions.get(type) if procedure procedure.call(value) else raise ConfigurationError.new("Can't find casting procedure for `#{type}`") end end