Class: Lib::Resource::Duplicate

Inherits:
Object
  • Object
show all
Defined in:
backend/app/lib/resource/duplicate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_id) ⇒ Duplicate

Returns a new instance of Duplicate.



6
7
8
9
# File 'backend/app/lib/resource/duplicate.rb', line 6

def initialize(resource_id)
  @errors = []
  @resource_id = resource_id
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors



4
5
6
# File 'backend/app/lib/resource/duplicate.rb', line 4

def errors
  @errors
end

#resourceObject (readonly)

Returns the value of attribute resource



4
5
6
# File 'backend/app/lib/resource/duplicate.rb', line 4

def resource
  @resource
end

#resource_idObject (readonly)

Returns the value of attribute resource_id



4
5
6
# File 'backend/app/lib/resource/duplicate.rb', line 4

def resource_id
  @resource_id
end

Instance Method Details

#duplicateObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'backend/app/lib/resource/duplicate.rb', line 11

def duplicate
  if resource_source.nil?
    @errors.push({ error: I18n.t('resource_duplicate_job.success_reload_message', resource_id: @resource_id) })
    return false
  end

  begin
    DB.open(DB.supports_mvcc?, :retry_on_optimistic_locking_fail => true, :isolation_level => :committed) do
      duplicate_resource

      raise Sequel::Rollback if !@errors.empty?
    end
  rescue
    last_error = $!
    @errors.push({ error: "last_error #{last_error.inspect}." }) if @errors.empty?

    return false
  end

  return false if @errors.length > 0
  return true
end

#resource_sourceObject



40
41
42
43
44
# File 'backend/app/lib/resource/duplicate.rb', line 40

def resource_source
  return @resource_source if instance_variable_defined?(:@resource_source)

  @resource_source = ::Resource.where(id: @resource_id).first
end

#resource_source_json_modelObject



34
35
36
37
38
# File 'backend/app/lib/resource/duplicate.rb', line 34

def resource_source_json_model
  return @resource_source_json_model if instance_variable_defined?(:@resource_source_json_model)

  @resource_source_json_model = ::Resource.to_jsonmodel(resource_source)
end