Class: Relaton::Cli::SubcommandDb

Inherits:
Thor
  • Object
show all
Includes:
Relaton::Cli
Defined in:
lib/relaton/cli/subcommand_db.rb

Constant Summary

Constants included from Relaton::Cli

VERSION

Instance Method Summary collapse

Methods included from Relaton::Cli

parse_xml, processor, relaton, start, version

Instance Method Details

#clearObject



29
30
31
32
33
# File 'lib/relaton/cli/subcommand_db.rb', line 29

def clear
  db = Relaton.db
  db.clear
  Util.warn "Cache DB is cleared"
end

#create(dir = nil) ⇒ Object



10
11
12
13
14
# File 'lib/relaton/cli/subcommand_db.rb', line 10

def create(dir = nil)
  db = Relaton.db (dir && File.expand_path(dir))
  path = db.instance_variable_get(:@db).dir
  Util.warn "Cache DB is in `#{path}`"
end

#doctype(ref) ⇒ Object



69
70
71
72
# File 'lib/relaton/cli/subcommand_db.rb', line 69

def doctype(ref)
  io = IO.new($stdout.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
  io.puts Relaton.db.docid_type(ref)
end

#fetch(code) ⇒ Object



44
45
46
47
48
# File 'lib/relaton/cli/subcommand_db.rb', line 44

def fetch(code)
  io = IO.new($stdout.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
  opts = options.merge(fetch_db: true)
  io.puts(fetch_document(code, opts) || supported_type_message)
end

#fetch_all(text = nil) ⇒ Object

rubocop:disable Metrics/AbcSize



57
58
59
60
61
62
63
64
65
# File 'lib/relaton/cli/subcommand_db.rb', line 57

def fetch_all(text = nil) # rubocop:disable Metrics/AbcSize
  io = IO.new($stdout.fcntl(::Fcntl::F_DUPFD), mode: "w:UTF-8")
  opts = options.each_with_object({}) do |(k, v), o|
    o[k.to_sym] = v unless k == "format"
  end
  Relaton.db.fetch_all(text, **opts).each do |doc|
    io.puts serialize(doc, options[:format])
  end
end

#mv(dir) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/relaton/cli/subcommand_db.rb', line 18

def mv(dir)
  new_path = File.expand_path dir
  path = Relaton.db.mv new_path
  if path
    File.write Cli::RelatonDb::DBCONF, path, encoding: "UTF-8"
    Util.warn "Cache DB is moved to `#{path}`"
  end
end