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

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

    class for deserialization



15
16
17
18
19
# File 'lib/relaton/index/type.rb', line 15

def initialize(type, url = nil, file = nil, id_keys = nil, pubid_class = nil)
  @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



35
36
37
# File 'lib/relaton/index/type.rb', line 35

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



47
48
49
50
51
52
53
54
# File 'lib/relaton/index/type.rb', line 47

def add_or_update(id, file)
  item = index.find { |i| i[:id] == id }
  if item
    item[:file] = file
  else
    index << { id: id, file: file }
  end
end

#indexObject



21
22
23
# File 'lib/relaton/index/type.rb', line 21

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

#remove_allvoid

This method returns an undefined value.

Remove all index items



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

def remove_all
  @index = []
end

#remove_filevoid

This method returns an undefined value.

Remove index file from storage and clear index



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

def remove_file
  @file_io.remove
  @index = nil
end

#savevoid

This method returns an undefined value.

Save index to storage



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

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

#search(id = nil) ⇒ 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



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/relaton/index/type.rb', line 63

def search(id = nil)
  index.select do |i|
    if block_given?
      yield(i)
    else
      if i[:id].is_a?(String)
        id.is_a?(String) ? i[:id].include?(id) : i[:id].include?(id.to_s)
      else
        id.is_a?(String) ? i[:id].to_s.include?(id) : i[:id] == id
      end
    end
  end
end