Class: SpecificationsDb

Inherits:
Object
  • Object
show all
Defined in:
lib/almirah/search/specifications_db.rb

Overview

Prepare JSON database file for further indexing by other tools

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec_list) ⇒ SpecificationsDb

Returns a new instance of SpecificationsDb.



9
10
11
12
13
# File 'lib/almirah/search/specifications_db.rb', line 9

def initialize(spec_list)
  @specifications = spec_list
  @data = []
  create_data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/almirah/search/specifications_db.rb', line 7

def data
  @data
end

#specificationsObject

Returns the value of attribute specifications.



7
8
9
# File 'lib/almirah/search/specifications_db.rb', line 7

def specifications
  @specifications
end

Instance Method Details

#add_markdown_list_item_to_db(data, item_for_reference, item_to_process) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/almirah/search/specifications_db.rb', line 34

def add_markdown_list_item_to_db(data, item_for_reference, item_to_process) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  e = nil
  item_to_process.rows.each do |r|
    if r.is_a?(MarkdownList)
      f_text = r.text
      e = {   'document' => item_for_reference.parent_doc.title, \
              'doc_color' => item_for_reference.parent_doc.color, \
              'text' => f_text, \
              'heading_url' => item_for_reference.parent_heading.get_url, \
              'heading_text' => item_for_reference.parent_heading. }
      data << e
      add_markdown_list_item_to_db(data, item_for_reference, r)
    else
      f_text = r
      e = {   'document' => item_for_reference.parent_doc.title, \
              'doc_color' => item_for_reference.parent_doc.color, \
              'text' => f_text, \
              'heading_url' => item_for_reference.parent_heading.get_url, \
              'heading_text' => item_for_reference.parent_heading. }
      data << e
    end
  end
  e
end

#add_markdown_table_item_to_db(data, item_for_reference, item_to_process) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/almirah/search/specifications_db.rb', line 59

def add_markdown_table_item_to_db(data, item_for_reference, item_to_process)
  table_text = ''
  item_to_process.rows.each do |row|
    table_text += "| #{row.join(' | ')} |"
  end
  e = {   'document' => item_for_reference.parent_doc.title, \
          'doc_color' => item_for_reference.parent_doc.color, \
          'text' => table_text, \
          'heading_url' => item_for_reference.parent_heading.get_url, \
          'heading_text' => item_for_reference.parent_heading. }
  data << e
end

#create_dataObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/almirah/search/specifications_db.rb', line 15

def create_data # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  @specifications.each do |sp|
    sp.items.each do |i|
      if (i.instance_of? Paragraph) or (i.instance_of? ControlledParagraph)
        e = { 'document' => i.parent_doc.title, \
              'doc_color' => i.parent_doc.color, \
              'text' => i.text, \
              'heading_url' => i.parent_heading.get_url, \
              'heading_text' => i.parent_heading. }
        @data.append e
      elsif i.instance_of? MarkdownList
        add_markdown_list_item_to_db(@data, i, i)
      elsif i.instance_of? MarkdownTable
        add_markdown_table_item_to_db(@data, i, i)
      end
    end
  end
end

#save(path) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/almirah/search/specifications_db.rb', line 72

def save(path)
  json = JSON.generate(@data)

  file = File.open("#{path}/specifications_db.json", 'w')
  file.puts json
  file.close
end