Class: Relaton::Cli::SubcommandCollection

Inherits:
Thor
  • Object
show all
Includes:
Relaton::Cli
Defined in:
lib/relaton/cli/subcommand_collection.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

#create(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# 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
    Util.warn "Collection `#{file}` aready exist"
  else
    FileUtils.mkdir_p dir # unless Dir.exist? dir
    File.write file_path, col.to_yaml, encoding: "UTF-8"
  end
end

#export(file) ⇒ Object



150
151
152
153
154
# File 'lib/relaton/cli/subcommand_collection.rb', line 150

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) ⇒ Object

rubocop:disable Metrics/AbcSize



110
111
112
113
114
115
116
117
118
119
# 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 Util.info "No matching bibliographic entry found"
  end
end

#find(text) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/relaton/cli/subcommand_collection.rb', line 129

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) ⇒ Object

rubocop:disable Metrics/AbcSize



32
33
34
35
36
37
38
39
40
41
# 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

#listObject



48
49
50
51
52
53
54
55
56
# 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