class Morf::Casters::ArrayCaster

Public Class Methods

cast(value, attr_name, options = {}) click to toggle source
# File lib/morf/casters/array_caster.rb, line 2
def self.cast(value, attr_name, options = {})
  if value.is_a?(Array)
    if options[:each]
      cast_array_items(value, attr_name, options)
    else
      value
    end
  else
    raise Morf::Errors::CastingError, "should be an array"
  end
end

Private Class Methods

cast_array_items(array, attr_name, options) click to toggle source
# File lib/morf/casters/array_caster.rb, line 16
def self.cast_array_items(array, attr_name, options)
  caster_name = options[:each]
  caster = Morf.casters[caster_name]
  check_caster_exists!(caster, caster_name)
  array.map do |item|
    caster.cast(item, "#{attr_name} item", options)
  end
end
check_caster_exists!(caster, caster_name) click to toggle source
# File lib/morf/casters/array_caster.rb, line 25
def self.check_caster_exists!(caster, caster_name)
  unless caster
    raise Morf::Errors::CasterNotFoundError, "caster with name #{caster_name} is not found"
  end
end