Class: NotionRubyMapping::MermaidDatabase

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ MermaidDatabase

Returns a new instance of MermaidDatabase.



4
5
6
7
8
9
10
11
12
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 4

def initialize(key)
  @key = key
  @name = key
  @title = nil
  @properties = {}
  @relations = {}
  @real_db = nil
  @reverse_name_queue = {}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



14
15
16
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 14

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



13
14
15
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 13

def properties
  @properties
end

#real_dbObject (readonly)

Returns the value of attribute real_db.



13
14
15
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 13

def real_db
  @real_db
end

#relationsObject (readonly)

Returns the value of attribute relations.



13
14
15
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 13

def relations
  @relations
end

#titleObject (readonly)

Returns the value of attribute title.



13
14
15
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 13

def title
  @title
end

Instance Method Details

#add_property(key, value) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 16

def add_property(key, value)
  if key == "title"
    @title = value
  else
    @properties[value] = key
  end
end

#append_relation(other, relation) ⇒ Object



24
25
26
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 24

def append_relation(other, relation)
  @relations[relation] = other
end

#append_reverse_name_queue(other_db, forward, reverse) ⇒ Object



130
131
132
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 130

def append_reverse_name_queue(other_db, forward, reverse)
  @reverse_name_queue[reverse] = [other_db, forward]
end

#attach_database(db) ⇒ Object



28
29
30
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 28

def attach_database(db)
  @real_db = db
end

#create_notion_db(target_page, inline) ⇒ Object

Parameters:



33
34
35
36
37
38
39
40
41
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 33

def create_notion_db(target_page, inline)
  unless @title
    print "Database must have a title property"
    exit
  end
  @real_db = target_page.create_child_database(@name, TitleProperty, @title) do |d, _|
    d.is_inline = inline
  end
end

#rename_reverse_nameObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 139

def rename_reverse_name
  save = false
  @reverse_name_queue.each do |reverse, (other_db, forward)|
    frp = other_db.real_db.properties[forward]
    rp_id = frp.synced_property_id
    rrp = @real_db.properties.filter { |pp| pp.property_id == rp_id }.first
    if rrp && rrp.name != reverse
      rrp.new_name = reverse
      save = true
    end
  end
  @real_db.save if save
end

#update_databaseObject



134
135
136
137
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 134

def update_database
  update_properties
  @real_db.save
end

#update_propertiesObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 43

def update_properties
  ps = @real_db.properties
  title_property = ps.select { |p| p.is_a? TitleProperty }.first
  title_property.new_name = @title unless title_property.name == @title
  @properties.each do |(value, key)|
    name, *options = value.split("|")
    property = ps.values_at(name).first
    case key
    when /checkbox|created_by|created_time|date|email|files|last_edited_by|last_edited_time|people|phone_number|text|url|status/
      klass = {
        checkbox: CheckboxProperty, created_by: CreatedByProperty, created_time: CreatedTimeProperty,
        date: DateProperty, email: EmailProperty, files: FilesProperty, last_edited_by: LastEditedByProperty,
        last_edited_time: LastEditedTimeProperty, people: PeopleProperty, phone_number: PhoneNumberProperty,
        rich_text: RichTextProperty, url: UrlProperty, status: StatusProperty
      }[key.to_sym]
      @real_db.add_property klass, name unless property
    when "formula"
      f_e = options.first&.gsub "@", '"'
      if property
        property.formula_expression = f_e unless property.formula_expression == f_e
      else
        @real_db.add_property(FormulaProperty, name) { |dp| dp.formula_expression = f_e }
      end
    when "multi_select"
      if property
        (options - (property.multi_select_options.map { |h| h["name"] })).each do |select_name|
          property.add_multi_select_option name: select_name, color: "default"
        end
      else
        @real_db.add_property(MultiSelectProperty, name) do |dp|
          options.each do |select_name|
            dp.add_multi_select_option name: select_name, color: "default"
          end
        end
      end
    when "number"
      format_value = options.empty? ? "number" : options.first
      if property
        property.format = format_value unless property.format == format_value
      else
        @real_db.add_property(NumberProperty, name) { |p| p.format = format_value }
      end
    when "select"
      if property
        (options - (property.select_options.map { |h| h["name"] })).each do |select_name|
          property.add_select_option name: select_name, color: "default"
        end
      else
        @real_db.add_property(SelectProperty, name) do |dp|
          options.each do |select_name|
            dp.add_select_option name: select_name, color: "default"
          end
        end
      end
    else
      nil
    end
  end
  @relations.each do |value, relation_db|
    db_id = relation_db.real_db.id
    forward, reverse = value.split "|"
    property = ps.values_at(forward).first
    if property
      if reverse
        next if property.database_id == db_id && property.synced_property_name == reverse

        property.replace_relation_database database_id: db_id
        relation_db.append_reverse_name_queue self, forward, reverse
      else
        next if property.database_id == db_id

        property.replace_relation_database database_id: db_id, type: "single_property"
      end
    else
      @real_db.add_property(RelationProperty, forward) do |p|
        if reverse
          p.replace_relation_database database_id: relation_db.real_db.id
          relation_db.append_reverse_name_queue self, forward, reverse
        else
          p.replace_relation_database database_id: relation_db.real_db.id, type: "single_property"
        end
      end
    end
  end
  @real_db.property_schema_json
end

#update_rollupObject



177
178
179
180
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 177

def update_rollup
  update_rollup_schema
  @real_db.save
end

#update_rollup_schemaObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 153

def update_rollup_schema
  ps = @real_db.properties
  @properties.each do |(value, key)|
    next unless key == "rollup"

    name, *options = value.split("|")
    property = ps.values_at(name).first

    relation_name, rollup_name, function = options
    if property
      property.function = function unless property.function == function
      property.relation_property_name = relation_name unless property.relation_property_name == relation_name
      property.rollup_property_name = rollup_name unless property.rollup_property_name == rollup_name
    else
      @real_db.add_property(RollupProperty, name) do |dp|
        dp.function = function
        dp.relation_property_name = relation_name
        dp.rollup_property_name = rollup_name
      end
    end
  end
  @real_db.property_schema_json
end