class Syncify::NormalizeAssociations

Public Instance Methods

execute() click to toggle source
# File lib/syncify/normalize_associations.rb, line 7
def execute
  normalize_associations(association)
end

Private Instance Methods

normalize_associations(association) click to toggle source
# File lib/syncify/normalize_associations.rb, line 13
def normalize_associations(association)
  Array.wrap(
    case association
    when Symbol
      Hash[association, {}]
    when Array
      association.map { |node| normalize_associations(node) }
    when Hash
      association.reduce([]) do |memo, (key, value)|
        if polymorphic_values?(value)
          value = value.reduce({}, :merge) if value.is_a? Array
          memo << Hash[key, value]
        else
          values = normalize_associations(value)

          if values.empty?
            memo << Hash[key, {}]
          else
            values.each do |value|
              memo << Hash[key, value]
            end
          end
        end

        memo
      end
    else
      association
    end
  ).flatten
end
polymorphic_values?(values) click to toggle source
# File lib/syncify/normalize_associations.rb, line 47
def polymorphic_values?(values)
  if values.is_a? Hash
    values.keys.all? { |key| key.is_a? Class }
  elsif values.is_a? Array
    return false unless values.all? { |value| value.is_a? Hash }
    return polymorphic_values?(values.reduce({}, :merge))
  else
    false
  end
end