Class: Catalog

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

Overview

A Catalog is a series of catalog entries, which in turn have entry items. The model more or less represents a two level

Direct Known Subclasses

CollectionObject, Nomenclature, Timeline

Defined Under Namespace

Classes: CollectionObject, Entry, EntryItem, Nomenclature, Timeline

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(targets:) ⇒ Catalog

Returns a new instance of Catalog.



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

def initialize(targets:)
  @catalog_targets = targets
  @entries = []
  build
end

Instance Attribute Details

#catalog_targetsObject

Each Object is the basis for a Catalog::Entry



5
6
7
# File 'lib/catalog.rb', line 5

def catalog_targets
  @catalog_targets
end

#entriesObject

Array of Catalog::Entry


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

def entries
  @entries
end

Class Method Details

.all_dates(source_list, item_list) ⇒ Array

Returns do not uniq! used in counts.

Returns:

  • (Array)

    do not uniq! used in counts



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

def self.all_dates(source_list, item_list)
  d = []
   #source_list.each do |s|
   #  d.push s.respond_to?(:nomenclature_date) ? s.nomenclature_date : nil # was cached_nomenclature_date (a Date)
   #end

  item_list.each do |i|
    d.push i.nomenclature_date
  end

  d.compact.uniq.sort
end

.chronological_item_sort(entry_items) ⇒ Object



61
62
63
64
# File 'lib/catalog.rb', line 61

def self.chronological_item_sort(entry_items)
  now = Time.now
  entry_items.sort{|a,b| [(a.nomenclature_date || now) ] <=> [(b.nomenclature_date || now) ] }
end

.topic_year_metadata(entry_item_list) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/catalog.rb', line 136

def self.(entry_item_list)
  h = {}
  entry_item_list.each do |i|
    y = i.nomenclature_date&.year
    y ||= 'unknown'
    i.topics.each do |t|
      id = t.metamorphosize.to_global_id.to_s
      if h[id]
        if h[id][y]
          h[id][y] += 1
        else
          h[id][y] = 1
        end
      else
        h[id] = {y => 1}
      end
    end
  end
  h
end

.year_hash(date_list) ⇒ Hash

Returns:

  • (Hash)


165
166
167
168
169
170
171
172
173
174
175
# File 'lib/catalog.rb', line 165

def self.year_hash(date_list)
  h = {}
  date_list.each do |d|
    if h[d.year]
      h[d.year] += 1
    else
      h[d.year] = 1
    end
  end
  h
end

.year_metadata(source_list, item_list) ⇒ Hash

Returns:

  • (Hash)


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

def self.(source_list, item_list)
  ::Catalog.year_hash(
    ::Catalog.all_dates(source_list, item_list)
  )
end

Instance Method Details

#buildObject

Handled in individual subclasses



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

def build
  false
end

#citationsObject



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

def citations
  t = []
  entries.each do |e|
    t += e.all_citations
  end
  t.uniq.sort{|a, b| (a&.source.cached_nomenclature_date || Time.now) <=> (b&.source.cached_nomenclature_date || Time.now)}.compact
end

#entries_sortedObject



49
50
51
# File 'lib/catalog.rb', line 49

def entries_sorted
  # TODO: sort!
end

#entry_sort_valid?Boolean

Returns:

  • (Boolean)


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

def entry_sort_valid?
  i = entries.first&.sort_order
  entries.each do |e|
    return false if e.sort_order.size != i
  end
  true
end

#itemsObject



41
42
43
# File 'lib/catalog.rb', line 41

def items
  entries&.collect{|e| e.items}&.flatten || []
end

#items_chronologicallyObject



45
46
47
# File 'lib/catalog.rb', line 45

def items_chronologically
  Catalog.chronological_item_sort(items)
end

#objects_for_source(source) ⇒ Object

TODO: optimize ;)



93
94
95
96
97
98
99
# File 'lib/catalog.rb', line 93

def objects_for_source(source)
  d = []
  items.each do |i|
    d << i if i.in_source?(source)
  end
  d.uniq
end

#reference_object_global_idObject



21
22
23
24
25
26
27
# File 'lib/catalog.rb', line 21

def reference_object_global_id
  if catalog_targets.size == 1
    catalog_targets.first.to_global_id.to_s
  else
    nil
  end
end

#reference_object_valid_taxon_name_global_idObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/catalog.rb', line 29

def reference_object_valid_taxon_name_global_id
  if catalog_targets.size == 1
    o = catalog_targets.first
    if o.class.name == 'Otu'
      return o.taxon_name&.get_valid_taxon_name&.to_global_id.to_s
    elsif o.class.name == 'Protonym'
      return o.get_valid_taxon_name&.to_global_id.to_s
    end
  end
  nil
end

#sourcesObject



76
77
78
79
80
81
82
# File 'lib/catalog.rb', line 76

def sources
  t = []
  entries.each do |e|
    t += e.sources
  end
  t.uniq.sort{|a, b| (a&.cached_nomenclature_date || Time.now) <=> (b&.cached_nomenclature_date || Time.now)}.compact
end

#sources_to_jsonHash

Returns:

  • (Hash)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/catalog.rb', line 102

def sources_to_json
  h = {
    list: {},
    year_metadata: ::Catalog.(sources, items)
  }

  sources.each do |s|
    h[:list][s.metamorphosize.to_global_id.to_s] = {
      cached: s.cached,
      year: s.cached_nomenclature_date&.year,
      objects: objects_for_source(s).collect{|o| o.object.to_global_id.to_s }
    }
  end
  h
end

#topicsArray of TaxonName

Returns all topics observed in this catalog. For example the index.

Returns:

  • (Array of TaxonName)

    all topics observed in this catalog. For example the index.



68
69
70
71
72
73
74
# File 'lib/catalog.rb', line 68

def topics
  t = []
  entries.each do |e|
    t += e.topics
  end
  t.uniq
end

#topics_to_jsonObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/catalog.rb', line 118

def topics_to_json
  h = {
    list: {}
  }

  year_data = ::Catalog.(items)

  topics.each do |t|
    id = t.metamorphosize.to_global_id.to_s
    h[:list][id] = {
      name: t.name,
      css_color: t.css_color,
      year_metadata: year_data[id]
    }
  end
  h
end