Class: Quiver::Adapter::ActiveRecord::ARecLowLevelCreator

Inherits:
Object
  • Object
show all
Defined in:
lib/quiver/adapter/arec_low_level_creator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_klass, original_attributes) ⇒ ARecLowLevelCreator

Returns a new instance of ARecLowLevelCreator.



7
8
9
10
11
12
# 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

Instance Attribute Details

#primary_keyObject

Returns the value of attribute primary_key.



5
6
7
# File 'lib/quiver/adapter/arec_low_level_creator.rb', line 5

def primary_key
  @primary_key
end

Instance Method Details

#failed!Object



61
62
63
# File 'lib/quiver/adapter/arec_low_level_creator.rb', line 61

def failed!
  self.failed = true
end

#map(attributes, opts) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#resultObject



65
66
67
68
69
# 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?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/quiver/adapter/arec_low_level_creator.rb', line 57

def success?
  !failed
end