Class: RelatonIec::Index
- Inherits:
-
Object
- Object
- RelatonIec::Index
- Defined in:
- lib/relaton_iec/index.rb
Instance Method Summary collapse
-
#add(pubid, file, change = nil) ⇒ void
Add document to index or update existing document.
-
#clear ⇒ void
Clear index.
-
#initialize(index = nil) ⇒ Index
constructor
Initialize index.
-
#last_change ⇒ String?
Last change date.
-
#save ⇒ void
Save index to file.
-
#search(ref) ⇒ Array<Hash>
Find document in index by reference and sort results by document ID.
Constructor Details
#initialize(index = nil) ⇒ Index
Initialize index. If index argument is nil, read index from file or from GitHub. If index argument is not nil, then read index from file or create new empty index. (use this option for creating index for dataset)
11 12 13 14 15 16 17 18 |
# File 'lib/relaton_iec/index.rb', line 11 def initialize(index = nil) if index @path = index @index = create_index_file else @index = read_index_file || get_index_from_gh end end |
Instance Method Details
#add(pubid, file, change = nil) ⇒ void
This method returns an undefined value.
Add document to index or update existing document
29 30 31 32 33 34 35 36 37 |
# File 'lib/relaton_iec/index.rb', line 29 def add(pubid, file, change = nil) item = @index.find { |i| i[:pubid] == pubid } unless item item = { pubid: pubid } @index << item end item[:file] = file item[:last_change] = change if change end |
#clear ⇒ void
This method returns an undefined value.
Clear index
44 45 46 |
# File 'lib/relaton_iec/index.rb', line 44 def clear @index.clear end |
#last_change ⇒ String?
Last change date
53 54 55 56 57 |
# File 'lib/relaton_iec/index.rb', line 53 def last_change return unless @index.any? @last_change ||= @index.max_by { |i| i[:last_change].to_s }[:last_change] end |
#save ⇒ void
This method returns an undefined value.
Save index to file
78 79 80 |
# File 'lib/relaton_iec/index.rb', line 78 def save File.write @path, @index.to_yaml, encoding: "UTF-8" end |
#search(ref) ⇒ Array<Hash>
Find document in index by reference and sort results by document ID
66 67 68 69 70 71 |
# File 'lib/relaton_iec/index.rb', line 66 def search(ref) upcase_ref = ref.upcase @index.select do |i| RelatonBib.array(i[:pubid]).detect { |r| r.include? upcase_ref } end.sort_by { |r| r[:pubid].is_a?(Array) ? r[:pubid].min : r[:pubid] } end |