class Relaton::Cli::SubcommandCollection

Public Instance Methods

create(file) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 16
def create(file)
  dir = directory
  file_path = File.join dir, file
  col = Relaton::Bibcollection.new options
  if File.exist? file_path
    warn "Collection #{file} aready exist"
  else
    Dir.mkdir dir unless Dir.exist? dir
    File.write file_path, col.to_yaml, encoding: "UTF-8"
  end
end
export(file) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 149
def export(file)
  coll = read_collection File.join(directory, file)
  outfile = file.sub(/\.\w+$/, "") + ".xml"
  File.write outfile, coll.to_xml(bibdata: true), encoding: "UTF-8"
end
fetch(code) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 110
def fetch(code) # rubocop:disable Metrics/AbcSize
  doc = Relaton.db.fetch(code, options[:year]&.to_s)
  if doc
    colfile = File.join directory, options[:collection]
    coll = read_collection colfile
    coll << doc
    File.write colfile, coll.to_yaml, encoding: "UTF-8"
  else warn "No matching bibliographic entry found"
  end
end
find(text) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 87
def find(text)
  collections.each do |col|
    searcher = Relaton::FullTextSeatch.new(col[:collection])
    searcher.search text
    if searcher.any?
      puts "Collection: #{File.basename(col[:file])}"
      searcher.print_results
    end
  end
end
get(docid) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 70
def get(docid)
  collections.each do |col|
    col[:collection].items.each do |item|
      if item.docidentifier == docid
        output_item(item)
        return
      end
    end
  end
end
import(file) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 128
def import(file) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  collfile = File.join directory, options[:collection]
  coll = read_collection collfile
  xml = Nokogiri::XML File.read(file, encoding: "UTF-8")
  if xml.at "relaton-collection"
    if coll
      coll << Relaton::Bibcollection.from_xml(xml)
    else
      coll = Relaton::Bibcollection.from_xml(xml)
    end
  else
    coll ||= Relaton::Bibcollection.new({})
    coll << Relaton::Bibdata.from_xml(xml)
  end
  File.write collfile, coll.to_yaml, encoding: "UTF-8"
end
info(file) click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 32
def info(file) # rubocop:disable Metrics/AbcSize
  path = File.join directory, file
  puts "Collection: #{File.basename path}"
  puts "Last updated: #{File.mtime path}"
  puts "File size: #{File.size path}"
  col = Relaton::Bibcollection.new YAML.load_file(path)["root"]
  puts "Number of items: #{col.items.size}"
  puts "Author: #{col.author}"
  puts "Title: #{col.title}"
end
list() click to toggle source
# File lib/relaton/cli/subcommand_collection.rb, line 48
def list
  Dir[File.join(directory, "*")].each do |f|
    yml = read_yaml f
    if yml && yml["root"]
      puts File.basename f
      puts_entries yml
    end
  end
end

Private Instance Methods

collections() click to toggle source

@return [Array<Hash>]

# File lib/relaton/cli/subcommand_collection.rb, line 179
def collections
  file = options.fetch :collection, "*"
  Dir[File.join directory, file].reduce([]) do |m, f|
    yml = read_yaml f
    if yml && yml["root"]
      m << { collection: Relaton::Bibcollection.new(yml["root"]),
             file: f }
    end
    m
  end
end
directory() click to toggle source

@return [String]

# File lib/relaton/cli/subcommand_collection.rb, line 158
def directory
  options.fetch :dir, File.join(Dir.home, ".relaton/collections")
end
output_item(item) click to toggle source

@param item [Relaton::Bibdata]

# File lib/relaton/cli/subcommand_collection.rb, line 202
def output_item(item)
  case options[:format]
  when "xml" then puts item.to_xml bibdata: true
  when "abb" then puts item.to_asciibib
  else puts_human_readable_item item
  end
  out = case options[:output]
        when /\.abb$/ then item.to_asciibib
        when /\.xml$/ then item.to_xml bibitem: true
        end
  File.write options[:output], out, encoding: "UTF-8" if out
end
puts_entries(hash) click to toggle source

Puts document IDs for each item in tthe cokllection @param hash [Hash] Relaton collection

# File lib/relaton/cli/subcommand_collection.rb, line 193
def puts_entries(hash)
  return unless options[:entries]

  Relaton::Bibcollection.new(hash["root"]).items.each do |b|
    puts "  " + b.docidentifier
  end
end
puts_human_readable_item(item) click to toggle source

@param item [Relaton::Bibdata]

# File lib/relaton/cli/subcommand_collection.rb, line 216
def puts_human_readable_item(item) # rubocop:disable Metrics/AbcSize
  puts "Document identifier: #{item.docidentifier}"
  puts "Title: #{item.title.first.title.content}"
  puts "Status: #{item.status.stage}"
  item.date.each { |d| puts "Date #{d.type}: #{d.on || d.from}" }
end
read_collection(file) click to toggle source

@param file [String] @return [Relaton::Bibcollection, nil]

# File lib/relaton/cli/subcommand_collection.rb, line 172
def read_collection(file)
  return unless File.file?(file)

  Relaton::Bibcollection.new YAML.load_file(file)["root"]
end
read_yaml(file) click to toggle source

@param file [String] @return [Hash]

# File lib/relaton/cli/subcommand_collection.rb, line 164
def read_yaml(file)
  YAML.load_file file if File.file? file
rescue Psych::SyntaxError
  warn "[relaton-cli] WARNING: the file #{file} isn't a collection."
end