class Dynamoid::TypeCasting::ArrayTypeCaster

Public Instance Methods

process(value) click to toggle source
# File lib/dynamoid/type_casting.rb, line 161
def process(value)
  array = type_cast_to_array(value)

  if array.present? && @options[:of].present?
    process_typed_array(array)
  else
    array
  end
end

Private Instance Methods

element_options() click to toggle source
# File lib/dynamoid/type_casting.rb, line 199
def element_options
  if @options[:of].is_a?(Hash)
    @options[:of][element_type].dup.tap do |options|
      options[:type] = element_type
    end
  else
    { type: element_type }
  end
end
element_type() click to toggle source
# File lib/dynamoid/type_casting.rb, line 191
def element_type
  if @options[:of].is_a?(Hash)
    @options[:of].keys.first
  else
    @options[:of]
  end
end
process_typed_array(array) click to toggle source
# File lib/dynamoid/type_casting.rb, line 181
def process_typed_array(array)
  type_caster = TypeCasting.find_type_caster(element_options)

  if type_caster.nil?
    raise ArgumentError, "Set element type #{element_type} isn't supported"
  end

  array.map { |el| type_caster.process(el) }
end
type_cast_to_array(value) click to toggle source
# File lib/dynamoid/type_casting.rb, line 173
def type_cast_to_array(value)
  if value.is_a?(Array)
    value.dup
  elsif value.respond_to?(:to_a)
    value.to_a
  end
end