Class: RelatonIec::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_iec/index.rb

Instance Method Summary collapse

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)

Parameters:

  • index (String, nil) (defaults to: nil)

    to index file



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

Parameters:

  • pubid (String, Array<String>)

    document identifier

  • file (String)

    document file name

  • change (String) (defaults to: nil)

    last change date time



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

#clearvoid

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_changeString?

Last change date

Returns:

  • (String, nil)

    last change date or nil if index is empty



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

#savevoid

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

Parameters:

  • ref (String)

    reference

Returns:

  • (Array<Hash>)

    search result



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