class Quiver::Adapter::ActiveRecord::ARecLowLevelCreator

Attributes

adapter_klass[RW]
attrs[RW]
failed[RW]
mapper_klass[RW]
original_attributes[RW]
primary_key[RW]

Public Class Methods

new(adapter_klass, original_attributes) click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 7
def initialize(adapter_klass, original_attributes)
  self.adapter_klass = adapter_klass
  self.failed = false
  self.original_attributes = original_attributes
  self.attrs = {}
end

Public Instance Methods

failed!() click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 61
def failed!
  self.failed = true
end
map(attributes, opts) click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 14
def map(attributes, opts)
  if opts[:foreign_key]
    attributes.merge!(opts[:foreign_key])
  end

  if opts[:primary] && original_attributes[:__type__]
    attributes.merge!(
      original_attributes[:__type__][:name] => original_attributes[:__type__][:value]
    )
  end

  record = record_class(opts[:to]).create(attributes)

  if record.persisted?
    attrs.merge!(record.attributes)

    if opts[:primary]
      self.primary_key = record.send(adapter_klass.primary_key_name)
    end
  else
    self.failed = true
  end
end
map_array(h, opts) click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 38
def map_array(h, opts)
  h.each do |key, items|
    attrs[key] = items.map do |attributes|
      if opts[:foreign_key]
        attributes.merge!(opts[:foreign_key])
      end

      record = record_class(opts[:to]).create(attributes)

      if record.persisted?
        attributes.merge(id: record.id)
      else
        self.failed = true
        {}
      end
    end
  end
end
result() click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 65
def result
  original_attributes.symbolize_keys.merge(attrs.merge(
    adapter_klass.primary_key_name => primary_key
  ).symbolize_keys)
end
success?() click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 57
def success?
  !failed
end

Private Instance Methods

record_class(name) click to toggle source
# File lib/quiver/adapter/arec_low_level_creator.rb, line 76
def record_class(name)
  adapter_klass.record_classes[name]
end