Class: RelatonW3c::DataIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton_w3c/data_index.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index_file: "index-w3c.yaml", index: []) ⇒ DataIndex

Initialize data index.

Parameters:

  • index_file (String) (defaults to: "index-w3c.yaml")

    path to index file

  • index (Array<Hash>) (defaults to: [])

    index data



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

Parameters:

  • index_file (String) (defaults to: "index-w3c.yaml")

    path to index file

Returns:



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

Parameters:

  • pubid (RelatonW3c::PubId)

    document number

  • file (String)

    path to document file



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

Parameters:

  • aid (Hash)

    first item

  • bid (Hash)

    second item

Returns:

  • (Integer)

    comparison result



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

Parameters:

  • date (String)

    date

Returns:

  • (String)

    weight



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

#saveObject

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

Returns:

  • (Array<Hash>)

    sorted 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

Parameters:

  • stage (String, nil)

    stage

Returns:

  • (Integer)

    weight



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