Class: Relaton::Index::Type
- Inherits:
-
Object
- Object
- Relaton::Index::Type
- Defined in:
- lib/relaton/index/type.rb
Overview
Relaton::Index::Type is a class for indexing Relaton files.
Instance Method Summary collapse
-
#actual?(**args) ⇒ Boolean
Check if index is actual.
-
#add_or_update(id, file) ⇒ void
Add or update index item.
- #index ⇒ Object
-
#initialize(type, url = nil, file = nil, id_keys = nil, pubid_class = nil) ⇒ Type
constructor
Initialize a new Relaton::Index::Type object.
-
#remove_all ⇒ void
Remove all index items.
-
#remove_file ⇒ void
Remove index file from storage and clear index.
-
#save ⇒ void
Save index to storage.
-
#search(id = nil, &block) ⇒ Array<Hash>
Search index for a given ID.
Constructor Details
#initialize(type, url = nil, file = nil, id_keys = nil, pubid_class = nil) ⇒ Type
Initialize a new Relaton::Index::Type object
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.
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
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 |
#index ⇒ Object
23 24 25 |
# File 'lib/relaton/index/type.rb', line 23 def index @index ||= @file_io.read end |
#remove_all ⇒ void
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_file ⇒ void
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 |
#save ⇒ void
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
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 |