class Morf::AttributesCaster
Attributes
attributes[R]
options[R]
Public Class Methods
new(attributes, options)
click to toggle source
# File lib/morf/attributes_caster.rb, line 4 def initialize(attributes, options) @attributes = attributes @options = options end
Public Instance Methods
cast(input_hash, params = {})
click to toggle source
# File lib/morf/attributes_caster.rb, line 9 def cast(input_hash, params = {}) casted_hash = {} hash_keys = get_keys(input_hash) attributes.each do |attribute| if hash_keys.include?(attribute.name) begin casted_value = cast_attribute(attribute, input_hash) casted_hash[attribute.name] = casted_value rescue Morf::Errors::AttributeError => e e.add_namespace(attribute.name) raise e end else raise Morf::Errors::MissingAttributeError.new("should be given", attribute.name) if attribute.required? end end if !options[:skip_unexpected_attributes] check_unexpected_attributes_not_given!(hash_keys, casted_hash.keys) end casted_hash end
Private Instance Methods
cast_attribute(attribute, hash)
click to toggle source
# File lib/morf/attributes_caster.rb, line 36 def cast_attribute(attribute, hash) value = get_value(hash, attribute.name) if value.nil? && attribute.allow_nil? nil else casted_value = attribute.caster.cast(value, attribute.name, attribute.options) if attribute.has_children? cast_children(casted_value, attribute) elsif caster = attribute.options[:caster] cast_children_with_caster(casted_value, attribute, caster) else casted_value end end end
cast_children(value, attribute)
click to toggle source
# File lib/morf/attributes_caster.rb, line 52 def cast_children(value, attribute) caster = self.class.new(attribute.children, options) cast_children_with_caster(value, attribute, caster) end
cast_children_with_caster(value, attribute, caster)
click to toggle source
# File lib/morf/attributes_caster.rb, line 57 def cast_children_with_caster(value, attribute, caster) if attribute.caster == Morf::Casters::ArrayCaster value.map do |val| caster.cast(val, options) end else caster.cast(value, options) end end
check_unexpected_attributes_not_given!(input_hash_keys, casted_hash_keys)
click to toggle source
# File lib/morf/attributes_caster.rb, line 91 def check_unexpected_attributes_not_given!(input_hash_keys, casted_hash_keys) unexpected_keys = input_hash_keys - casted_hash_keys unless unexpected_keys.empty? raise Morf::Errors::UnexpectedAttributeError.new("is not valid attribute name", unexpected_keys.first) end end
get_keys(hash)
click to toggle source
# File lib/morf/attributes_caster.rb, line 67 def get_keys(hash) if options[:input_keys] != options[:output_keys] if options[:input_keys] == :symbol hash.keys.map(&:to_s) else hash.keys.map(&:to_sym) end else hash.keys end end
get_value(hash, key)
click to toggle source
# File lib/morf/attributes_caster.rb, line 79 def get_value(hash, key) if options[:input_keys] != options[:output_keys] if options[:input_keys] == :symbol hash[key.to_sym] else hash[key.to_s] end else hash[key] end end