Class: RelatonW3c::DataIndex
- Inherits:
-
Object
- Object
- RelatonW3c::DataIndex
- Defined in:
- lib/relaton_w3c/data_index.rb
Class Method Summary collapse
-
.create_from_file(index_file = "index-w3c.yaml") ⇒ RelatonW3c::DataIndex
Create index from a file.
Instance Method Summary collapse
-
#add(pubid, file) ⇒ Object
Add document to index or update it if already exists.
-
#compare_index_items(aid, bid) ⇒ Integer
Compare index items.
-
#date_weight(date) ⇒ String
Weight of date.
-
#initialize(index_file: "index-w3c.yaml", index: []) ⇒ DataIndex
constructor
Initialize data index.
-
#save ⇒ Object
Save index to file.
-
#sort! ⇒ Array<Hash>
Sort index.
-
#stage_weight(stage) ⇒ Integer
Weight of stage.
Constructor Details
#initialize(index_file: "index-w3c.yaml", index: []) ⇒ DataIndex
Initialize data index.
11 12 13 14 |
# File 'lib/relaton_w3c/data_index.rb', line 11 def initialize(index_file: "index-w3c.yaml", index: []) @index_file = index_file @index = index end |
Class Method Details
.create_from_file(index_file = "index-w3c.yaml") ⇒ RelatonW3c::DataIndex
Create index from a file
140 141 142 143 144 145 146 |
# File 'lib/relaton_w3c/data_index.rb', line 140 def create_from_file(index_file = "index-w3c.yaml") index = if File.exist?(index_file) RelatonBib.parse_yaml(File.read(index_file), [Symbol]) else [] end new index_file: index_file, index: index end |
Instance Method Details
#add(pubid, file) ⇒ Object
Add document to index or update it if already exists
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/relaton_w3c/data_index.rb', line 22 def add(pubid, file) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength # dnparts = self.class.docnumber_to_parts docnumber # pubid = PubId.parse docnumber rec = @index.detect { |i| i[:file] == file } if rec rec[:code] = pubid.code pubid.stage ? rec[:stage] = pubid.stage : rec.delete(:stage) pubid.type ? rec[:type] = pubid.type : rec.delete(:type) pubid.date ? rec[:date] = pubid.date : rec.delete(:date) pubid.suff ? rec[:suff] = pubid.suff : rec.delete(:suff) else dnparts = pubid.to_hash dnparts[:file] = file @index << dnparts end end |
#compare_index_items(aid, bid) ⇒ Integer
Compare index items
85 86 87 88 89 90 91 |
# File 'lib/relaton_w3c/data_index.rb', line 85 def compare_index_items(aid, bid) # rubocop:disable Metrics/AbcSize ret = aid[:code].downcase <=> bid[:code].downcase ret = stage_weight(bid[:stage]) <=> stage_weight(aid[:stage]) if ret.zero? ret = date_weight(bid[:date]) <=> date_weight(aid[:date]) if ret.zero? # ret = aid[:type] <=> bid[:type] if ret.zero? ret end |
#date_weight(date) ⇒ String
Weight of date
113 114 115 116 117 |
# File 'lib/relaton_w3c/data_index.rb', line 113 def date_weight(date) return "99999999" if date.nil? date end |
#save ⇒ Object
Save index to file.
42 43 44 |
# File 'lib/relaton_w3c/data_index.rb', line 42 def save File.write @index_file, @index.to_yaml, encoding: "UTF-8" end |
#sort! ⇒ Array<Hash>
Sort index
51 52 53 54 |
# File 'lib/relaton_w3c/data_index.rb', line 51 def sort! @index.sort! { |a, b| compare_index_items a, b } self end |
#stage_weight(stage) ⇒ Integer
Weight of stage
100 101 102 103 104 |
# File 'lib/relaton_w3c/data_index.rb', line 100 def stage_weight(stage) return DataParser::STAGES.size if stage.nil? DataParser::STAGES.keys.index(stage) end |