class Bagatelle::Mapper

Attributes

associations[R]
client[R]

Public Class Methods

children(*children) click to toggle source
# File lib/bagatelle.rb, line 45
def children(*children)
  @associations = children
end
new(client) click to toggle source
# File lib/bagatelle.rb, line 38
def initialize(client)
  @client = client
end

Public Instance Methods

associations() click to toggle source
# File lib/bagatelle.rb, line 50
def associations
  self.class.associations
end
recursive_map(table, key, value, children=[]) click to toggle source
# File lib/bagatelle.rb, line 54
def recursive_map(table, key, value, children=[])
  ids = []
  rel = []
  fkey = table.singularize.foreign_key

  # client.query(client.build_query(table, key, value)).each do |row|
  client.query(table, key, value).each do |row|
    ids << row['id']
    child_hash = Hash.new

    children.each do |child|
      if child.respond_to?(:each_pair)
        child.each_pair do |k, v|
          child_hash[k] = recursive_map(k.to_s, fkey, ids, Array(v))
        end
      else
        child_hash[child] = recursive_map(child.to_s, fkey, ids)
      end
    end

    klass = table.classify.constantize
    rel << klass.new(row.merge(child_hash))
  end
  rel
end