class Rectify::FormatAttributesHash
Attributes
attribute_set[R]
Public Class Methods
new(attribute_set)
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 5 def initialize(attribute_set) @attribute_set = attribute_set end
Public Instance Methods
format(params)
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 9 def format(params) convert_indexed_hashes_to_arrays(params) convert_hash_keys(params) end
Private Instance Methods
array_attributes()
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 39 def array_attributes attribute_set.select { |attribute| attribute.primitive == Array } end
convert_hash_keys(value)
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 43 def convert_hash_keys(value) case value when Array value.map { |v| convert_hash_keys(v) } when Hash Hash[value.map { |k, v| [underscore_key(k), convert_hash_keys(v)] }] else value end end
convert_indexed_hashes_to_arrays(attributes_hash)
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 18 def convert_indexed_hashes_to_arrays(attributes_hash) array_attributes.each do |array_attribute| name = array_attribute.name attribute = attributes_hash[name] next unless attribute.is_a?(Hash) attributes_hash[name] = transform_values_for_type( attribute.values, array_attribute.member_type.primitive ) end end
transform_values_for_type(values, element_type)
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 31 def transform_values_for_type(values, element_type) return values unless element_type < Rectify::Form values.map do |value| self.class.new(element_type.attribute_set).format(value) end end
underscore_key(k)
click to toggle source
# File lib/rectify/format_attributes_hash.rb, line 54 def underscore_key(k) k.to_s.underscore.to_sym end