Class: NotionRubyMapping::Mermaid

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_ruby_mapping/controllers/mermaid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(full_text) ⇒ Mermaid

Returns a new instance of Mermaid.

Parameters:

  • full_text (String)


6
7
8
9
10
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 6

def initialize(full_text)
  @lines = full_text.split "\n"
  @data_sources = Hash.new {}
  parse_text
end

Instance Attribute Details

#data_sourcesObject (readonly)

Returns the value of attribute data_sources.



11
12
13
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 11

def data_sources
  @data_sources
end

#linesObject (readonly)

Returns the value of attribute lines.



11
12
13
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 11

def lines
  @lines
end

Instance Method Details

#append_db_with_attributes(db_name) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 31

def append_db_with_attributes(db_name)
  db = db_or_create db_name
  until @lines.empty?
    case @lines.shift
    when /^ *} *$/
      break
    when /^ *DataSource +title +"(.*)" *$/
      db.name = Regexp.last_match(1)
    when /^ *([^ ]+) +[^ ]+ +"(.*)" *$/
      db.add_property Regexp.last_match(1), Regexp.last_match(2)
    when /^ *([^ ]+) +([^ ]+) *$/
      db.add_property Regexp.last_match(1), Regexp.last_match(2)
    end
  end
end

#attach_data_source(ds) ⇒ Object



47
48
49
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 47

def attach_data_source(ds)
  @data_sources.values.select { |mds| mds.name == ds.data_source_title.full_text }.first&.attach_data_source(ds)
end

#countObject



71
72
73
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 71

def count
  @data_sources.values.map(&:count).sum
end

#create_notion_db(target_page, inline) ⇒ Object



51
52
53
54
55
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 51

def create_notion_db(target_page, inline)
  @data_sources.each_value do |mdb|
    mdb.create_notion_db(target_page, inline) unless mdb.real_db
  end
end

#db_or_create(db_name) ⇒ Object



27
28
29
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 27

def db_or_create(db_name)
  @data_sources[db_name] ||= MermaidDataSource.new db_name
end

#parse_textObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 13

def parse_text
  return unless @lines.shift =~ /^erDiagram/

  until @lines.empty?
    case @lines.shift
    when / *(\w+) *[|}][|o]--[o|][|{] *(\w+) *: *"(.*)" */
      db_or_create(Regexp.last_match(1)).append_relation_queue db_or_create(Regexp.last_match(2)),
                                                               Regexp.last_match(3)
    when / *(\w+) *{ */
      append_db_with_attributes Regexp.last_match(1)
    end
  end
end

#remainObject



75
76
77
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 75

def remain
  @data_sources.values.map(&:remain).sum
end

#rename_reverse_nameObject



67
68
69
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 67

def rename_reverse_name
  @data_sources.each_value(&:rename_reverse_name)
end

#update_data_sourcesObject



63
64
65
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 63

def update_data_sources
  @data_sources.each_value(&:update_database)
end

#update_titleObject



57
58
59
60
61
# File 'lib/notion_ruby_mapping/controllers/mermaid.rb', line 57

def update_title
  @data_sources.each_value do |mdb|
    mdb.update_title
  end
end