class ActionSet::AttributeValue::ActiveModelAdapter

Public Class Methods

new(raw, target) click to toggle source
# File lib/action_set/attribute_value.rb, line 70
def initialize(raw, target)
  @raw = raw
  @target = target
end

Public Instance Methods

process() click to toggle source
# File lib/action_set/attribute_value.rb, line 75
def process
  return @raw if @raw.is_a? @target

  possible_values.find { |v| v.is_a? @target }
end

Private Instance Methods

can_typecast?(const_name) click to toggle source
# File lib/action_set/attribute_value.rb, line 102
def can_typecast?(const_name)
  typecasting_class = type_class.const_get(const_name)
  typecasting_class.instance_methods.include?(:cast) ||
    typecasting_class.instance_methods.include?(:type_cast)
end
init_typecaster(const_name) click to toggle source
# File lib/action_set/attribute_value.rb, line 108
def init_typecaster(const_name)
  type_class.const_get(const_name).new
rescue StandardError
  # :nocov:
  nil
  # :nocov:
end
possible_typecasters() click to toggle source
# File lib/action_set/attribute_value.rb, line 87
def possible_typecasters
  @possible_typecasters ||= type_class.constants
                                      .map(&:to_s)
                                      .select { |t| can_typecast?(t) }
                                      .reject { |t| t == 'Time' }
                                      .map { |t| init_typecaster(t) }
                                      .compact
end
possible_values() click to toggle source
# File lib/action_set/attribute_value.rb, line 83
def possible_values
  possible_typecasters.map { |m| typecast(m, @raw) }
end
type_class() click to toggle source
# File lib/action_set/attribute_value.rb, line 116
def type_class
  ActiveModel::Type
rescue NameError
  # :nocov:
  ActiveRecord::Type
  # :nocov:
end
typecast(to_type, value) click to toggle source
# File lib/action_set/attribute_value.rb, line 96
def typecast(to_type, value)
  return to_type.type_cast(value) if to_type.respond_to? :type_cast

  to_type.cast(value)
end