Class: Materialist::Materializer::Internals::Materializer

Inherits:
Object
  • Object
show all
Defined in:
lib/materialist/materializer/internals/materializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, klass) ⇒ Materializer

Returns a new instance of Materializer.



8
9
10
11
12
# File 'lib/materialist/materializer/internals/materializer.rb', line 8

def initialize(url, klass)
  @url = url
  @instance = klass.new
  @options = klass.__materialist_options
end

Instance Method Details

#destroyObject



35
36
37
38
39
40
41
42
43
# File 'lib/materialist/materializer/internals/materializer.rb', line 35

def destroy
  return unless materialize_self?
  model_class.find_by(source_lookup(url)).tap do |entity|
    send_messages(before_destroy, entity) unless before_destroy.nil?
    entity.destroy!.tap do |entity|
      send_messages(after_destroy, entity) unless after_destroy.nil?
    end if entity
  end
end

#upsert(retry_on_race_condition: true) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/materialist/materializer/internals/materializer.rb', line 14

def upsert(retry_on_race_condition: true)
  return unless root_resource

  if materialize_self?
    upsert_record.tap do |entity|
      send_messages(after_upsert, entity) unless after_upsert.nil?
    end
  end

  materialize_links
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid
  # when there is a race condition and uniqueness of :source_url
  # is enforced by database index, this error is raised
  # so we simply try upsert again
  # if error is due to another type of uniqueness constraint
  # second call will also fail and error would bubble up
  retry_on_race_condition ?
    upsert(retry_on_race_condition: false) :
    raise
end