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.



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

def initialize(key)
  @key = key
  @name = key
  @title = nil
  @properties = {}
  @finish_flag = {}
  @working = []
  @relations = {}
  @relation_queue = []
  @real_db = nil
  @reverse_name_queue = {}
end

Instance Attribute Details

#finish_flagObject (readonly)

Returns the value of attribute finish_flag.



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

def finish_flag
  @finish_flag
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 16

def name
  @name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



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

def properties
  @properties
end

#real_dbObject (readonly)

Returns the value of attribute real_db.



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

def real_db
  @real_db
end

#relation_queueObject (readonly)

Returns the value of attribute relation_queue.



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

def relation_queue
  @relation_queue
end

#relationsObject (readonly)

Returns the value of attribute relations.



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

def relations
  @relations
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_property(key, value) ⇒ Object



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

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

#append_relation_queue(other, relation) ⇒ Object



26
27
28
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 26

def append_relation_queue(other, relation)
  @relation_queue << [relation, other]
end

#append_reverse_name_queue(other_db, forward, reverse) ⇒ Object



206
207
208
209
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 206

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

#attach_database(db) ⇒ Object



30
31
32
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 30

def attach_database(db)
  @real_db = db
end

#blocked?(name) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 45

def blocked?(name)
  @finish_flag[name].nil?
end

#countObject



49
50
51
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 49

def count
  @properties.count + @relation_queue.count + 1
end

#create_notion_db(target_page, inline) ⇒ Object

Parameters:



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

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

#remainObject



53
54
55
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 53

def remain
  count - @finish_flag.count
end

#rename_reverse_nameObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 219

def rename_reverse_name
  save = false
  reverses = []
  clears = []
  @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
      reverses << other_db.real_db
      clears << rrp
      save = true
    end
    @finish_flag[reverse] = true
  end
  if save
    @real_db.save
    clears.map(&:clear_will_update)
    reverses.map(&:reload)
  end
  @reverse_name_queue = {}
end

#update_databaseObject



211
212
213
214
215
216
217
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 211

def update_database
  update_properties
  @real_db.save
  while (name = @working.shift)
    @finish_flag[name] = true
  end
end

#update_propertiesObject



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/notion_ruby_mapping/controllers/mermaid_database.rb', line 64

def update_properties
  ps = @real_db.properties
  @properties.each do |(value, key)|
    name, *options = value.split("|")
    next if @finish_flag[name]

    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
      @working << name
    when "formula"
      f_e = options.first&.gsub "@", '"'
      dependencies = f_e&.scan(/prop\("([^"]+)"\)/) || []
      blocked_key = dependencies.select { |k| blocked? k.first }
      if blocked_key.empty?
        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
        @working << name
      else
        print("#{name} blocked by #{blocked_key.flatten}\n")
        next
      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
      @working << name
    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
      @working << name
    when "rollup"
      name, *options = value.split("|")
      relation_name, rollup_name, function = options
      if blocked? relation_name
        print("#{name} blocked by #{relation_name}\n")
        next
      else
        relation_db = @relations[relation_name]
        if relation_db.nil? || relation_db.blocked?(rollup_name)
          print "#{name} blocked by #{relation_name}-#{rollup_name}\n"
          next
        else
          property = ps.values_at(name).first

          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
          @working << name
        end
      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
      @working << name
    end
  end
  while (array = @relation_queue.shift)
    value, relation_db = array
    db_id = relation_db.real_db.id
    forward, reverse = value.split("|").map(&:to_s)
    property = ps.values_at(forward).first
    if property
      if reverse
        if property.database_id == db_id && property.synced_property_name == reverse
          relation_db.add_property "relation", reverse
          relation_db.finish_flag[reverse] = true
        else
          unless @finish_flag[forward]
            property.replace_relation_database database_id: db_id
            relation_db.append_reverse_name_queue self, forward, reverse
          end
          @working << forward
          add_property "relation", forward
        end
        relation_db.relations[reverse] = self
      else
        unless property.database_id == db_id
          property.replace_relation_database database_id: db_id, type: "single_property"
          @working << forward
          add_property "relation", forward
        end
      end
      @relations[forward] = relation_db
    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
          relation_db.relations[reverse] = self
        else
          p.replace_relation_database database_id: relation_db.real_db.id, type: "single_property"
        end
      end
      @relations[forward] = relation_db
      @working << forward
      add_property "relation", forward
    end
  end
  @real_db.property_schema_json
end

#update_titleObject



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

def update_title
  ps = @real_db.properties
  title_property = ps.select { |p| p.is_a? TitleProperty }.first
  title_property.new_name = @title unless title_property.name == @title
  @finish_flag[@title] = true
end