Module: Specify::CLI

Defined in:
lib/specify/cli/stubs.rb,
lib/specify/cli/viewset.rb,
lib/specify/cli/database_setup.rb

Class Method Summary collapse

Class Method Details

.arg_to_hash(arg) ⇒ Object


5
6
7
8
9
10
11
# File 'lib/specify/cli/stubs.rb', line 5

def self.arg_to_hash(arg)
  return unless arg
  arg.split(';')
     .map { |pair| pair.split(':').map(&:strip) }
     .to_h
     .transform_keys { |key| key == 'locality' ? key.to_sym : key }
end

.configure_database(config) ⇒ Object


5
6
7
8
9
10
11
# File 'lib/specify/cli/database_setup.rb', line 5

def self.configure_database(config)
  STDERR.puts "Configuring new database: #{config.database}"
  config.user_name = require_input 'MySQL user name'
  config.user_password = require_input 'password (blank for prompt)'
  config.session_user = require_input 'Specify user (leave blank to skip)'
  config.save
end

.configure_host(config) ⇒ Object


13
14
15
16
17
# File 'lib/specify/cli/database_setup.rb', line 13

def self.configure_host(config)
  return unless proceed? "host #{config.host} not known"
  config.port = require_input 'port number (leave blank for default)'
  config.save
end

.db_config!(file, global_options) ⇒ Object


19
20
21
22
23
24
25
# File 'lib/specify/cli/database_setup.rb', line 19

def self.db_config!(file, global_options)
  return if File.exist?(global_options[:db_config])
  STDERR.puts "Creating new config file #{file}"
  Specify::Configuration::Config.empty file do |config|
    config.add_host global_options[:host], global_options[:port]
  end
end

.level(options) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/specify/cli/viewset.rb', line 5

def self.level(options)
  if options[:d]
    :discipline
  elsif options[:c]
    :collection
  elsif options[:t]
    { user_type: options[:t] }
  elsif options[:u]
    { user: options[:u] }
  else
    raise 'level required (use -d, -c, -t, or -u option)'
  end
end

.make_stubs(generator, count) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/specify/cli/stubs.rb', line 13

def self.make_stubs(generator, count)
  STDERR.puts "started creating #{count} records"
  STDERR.puts "cataloger: #{generator.cataloger}"
  generator.database.transaction do
    generator.create count
    STDERR.puts "creating: #{generator.generated.last.catalog_number}"
  end
  STDERR.puts 'done'
  puts "generated #{generator.generated.count} catalog numbers:"
  puts '--------------------------'
  generator.generated.each { |co| puts co.CatalogNumber }
end

.proceed?(message) ⇒ Boolean


27
28
29
30
31
# File 'lib/specify/cli/database_setup.rb', line 27

def self.proceed?(message)
  STDERR.puts message
  STDERR.print "Configure? (Y/n)"
  return true if /^[Yy](es)?/.match Readline.readline(': ')
end

.require_input(message) ⇒ Object


33
34
35
36
37
38
# File 'lib/specify/cli/database_setup.rb', line 33

def self.require_input(message)
  STDERR.print message
  answer = Readline.readline(': ')
  return if answer.empty?
  answer
end

.stub_parameters(options) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/specify/cli/stubs.rb', line 38

def self.stub_parameters(options)
  params = { 'dataset_name' => options[:dataset],
             'cataloger' => options[:cataloger],
             'accession' => options[:accession],
             'collecting_data' => arg_to_hash(options[:geography]),
             'default_locality_name' => options[:locality],
             'determination' => arg_to_hash(options[:taxon]) }
  return params unless options[:preptype]
  params['preparation'] = { type: options[:preptype],
                            count: options[:prepcount] }
  params.compact
end

.wrap_args(global_options, args, options) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/specify/cli/stubs.rb', line 26

def self.wrap_args(global_options, args, options)
  params = {}
  stub_generator = {}
  stub_generator[:host] = global_options[:host]
  stub_generator[:database] = global_options[:database]
  stub_generator[:collection] = args.shift
  stub_generator[:specify_user] = global_options[:specify_user]
  stub_generator[:config] = global_options[:db_config]
  params[:stub_generator] = stub_generator
  params.merge stub_parameters(options)
end