Class: BatchLoad::CitedObject

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_load/cited_object.rb

Overview

The citations of one citable object (e.g. an asserted distribution) while a batch file is processed: those already in the database, and those given by accepted lines of the file.

Answers "who already cites this source?" and "who already gives the original citation?" with a line number, or DATABASE, so the file and the database are one question.

Doesn't build anything itself.

Constant Summary collapse

DATABASE =
:database

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ CitedObject

Returns a new instance of CitedObject.

Parameters:

  • object (ActiveRecord::Base, nil) —

    the existing object, if there is one



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/batch_load/cited_object.rb', line 23

def initialize(object)
  @object = object
  @citing_source = {}
  @original_by = nil

  return unless object&.persisted?

  object.citations.pluck(:source_id, :is_original).each do |source_id, is_original|
    @citing_source[source_id] = DATABASE
    @original_by = DATABASE if is_original
  end
end

Instance Attribute Details

#object ⇒ ActiveRecord::Base?

Returns the cited object, in the database or built by an earlier line; nil until a line builds it.

Returns:

  • (ActiveRecord::Base, nil) —

    the cited object, in the database or built by an earlier line; nil until a line builds it



19
20
21
# File 'lib/batch_load/cited_object.rb', line 19

def object
  @object
end

Instance Method Details

#cite(line, source, is_original:) ⇒ Object

Record that line cites source.



53
54
55
56
# File 'lib/batch_load/cited_object.rb', line 53

def cite(line, source, is_original:)
  @citing_source[source.id] = line
  @original_by = line if is_original
end

#cited_by(source) ⇒ Integer, ...

Any citation of the source counts, with or without pages. That's stricter than Citation's uniqueness validation (scoped to pages), since a page-less citation next to a paged one of the same source is probably not useful.

Returns:

  • (Integer, DATABASE, nil) —

    the line, or the database, already citing the source



42
43
44
# File 'lib/batch_load/cited_object.rb', line 42

def cited_by(source)
  @citing_source[source.id]
end

#original_by ⇒ Integer, ...

Returns the line, or the database, already giving the original citation.

Returns:

  • (Integer, DATABASE, nil) —

    the line, or the database, already giving the original citation



48
49
50
# File 'lib/batch_load/cited_object.rb', line 48

def original_by
  @original_by
end