Class: Relaton::Index::Type

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

Overview

Relaton::Index::Type is a class for indexing Relaton files.

Instance Method Summary collapse

Constructor Details

#initialize(type, url = nil, file = nil, id_keys = nil, pubid_class = nil) ⇒ Type

Initialize a new Relaton::Index::Type object

Parameters:

  • type (String, Symbol)

    type of index (ISO, IEC, etc.)

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

    external URL to index, used to fetch index for searching files

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

    output file name

  • id_keys (Array<Symbol>) (defaults to: nil)

    keys of identifier to be used for sorting index format of index file is checked if id_keys all is provided at least in one of the IDs

  • pubid (Pubid::Core::Identifier::Base, nil)

    class for deserialization



17
18
19
20
21
# File 'lib/relaton/index/type.rb', line 17

def initialize(type, url = nil, file = nil, id_keys = nil, pubid_class = nil) # rubocop:disable Metrics/ParameterLists
  @file = file
  filename = file || Index.config.filename
  @file_io = FileIO.new type.to_s.downcase, url, filename, id_keys, pubid_class
end

Instance Method Details

#actual?(**args) ⇒ Boolean

Check if index is actual. If url or file is given, check if it is equal to index url or file.

Parameters:

  • **args (Hash)

    arguments

  • args (Hash)

    a customizable set of options

Options Hash (**args):

  • :url (String, nil)

    external URL to index, used to fetch index for searching files

  • :file (String, nil)

    output file name

Returns:

  • (Boolean)

    true if index is actual, false otherwise



37
38
39
# File 'lib/relaton/index/type.rb', line 37

def actual?(**args)
  (!args.key?(:url) || args[:url] == @file_io.url) && (!args.key?(:file) || args[:file] == @file)
end

#add_or_update(id, file) ⇒ void

This method returns an undefined value.

Add or update index item

Parameters:

  • id (Pubid::Core::Identifier::Base)

    document ID

  • file (String)

    file name of the document



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/relaton/index/type.rb', line 49

def add_or_update(id, file)
  key = id.to_s
  item = id_lookup[key]
  if item
    item[:file] = file
  else
    new_item = { id: id, file: file }
    index << new_item
    id_lookup[key] = new_item
    @file_io.sorted = false
  end
end

#indexObject



23
24
25
# File 'lib/relaton/index/type.rb', line 23

def index
  @index ||= @file_io.read
end

#remove_allvoid

This method returns an undefined value.

Remove all index items



101
102
103
104
105
# File 'lib/relaton/index/type.rb', line 101

def remove_all
  @index = []
  @id_lookup = nil
  @file_io.sorted = true
end

#remove_filevoid

This method returns an undefined value.

Remove index file from storage and clear index



90
91
92
93
94
# File 'lib/relaton/index/type.rb', line 90

def remove_file
  @file_io.remove
  @index = nil
  @id_lookup = nil
end

#savevoid

This method returns an undefined value.

Save index to storage



81
82
83
# File 'lib/relaton/index/type.rb', line 81

def save
  @file_io.save(@index || [])
end

#search(id = nil, &block) ⇒ Array<Hash>

Search index for a given ID

Parameters:

  • id (String, Pubid::Core::Identifier::Base) (defaults to: nil)

    ID to search for

Returns:

  • (Array<Hash>)

    search results



69
70
71
72
73
74
# File 'lib/relaton/index/type.rb', line 69

def search(id = nil, &block)
  items = search_candidates(id)
  return items.select(&block) if block

  items.select { |i| match_item(i, id) }
end