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) ⇒ 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
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.
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
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 |
#index ⇒ Object
21 22 23 |
# File 'lib/relaton/index/type.rb', line 21 def index @index ||= @file_io.read end |
#remove_all ⇒ void
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_file ⇒ void
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 |
#save ⇒ void
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
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 |