Class: Relaton::Cli::SubcommandCollection
- Inherits:
-
Thor
- Object
- Thor
- Relaton::Cli::SubcommandCollection
- Defined in:
- lib/relaton/cli/subcommand_collection.rb
Instance Method Summary collapse
- #create(file) ⇒ Object
- #export(file) ⇒ Object
- #fetch(code) ⇒ Object
- #find(text) ⇒ Object
- #get(docid) ⇒ Object
-
#import(file) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
-
#info(file) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #list ⇒ Object
Instance Method Details
#create(file) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 13 def create(file) dir = directory file_path = File.join dir, file col = Relaton::Bibcollection.new 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) ⇒ Object
146 147 148 149 150 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 146 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
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 107 def fetch(code) doc = Cli.relaton.fetch(code, [:year]&.to_s) if doc colfile = File.join directory, [:collection] coll = read_collection colfile coll << doc File.write colfile, coll.to_yaml, encoding: "UTF-8" else "No matching bibliographic entry found" end end |
#find(text) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 84 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
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 67 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
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 125 def import(file) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength collfile = File.join directory, [: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
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 29 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.}" puts "Title: #{col.title}" end |
#list ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/relaton/cli/subcommand_collection.rb', line 45 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 |