Class: Catalog::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/catalog/entry.rb

Overview

A Catalog::Entry has many entry items. Together Catalog::Entries form a Catalog

!! Make sure to add a dependency for all Entry types to the bottom of this file !! or `build` may not inherit correctly (would otherwise need to be above `def initialize`

Direct Known Subclasses

Nomenclature::Entry, Otu::Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, sort_order = []) ⇒ Entry

Returns a new instance of Entry.



37
38
39
40
41
42
43
# File 'lib/catalog/entry.rb', line 37

def initialize(object, sort_order = [])
  @object = object
  @items = []
  @sort_order = sort_order 
  build
  index_items
end

Instance Attribute Details

#datesScope

Returns:

  • (Scope)


21
22
23
# File 'lib/catalog/entry.rb', line 21

def dates
  @dates
end

#item_indexHash

An index of all the EntryItems by global_id taken from “object”

Returns:

  • (Hash)

    { global_id => [EntryItem] }



17
18
19
# File 'lib/catalog/entry.rb', line 17

def item_index
  @item_index
end

#itemsArray

Each item is a line item in the Entry

Returns:

  • (Array)


12
13
14
# File 'lib/catalog/entry.rb', line 12

def items
  @items
end

#objectObject

The target object for this entry



8
9
10
# File 'lib/catalog/entry.rb', line 8

def object
  @object
end

#sort_orderArray

Returns of strings, the array determines nesting order all entries in the catalog should have `sorts` of the same length !! Not yet implemented !!.

Returns:

  • (Array)

    of strings, the array determines nesting order all entries in the catalog should have `sorts` of the same length !! Not yet implemented !!



35
36
37
# File 'lib/catalog/entry.rb', line 35

def sort_order
  @sort_order
end

#sourcesScope

Returns:

  • (Scope)


25
26
27
# File 'lib/catalog/entry.rb', line 25

def sources
  @sources
end

#topicsScope

Returns:

  • (Scope)


29
30
31
# File 'lib/catalog/entry.rb', line 29

def topics
  @topics
end

Instance Method Details

#all_datesArray (protected)

Some duplication here

Returns:

  • (Array)


181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/catalog/entry.rb', line 181

def all_dates
  d = []
  sources.each do |s|
    d.push s.nomenclature_date # was cached_nomenclature_date (a Date)
  end

  items.each do |i|
    d.push i.nomenclature_date
  end
  
  d.compact.uniq.sort
end

#all_sourcesArray of Source (protected)

!! Redefined in some subclasses

Returns:



175
176
177
# File 'lib/catalog/entry.rb', line 175

def all_sources
  items.collect{|i| i.source} || []
end

#all_topicsArray of Topics (protected)

Returns:

  • (Array of Topics)


195
196
197
198
199
200
201
# File 'lib/catalog/entry.rb', line 195

def all_topics
  t = []
  sources.each do |s|
    t.push topics_for_source(s)
  end
  t.flatten.uniq.compact.sort
end

#buildObject

Redefined in subclasses! !! This is default only, it should be defined in subclasses.



47
48
49
50
# File 'lib/catalog/entry.rb', line 47

def build
  @items << Catalog::EntryItem.new(object: object, citation: object.origin_citation )
  true
end

#coordinate_entry_itemsObject



168
169
# File 'lib/catalog/entry.rb', line 168

def coordinate_entry_items
end

#date_rangeArray

Returns:

  • (Array)


130
131
132
# File 'lib/catalog/entry.rb', line 130

def date_range
  [dates.first, dates.last].compact
end

#first_item?(item) ⇒ Boolean

Named “first` to avoid conflice with `citation#is_original` Returns true if

* it's the only item with this object
* the citation is_original
* there is no original citation in any item for this object, and it's the first in the list chronologically

Inversely, returns false (in all other cases):

* its not the only one item with this object
* it doesn't have a citation that is_original
* there is no indication of the original citaiton, and the item isn't first in the list chronologically

Returns:

  • (Boolean)

    Boolean



70
71
72
73
74
75
76
77
# File 'lib/catalog/entry.rb', line 70

def first_item?(item)
  o = items_by_object(item.object)
  return true if o.size == 1
  return true if item.citation&.is_original?
  return true if !original_citation_present? && o.index(item) == 0

  false
end

#index_itemsObject



52
53
54
55
56
57
58
# File 'lib/catalog/entry.rb', line 52

def index_items
  items.each do |i|
    i.is_first = first_item?(i)
    i.is_last = last_item?(i)
  end
  true
end

#is_subsequent_entry_item?(entry_item) ⇒ Boolean

Returns:

  • (Boolean)


165
166
# File 'lib/catalog/entry.rb', line 165

def is_subsequent_entry_item?(entry_item)
end

#items_by_object(object) ⇒ Object



99
100
101
# File 'lib/catalog/entry.rb', line 99

def items_by_object(object)
  ::Catalog.chronological_item_sort( items.select{|i| i.object == object } )
end

#last_item?(item) ⇒ Boolean

Returns true if

* it's the only item with this object
* it's the last citation chronologically

Inversely, returns false in all other cases:

Returns:

  • (Boolean)

    Boolean



84
85
86
87
88
89
90
# File 'lib/catalog/entry.rb', line 84

def last_item?(item)
  o = items_by_object(item.object)
  return true if o.size == 1
  return true if o.last == item

  false
end

#ordered_by_nomenclature_dateArray of NomenclatureCatalog::EntryItem

Returns sorted by date, then taxon name name as rendered for this item.

Returns:



105
106
107
# File 'lib/catalog/entry.rb', line 105

def ordered_by_nomenclature_date
  ::Catalog.chronological_item_sort(items)
end

#original_citation_present?Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/catalog/entry.rb', line 92

def original_citation_present?
  items.each do |i|
    return true if i.citation&.is_original
  end
  false
end

#to_jsonObject



159
160
161
162
163
# File 'lib/catalog/entry.rb', line 159

def to_json
  return {
    item_count: items.count,
  }
end

#topics_for_source(source) ⇒ Array of Topics

Returns an extraction of all Topics referenced in citations that were observed in this CatalogEntry for the source TODO: SET/BUILD?.

Parameters:

Returns:

  • (Array of Topics)

    an extraction of all Topics referenced in citations that were observed in this CatalogEntry for the source TODO: SET/BUILD?



114
115
116
117
118
119
120
121
# File 'lib/catalog/entry.rb', line 114

def topics_for_source(source)
  topics = []
  items.each do |i|
    # topics += i.object.topics if i.source == source
    topics += i.topics if i.source == source
  end
  topics.uniq
end

#year_hashHash

Returns:

  • (Hash)


147
148
149
150
151
152
153
154
155
156
157
# File 'lib/catalog/entry.rb', line 147

def year_hash
  h = {}
  dates.each do |d|
    if h[d.year]
      h[d.year] += 1
    else
      h[d.year] = 1
    end
  end
  h
end