Class: Specify::Service::StubGenerator

Inherits:
Service
  • Object
show all
Defined in:
lib/specify/services/stub_generator.rb

Overview

A class that generates collection object stub records in a collection.

Instance Attribute Summary collapse

Attributes inherited from Service

#agent, #collection, #discipline, #division, #session

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Service

#database

Constructor Details

#initialize(collection:, config:, host:, database:, specify_user: nil) {|_self| ... } ⇒ StubGenerator

Returns a new instance of StubGenerator

Yields:

  • (_self)

Yield Parameters:


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/specify/services/stub_generator.rb', line 15

def initialize(collection:,
               config:,
               host:,
               database:,
               specify_user: nil)
  super
  @accession = nil
  @cataloger = agent
  @collecting_geography = nil
  @collecting_locality = nil
  @default_locality_name = 'not cataloged, see label'
  @dataset_name = "stub record set #{Time.now}"
  @preparation_type = nil
  @preparation_count = nil
  @record_set = nil
  @taxon = nil
  yield(self) if block_given?
end

Instance Attribute Details

#accessionObject

Returns the value of attribute accession


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def accession
  @accession
end

#catalogerObject

Returns the value of attribute cataloger


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def cataloger
  @cataloger
end

#collecting_geographyObject (readonly)

Returns the value of attribute collecting_geography


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def collecting_geography
  @collecting_geography
end

#collecting_localityObject (readonly)

Returns the value of attribute collecting_locality


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def collecting_locality
  @collecting_locality
end

#dataset_nameObject

Returns the value of attribute dataset_name


7
8
9
# File 'lib/specify/services/stub_generator.rb', line 7

def dataset_name
  @dataset_name
end

#default_locality_nameObject

Returns the value of attribute default_locality_name


7
8
9
# File 'lib/specify/services/stub_generator.rb', line 7

def default_locality_name
  @default_locality_name
end

#preparation_countObject (readonly)

Returns the value of attribute preparation_count


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def preparation_count
  @preparation_count
end

#preparation_typeObject (readonly)

Returns the value of attribute preparation_type


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def preparation_type
  @preparation_type
end

#record_setObject (readonly)

Returns the value of attribute record_set


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def record_set
  @record_set
end

#taxonObject (readonly)

Returns the value of attribute taxon


9
10
11
# File 'lib/specify/services/stub_generator.rb', line 9

def taxon
  @taxon
end

Class Method Details

.load_yaml(file) ⇒ Object


35
36
37
# File 'lib/specify/services/stub_generator.rb', line 35

def self.load_yaml(file)
  unwrap Psych.load_file(file)
end

.unwrap(hash) ⇒ Object

-> StubGenerator Loads a YAML file and creates an instance according to specifications in the file.


42
43
44
45
46
47
48
49
50
51
# File 'lib/specify/services/stub_generator.rb', line 42

def self.unwrap(hash)
  new hash.delete(:stub_generator) do |stubs|
    hash.each do |key, value|
      setter = (key + '=').to_sym
      puts "#{setter}#{value}"
      next unless value
      stubs.public_send(setter, value)
    end
  end
end

Instance Method Details

#collecting_data=(vals) ⇒ Object

-> Model::Locality Sets the instance's collecting_geography and collecting_locality. geography: Hash

{ 'Administrative division name' => 'Geographic name',
  locality: 'Locality name' }

77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/specify/services/stub_generator.rb', line 77

def collecting_data=(vals)
  locality = vals.delete :locality
  unless vals.empty?
    @collecting_geography = geography.search_tree(vals)
    unless @collecting_geography
      missing_geo = vals.values.join(', ')
      raise GEOGRAPHY_NOT_FOUND_ERROR + missing_geo
    end
  end
  return unless locality
  @collecting_locality = find_locality locality
  raise LOCALITY_NOT_FOUND_ERROR + locality unless collecting_locality
end

#collecting_locality!Object

-> Model::Locality Returns the collecting_locality, or the default_locality if collecting_locality is not set but collecting_geography is; creates default_locality if it does not exist in localities dataset.


96
97
98
99
100
# File 'lib/specify/services/stub_generator.rb', line 96

def collecting_locality!
  return collecting_locality if collecting_locality
  return unless collecting_geography
  default_locality!
end

#create(count) ⇒ Object

Creates Model::CollectionObject instances and persists them.


103
104
105
106
107
108
109
110
# File 'lib/specify/services/stub_generator.rb', line 103

def create(count)
  @record_set = collection.add_record_set Name: dataset_name,
                                          user: cataloger.user
  count.times do
    stub = create_stub
    @record_set.add_record_set_item collection_object: stub
  end
end

#default_localityObject

-> Model::Locality Returns the default locality.


114
115
116
# File 'lib/specify/services/stub_generator.rb', line 114

def default_locality
  find_locality default_locality_name
end

#default_locality!Object

-> Model::Locality Returns the default locality; creates it if it does not exist in localities dataset.


121
122
123
124
125
126
# File 'lib/specify/services/stub_generator.rb', line 121

def default_locality!
  return default_locality if default_locality
  default_locality ||
    discipline.add_locality(LocalityName: default_locality_name,
                            geographic_name: collecting_geography)
end

#determination=(vals) ⇒ Object

-> Model::Taxon Sets the taxon to which stub records will be determined. taxon: Hash { 'Rank name' => 'Taxon name' }


131
132
133
134
# File 'lib/specify/services/stub_generator.rb', line 131

def determination=(vals)
  @taxon = taxonomy.search_tree vals
  raise TAXON_NOT_FOUND_ERROR + vals.to_s unless taxon
end

#find_locality(locality_name) ⇒ Object

-> Model::Locality Returns the Specify::Model::Locality for locality_name from the instance's discipline's locality dataset or the instance's collecting_geography's locality dataset.


140
141
142
143
144
# File 'lib/specify/services/stub_generator.rb', line 140

def find_locality(locality_name)
  locality_matches = localities.where LocalityName: locality_name
  raise Model::AMBIGUOUS_MATCH_ERROR if locality_matches.count > 1
  locality_matches.first
end

#generatedObject

->


147
148
149
# File 'lib/specify/services/stub_generator.rb', line 147

def generated
  record_set&.collection_objects
end

#geographyObject

-> Model::Geography Returns the Specify::Model::Geography instance for the instance's discipline.


154
155
156
# File 'lib/specify/services/stub_generator.rb', line 154

def geography
  discipline.geography
end

#localitiesObject

-> Sequel::Dataset Returns a Sequel::Dataset for the instances's collecting_geography if it has one, otherwise for the instance's division.


162
163
164
165
# File 'lib/specify/services/stub_generator.rb', line 162

def localities
  @collecting_geography&.localities_dataset ||
    discipline.localities_dataset
end

#preparation=(type:, count: nil) ⇒ Object

-> Array Sets the instance's preparation_type and preparation_count prep_type: String count: Integer


172
173
174
175
176
177
178
# File 'lib/specify/services/stub_generator.rb', line 172

def preparation=(type:, count: nil)
  @preparation_type = collection.preparation_types_dataset
                                .first Name: type
  raise PREPTYPE_NOT_FOUND_ERROR + type unless preparation_type
  @preparation_count = count
  [preparation_type, preparation_count].compact
end

#taxonomyObject


180
181
182
# File 'lib/specify/services/stub_generator.rb', line 180

def taxonomy
  discipline.taxonomy
end