notion_ruby_mapping 0.3.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +34 -1224
  3. data/env.yml.sample +0 -13
  4. data/lib/notion_ruby_mapping/{base.rb → blocks/base.rb} +166 -90
  5. data/lib/notion_ruby_mapping/blocks/block.rb +565 -0
  6. data/lib/notion_ruby_mapping/{database.rb → blocks/database.rb} +40 -9
  7. data/lib/notion_ruby_mapping/{list.rb → blocks/list.rb} +35 -3
  8. data/lib/notion_ruby_mapping/blocks/page.rb +71 -0
  9. data/lib/notion_ruby_mapping/{notion_cache.rb → controllers/notion_cache.rb} +134 -98
  10. data/lib/notion_ruby_mapping/{payload.rb → controllers/payload.rb} +0 -0
  11. data/lib/notion_ruby_mapping/{property_cache.rb → controllers/property_cache.rb} +21 -20
  12. data/lib/notion_ruby_mapping/{query.rb → controllers/query.rb} +26 -18
  13. data/lib/notion_ruby_mapping/{rich_text_array.rb → controllers/rich_text_array.rb} +26 -15
  14. data/lib/notion_ruby_mapping/objects/emoji_object.rb +40 -0
  15. data/lib/notion_ruby_mapping/objects/equation_object.rb +43 -0
  16. data/lib/notion_ruby_mapping/objects/file_object.rb +60 -0
  17. data/lib/notion_ruby_mapping/{mention_object.rb → objects/mention_object.rb} +11 -0
  18. data/lib/notion_ruby_mapping/{rich_text_object.rb → objects/rich_text_object.rb} +11 -0
  19. data/lib/notion_ruby_mapping/{text_object.rb → objects/text_object.rb} +0 -1
  20. data/lib/notion_ruby_mapping/{user_object.rb → objects/user_object.rb} +9 -4
  21. data/lib/notion_ruby_mapping/{checkbox_property.rb → properties/checkbox_property.rb} +3 -0
  22. data/lib/notion_ruby_mapping/{created_by_property.rb → properties/created_by_property.rb} +0 -0
  23. data/lib/notion_ruby_mapping/{created_time_property.rb → properties/created_time_property.rb} +1 -0
  24. data/lib/notion_ruby_mapping/properties/date_base_property.rb +122 -0
  25. data/lib/notion_ruby_mapping/{date_property.rb → properties/date_property.rb} +13 -0
  26. data/lib/notion_ruby_mapping/{email_property.rb → properties/email_property.rb} +1 -0
  27. data/lib/notion_ruby_mapping/{files_property.rb → properties/files_property.rb} +26 -13
  28. data/lib/notion_ruby_mapping/{formula_property.rb → properties/formula_property.rb} +4 -0
  29. data/lib/notion_ruby_mapping/{last_edited_by_property.rb → properties/last_edited_by_property.rb} +1 -0
  30. data/lib/notion_ruby_mapping/{last_edited_time_property.rb → properties/last_edited_time_property.rb} +1 -0
  31. data/lib/notion_ruby_mapping/{multi_property.rb → properties/multi_property.rb} +0 -0
  32. data/lib/notion_ruby_mapping/{multi_select_property.rb → properties/multi_select_property.rb} +2 -3
  33. data/lib/notion_ruby_mapping/{number_property.rb → properties/number_property.rb} +4 -0
  34. data/lib/notion_ruby_mapping/{people_property.rb → properties/people_property.rb} +8 -3
  35. data/lib/notion_ruby_mapping/{phone_number_property.rb → properties/phone_number_property.rb} +4 -0
  36. data/lib/notion_ruby_mapping/{property.rb → properties/property.rb} +33 -0
  37. data/lib/notion_ruby_mapping/{relation_property.rb → properties/relation_property.rb} +30 -10
  38. data/lib/notion_ruby_mapping/{rich_text_property.rb → properties/rich_text_property.rb} +0 -0
  39. data/lib/notion_ruby_mapping/{rollup_property.rb → properties/rollup_property.rb} +7 -0
  40. data/lib/notion_ruby_mapping/{select_property.rb → properties/select_property.rb} +6 -1
  41. data/lib/notion_ruby_mapping/{text_property.rb → properties/text_property.rb} +0 -0
  42. data/lib/notion_ruby_mapping/{title_property.rb → properties/title_property.rb} +0 -0
  43. data/lib/notion_ruby_mapping/{url_property.rb → properties/url_property.rb} +5 -0
  44. data/lib/notion_ruby_mapping/version.rb +1 -1
  45. data/lib/notion_ruby_mapping.rb +14 -7
  46. metadata +43 -40
  47. data/lib/notion_ruby_mapping/block.rb +0 -10
  48. data/lib/notion_ruby_mapping/date_base_property.rb +0 -75
  49. data/lib/notion_ruby_mapping/page.rb +0 -50
@@ -0,0 +1,565 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NotionRubyMapping
4
+ # Notion block
5
+ class Block < Base
6
+ ### Public announced methods
7
+
8
+ def initialize(json: nil, id: nil, parent: nil)
9
+ super
10
+ @can_have_children = false
11
+ @can_append = true
12
+ end
13
+ attr_reader :can_have_children, :can_append, :type
14
+
15
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#298916c7c379424682f39ff09ee38544
16
+ # @param [String] id
17
+ # @return [NotionRubyMapping::Block]
18
+ def self.find(id, dry_run: false)
19
+ nc = NotionCache.instance
20
+ if dry_run
21
+ Base.dry_run_script :get, nc.block_path(id)
22
+ else
23
+ nc.block id
24
+ end
25
+ end
26
+
27
+ # @return [Hash{String (frozen)->String (frozen) | Array}, nil]
28
+ def block_json
29
+ ans = {"type" => @type}
30
+ ans["object"] = "block"
31
+ ans["archived"] = true if @archived
32
+ ans["has_children"] = true if @has_children
33
+ case @type
34
+ when "bookmark", "embed"
35
+ @caption.will_update = true
36
+ ans[@type] = @caption.update_property_schema_json
37
+ ans[@type]["url"] = @url
38
+ when "breadcrumb", "divider"
39
+ ans[@type] = {}
40
+ when "callout"
41
+ @rich_text_array.will_update = true
42
+ ans[@type] = @rich_text_array.update_property_schema_json
43
+ ans[@type]["color"] = @color
44
+ ans[@type]["icon"] = @emoji.property_values_json if @emoji
45
+ ans[@type]["icon"] = @file.property_values_json if @file
46
+ ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
47
+ when "child_database", "child_page"
48
+ ans[@type] = {"title" => @title}
49
+ when "code"
50
+ @rich_text_array.will_update = true
51
+ @caption.will_update = true
52
+ ans[@type] = @rich_text_array.update_property_schema_json.merge @caption.update_property_schema_json
53
+ ans[@type]["language"] = @language
54
+ when "column"
55
+ ans[@type] = {}
56
+ ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
57
+ @can_append = false
58
+ when "column_list"
59
+ ans[@type] = {}
60
+ ans[@type]["children"] = @columns.map(&:block_json) if @columns
61
+ when "paragraph", "bulleted_list_item", "heading_1", "heading_2", "heading_3", "numbered_list_item", "quote",
62
+ "toggle"
63
+ @rich_text_array.will_update = true
64
+ ans[@type] = @rich_text_array.update_property_schema_json
65
+ ans[@type]["color"] = @color
66
+ ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
67
+ when "equation"
68
+ ans[@type] = {"expression" => @equation_object.expression}
69
+ when "file", "image", "pdf", "video"
70
+ @caption.will_update = true if @caption
71
+ ans[@type] = @file_object.property_values_json
72
+ ans[@type].merge! @caption.update_property_schema_json if @caption
73
+ when "link_preview"
74
+ ans[@type] = {"url" => @url}
75
+ when "link_to_page"
76
+ ans[@type] = if @page_id
77
+ {"type" => "page_id", "page_id" => @page_id}
78
+ else
79
+ {"type" => "database_id", "database_id" => @database_id}
80
+ end
81
+ when "synced_block"
82
+ ans[@type] = {"synced_from" => @block_id ? {"type" => "block_id", "block_id" => @block_id} : nil}
83
+ ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
84
+ when "table"
85
+ ans[@type] = {
86
+ "has_column_header" => @has_column_header,
87
+ "has_row_header" => @has_row_header,
88
+ "table_width" => @table_width,
89
+ }
90
+ ans[@type]["children"] = @table_rows.map(&:block_json) if @table_rows
91
+ when "table_of_contents"
92
+ ans[@type] = {"color" => @color}
93
+ when "table_row"
94
+ ans[@type] = {"cells" => @cells.map { |cell| Array(cell).map(&:property_values_json) }}
95
+ when "template"
96
+ @rich_text_array.will_update = true
97
+ ans[@type] = @rich_text_array.update_property_schema_json
98
+ ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
99
+ when "to_do"
100
+ @rich_text_array.will_update = true
101
+ ans[@type] = @rich_text_array.update_property_schema_json
102
+ ans[@type]["checked"] = @checked
103
+ ans[@type]["color"] = @color
104
+ ans[@type]["children"] = @sub_blocks.map(&:block_json) if @sub_blocks
105
+ else
106
+ print "not yet implemented"
107
+ end
108
+ ans
109
+ end
110
+
111
+ # @param [Sting] url
112
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] caption
113
+ # @return [NotionRubyMapping::Block]
114
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#ff5299524c434c27bd79e4d942922928
115
+ def bookmark(url, caption: [])
116
+ @type = __method__.to_s
117
+ @url = url
118
+ @caption = RichTextArray.rich_text_array "caption", caption
119
+ self
120
+ end
121
+
122
+ # @return [NotionRubyMapping::Block]
123
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#f0a9dc4e0823485abe5b76051053e877
124
+ def breadcrumb
125
+ @type = __method__.to_s
126
+ self
127
+ end
128
+
129
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
130
+ # @param [String] color
131
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
132
+ # @return [NotionRubyMapping::Block]
133
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#a97ff4121e724ee1aa45e194d0a76099
134
+ def bulleted_list_item(text_info, sub_blocks: nil, color: "default")
135
+ @type = __method__.to_s
136
+ rich_text_array_and_color "rich_text", text_info, color
137
+ add_sub_blocks sub_blocks
138
+ self
139
+ end
140
+
141
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
142
+ # @param [String] emoji
143
+ # @param [String] file_url
144
+ # @param [String] color
145
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
146
+ # @return [NotionRubyMapping::Block]
147
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#39b874d7b97749a49eebb9a5f48747a1
148
+ def callout(text_info, emoji: nil, file_url: nil, sub_blocks: nil, color: "default")
149
+ @type = __method__.to_s
150
+ rich_text_array_and_color "rich_text", text_info, color
151
+ @emoji = EmojiObject.emoji_object emoji if emoji
152
+ @file = FileObject.file_object file_url if file_url
153
+ add_sub_blocks sub_blocks
154
+ self
155
+ end
156
+
157
+ # @return [Hash{String (frozen)->Array<Hash{String (frozen)->Hash}>}]
158
+ def children_block_json
159
+ {"children" => [block_json]}
160
+ end
161
+
162
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
163
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] caption
164
+ # @param [String] language
165
+ # @return [NotionRubyMapping::Block]
166
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#90f1ebf5ca3f4b479471e6a76ef8c169
167
+ def code(text_info, caption: [], language: "shell")
168
+ @type = __method__.to_s
169
+ rich_text_array_and_color "rich_text", text_info
170
+ @caption = RichTextArray.rich_text_array "caption", caption
171
+ @language = language
172
+ self
173
+ end
174
+
175
+ # @return [NotionRubyMapping::Block]
176
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
177
+ # @note created by column_list only
178
+ def column(sub_blocks)
179
+ @type = __method__.to_s
180
+ add_sub_blocks sub_blocks
181
+ self
182
+ end
183
+
184
+ # @param [Array<Array<NotionRubyMapping::Block, Array<NotionRubyMapping::Block>>>] array_of_sub_blocks
185
+ # @return [NotionRubyMapping::Block]
186
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#5413288d6d2b4f0f8fe331b0e8e643ca
187
+ def column_list(array_of_sub_blocks)
188
+ @type = __method__.to_s
189
+ raise StandardError, "The column_list must have at least 2 columns." if array_of_sub_blocks.count < 2
190
+
191
+ @columns = array_of_sub_blocks.map { |sub_blocks| Block.new.column(sub_blocks) }
192
+ self
193
+ end
194
+
195
+ # @return [NotionRubyMapping::Block]
196
+ def decode_block
197
+ @type = @json["type"]
198
+ sub_json = @json[@type]
199
+ case @type
200
+ when "bookmark", "embed"
201
+ @url = sub_json["url"]
202
+ decode_block_caption
203
+ when "bulleted_list_item", "paragraph", "numbered_list_item", "toggle", "heading_1", "heading_2", "heading_3"
204
+ decode_block_rich_text_array
205
+ decode_color
206
+ @can_have_children = true
207
+ when "quote"
208
+ decode_block_rich_text_array
209
+ decode_color
210
+ @can_have_children = true
211
+ when "callout"
212
+ decode_block_rich_text_array
213
+ decode_color
214
+ @can_have_children = true
215
+ when "child_database", "child_page"
216
+ @title = sub_json["title"]
217
+ @can_append = false
218
+ when "code"
219
+ decode_block_rich_text_array
220
+ decode_block_caption
221
+ @language = sub_json["language"] || "shell"
222
+ when "equation"
223
+ @equation_object = EquationObject.equation_object sub_json["expression"]
224
+ when "file", "image", "pdf", "video"
225
+ @file_object = FileObject.new json: sub_json
226
+ decode_block_caption
227
+ @can_append = @file_object.external?
228
+ when "link_preview"
229
+ @url = sub_json["url"]
230
+ @can_append = false
231
+ when "link_to_page"
232
+ @page_id, @database_id = sub_json.values_at(*%w[page_id database_id])
233
+ when "synced_block"
234
+ @block_id = sub_json["synced_from"] && sub_json["synced_from"]["block_id"]
235
+ @can_have_children = @block_id.nil?
236
+ when "table"
237
+ @has_column_header = sub_json["has_column_header"]
238
+ @has_row_header = sub_json["has_row_header"]
239
+ @table_width = sub_json["table_width"]
240
+ @can_have_children = true
241
+ when "table_of_contents"
242
+ decode_color
243
+ when "table_row"
244
+ print sub_json["cells"]
245
+ @cells = sub_json["cells"].map { |cell| cell.map { |to| RichTextObject.create_from_json to } }
246
+ when "template"
247
+ decode_block_rich_text_array
248
+ @can_have_children = true
249
+ when "to_do"
250
+ decode_block_rich_text_array
251
+ decode_color
252
+ @checked = sub_json["checked"]
253
+ @can_have_children = true
254
+ else
255
+ print "not yet implemented"
256
+ end
257
+ @has_children = @json["has_children"] == "true"
258
+ @archived = @json["archived"] == "true"
259
+ self
260
+ end
261
+
262
+ # @return [NotionRubyMapping::RichTextArray]
263
+ def decode_block_caption
264
+ @caption = RichTextArray.new "caption", json: @json[@type]["caption"]
265
+ end
266
+
267
+ # @return [String]
268
+ def decode_color
269
+ @color = @json[@type]["color"]
270
+ end
271
+
272
+ # @return [NotionRubyMapping::RichTextArray]
273
+ def decode_block_rich_text_array
274
+ @rich_text_array = RichTextArray.new "rich_text", json: @json[@type]["rich_text"]
275
+ end
276
+
277
+ # @param [Boolean] dry_run
278
+ # @return [NotionRubyMapping::Base, String]
279
+ def destroy(dry_run: false)
280
+ if dry_run
281
+ Base.dry_run_script :delete, @nc.block_path(@id)
282
+ else
283
+ @nc.destroy_block id
284
+ end
285
+ end
286
+
287
+ # @return [NotionRubyMapping::Block]
288
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#fe58d26468e04b38b8ae730e4f0c74ae
289
+ def divider
290
+ @type = __method__.to_s
291
+ self
292
+ end
293
+
294
+ # @param [String] url
295
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] caption
296
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#1a69571806c24035b93adec8c8480d87
297
+ def embed(url, caption: [])
298
+ @type = __method__.to_s
299
+ @url = url
300
+ @caption = RichTextArray.rich_text_array "caption", caption
301
+ self
302
+ end
303
+
304
+ # @param [String, NotionRubyMapping::EquationObject] expression
305
+ # @return [NotionRubyMapping::Block]
306
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#7e284283779c443a9e26a7bfd10f1b98
307
+ def equation(expression)
308
+ @type = __method__.to_s
309
+ @equation_object = EquationObject.equation_object expression
310
+ self
311
+ end
312
+
313
+ # @param [String] url
314
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] caption
315
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#6177fa62ef1e408ca498ebe8d8580d65
316
+ def file(url, caption: [])
317
+ @type = __method__.to_s
318
+ @file_object = FileObject.file_object url
319
+ @caption = RichTextArray.rich_text_array "caption", caption
320
+ self
321
+ end
322
+
323
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
324
+ # @param [String] color
325
+ # @return [NotionRubyMapping::Block]
326
+ def heading_1(text_info, color: "default")
327
+ @type = __method__.to_s
328
+ rich_text_array_and_color "rich_text", text_info, color
329
+ @can_have_children = true
330
+ self
331
+ end
332
+
333
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
334
+ # @param [String] color
335
+ # @return [NotionRubyMapping::Block]
336
+ def heading_2(text_info, color: "default")
337
+ @type = __method__.to_s
338
+ rich_text_array_and_color "rich_text", text_info, color
339
+ @can_have_children = true
340
+ self
341
+ end
342
+
343
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
344
+ # @param [String] color
345
+ # @return [NotionRubyMapping::Block]
346
+ def heading_3(text_info, color: "default")
347
+ @type = __method__.to_s
348
+ rich_text_array_and_color "rich_text", text_info, color
349
+ @can_have_children = true
350
+ self
351
+ end
352
+
353
+ # @param [String] url
354
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] caption
355
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#7aad77515ce14e3bbc7e0a7a5427820b
356
+ def image(url, caption: [])
357
+ @type = __method__.to_s
358
+ @file_object = FileObject.file_object url
359
+ @caption = RichTextArray.rich_text_array "caption", caption
360
+ self
361
+ end
362
+
363
+ # @param [String] page_id
364
+ # @param [String] database_id
365
+ # @return [NotionRubyMapping::Block]
366
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#aabda81c11a949d5ba2e73cf9b50d0ad
367
+ def link_to_page(page_id: nil, database_id: nil)
368
+ raise StandardError, "page_id or database_id is required." if page_id.nil? && database_id.nil?
369
+
370
+ @type = __method__.to_s
371
+ @page_id = page_id
372
+ @database_id = database_id
373
+ self
374
+ end
375
+
376
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
377
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
378
+ # @param [String] color
379
+ # @return [NotionRubyMapping::Block]
380
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#186bfd02b51946bcbc732063e6c3af1c
381
+ def numbered_list_item(text_info, sub_blocks: nil, color: "default")
382
+ @type = __method__.to_s
383
+ rich_text_array_and_color "rich_text", text_info, color
384
+ add_sub_blocks sub_blocks
385
+ self
386
+ end
387
+
388
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
389
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
390
+ # @param [String] color
391
+ # @return [NotionRubyMapping::Block]
392
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#f3c4fd6a1578429680a5921cbe03c560
393
+ def paragraph(text_info, sub_blocks: nil, color: "default")
394
+ @type = __method__.to_s
395
+ rich_text_array_and_color "rich_text", text_info, color
396
+ add_sub_blocks sub_blocks
397
+ self
398
+ end
399
+
400
+ # @param [String] url
401
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#cd22a466ded048f686ad1f201588d42a
402
+ def pdf(url)
403
+ @type = __method__.to_s
404
+ @file_object = FileObject.file_object url
405
+ self
406
+ end
407
+
408
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
409
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
410
+ # @param [String] color
411
+ # @return [NotionRubyMapping::Block]
412
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#0a1ed3fc49d544d18606b84d59727b27
413
+ def quote(text_info, sub_blocks: nil, color: "default")
414
+ @type = __method__.to_s
415
+ rich_text_array_and_color "rich_text", text_info, color
416
+ add_sub_blocks sub_blocks
417
+ self
418
+ end
419
+
420
+ # @param [Array<Block>] sub_blocks
421
+ # @param [String] block_id
422
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#69fa1e60d151454aac3c7209a1c41793
423
+ def synced_block(sub_blocks: nil, block_id: nil)
424
+ raise StandardError, "blocks or block_id cannot set at the same time." if !sub_blocks.nil? && !block_id.nil?
425
+
426
+ @type = __method__.to_s
427
+ @sub_blocks = Array(sub_blocks) if sub_blocks
428
+ @block_id = block_id
429
+ @can_have_children = @block_id.nil?
430
+ self
431
+ end
432
+
433
+ # @return [Boolean] true if synced_block & block_id is nil
434
+ def synced_block_original?
435
+ @type == "synced_block" && @block_id.nil?
436
+ end
437
+
438
+ def table(table_width:, has_column_header: false, has_row_header: false, table_rows: nil)
439
+ @type = __method__.to_s
440
+ @table_width = table_width
441
+ @has_column_header = has_column_header
442
+ @has_row_header = has_row_header
443
+ if table_rows
444
+ @table_rows = table_rows.map do |table_row|
445
+ Block.new.table_row table_row, @table_width
446
+ end
447
+ end
448
+ self
449
+ end
450
+
451
+ # @param [String] color
452
+ # @return [NotionRubyMapping::Block]
453
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#272b41e76bef4f3d8e7386369475910e
454
+ def table_of_contents(color: "default")
455
+ @type = __method__.to_s
456
+ @color = color
457
+ self
458
+ end
459
+
460
+ # @param [Array<String, Array<String>, TextObject, Array<TextObject>>] array_array_of_text_objects
461
+ # @return [NotionRubyMapping::Block]
462
+ def table_row(array_array_of_text_objects, table_width)
463
+ @type = __method__.to_s
464
+ cc = array_array_of_text_objects.count
465
+ raise StandardError, "table width must be #{table_width} (given array size is #{cc}" unless table_width == cc
466
+
467
+ @cells = array_array_of_text_objects.map do |cell|
468
+ Array(cell).map { |text_info| TextObject.text_object text_info }
469
+ end
470
+ self
471
+ end
472
+
473
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
474
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
475
+ # @return [NotionRubyMapping::Block]
476
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#840eefedd571423e8532ab9042cc0fc1
477
+ def template(text_info, sub_blocks: nil)
478
+ @type = __method__.to_s
479
+ rich_text_array_and_color "rich_text", text_info, nil
480
+ add_sub_blocks sub_blocks
481
+ self
482
+ end
483
+
484
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
485
+ # @param [Boolean] checked
486
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
487
+ # @param [String] color
488
+ # @return [NotionRubyMapping::Block]
489
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#386e74f3760b43b49a1c3316532f252d
490
+ def to_do(text_info, checked: false, sub_blocks: nil, color: "default")
491
+ @type = __method__.to_s
492
+ rich_text_array_and_color "rich_text", text_info, color
493
+ add_sub_blocks sub_blocks
494
+ @checked = checked
495
+ self
496
+ end
497
+
498
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
499
+ # @param [Boolean] checked
500
+ # @param [String] color
501
+ # @return [NotionRubyMapping::Block]
502
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#5a156ee95d0f4a7a83689f7ce5830296
503
+ def toggle(text_info, checked: false, sub_blocks: nil, color: "default")
504
+ @type = __method__.to_s
505
+ rich_text_array_and_color "rich_text", text_info, color
506
+ @checked = checked
507
+ add_sub_blocks sub_blocks
508
+ self
509
+ end
510
+
511
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
512
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
513
+ # @param [String] color
514
+ # @return [NotionRubyMapping::Block]
515
+ # @see
516
+ def toggle_heading_1(text_info, sub_blocks:, color: "default")
517
+ heading_1 text_info, color: color
518
+ add_sub_blocks sub_blocks
519
+ self
520
+ end
521
+
522
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
523
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
524
+ # @param [String] color
525
+ # @return [NotionRubyMapping::Block]
526
+ def toggle_heading_2(text_info, sub_blocks:, color: "default")
527
+ heading_2 text_info, color: color
528
+ add_sub_blocks sub_blocks
529
+ self
530
+ end
531
+
532
+ # @param [RichTextArray, String, Array<String>, RichTextObject, Array<RichTextObject>] text_info
533
+ # @param [NotionRubyMapping::Block, Array<NotionRubyMapping::Block>, nil] sub_blocks
534
+ # @param [String] color
535
+ # @return [NotionRubyMapping::Block]
536
+ def toggle_heading_3(text_info, sub_blocks:, color: "default")
537
+ heading_3 text_info, color: color
538
+ add_sub_blocks sub_blocks
539
+ self
540
+ end
541
+
542
+ # @param [String] url
543
+ # @see https://www.notion.so/hkob/Block-689ad4cbff50404d8a1baf67b6d6d78d#7aad77515ce14e3bbc7e0a7a5427820b
544
+ def video(url)
545
+ @type = __method__.to_s
546
+ @file_object = FileObject.file_object url
547
+ self
548
+ end
549
+
550
+ protected
551
+
552
+ def add_sub_blocks(sub_blocks)
553
+ @sub_blocks = Array(sub_blocks) if sub_blocks
554
+ @can_have_children = true
555
+ end
556
+
557
+ # @param [String] type
558
+ # @param [String] color
559
+ def rich_text_array_and_color(type, text_info, color = "default")
560
+ @rich_text_array = RichTextArray.rich_text_array type, text_info
561
+ @color = color
562
+ self
563
+ end
564
+ end
565
+ end
@@ -7,6 +7,7 @@ module NotionRubyMapping
7
7
 
8
8
  # @param [String] id
9
9
  # @return [NotionRubyMapping::Database, String]
10
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#58ba9190fd544432a9e2a5823d6c33b7
10
11
  def self.find(id, dry_run: false)
11
12
  nc = NotionCache.instance
12
13
  if dry_run
@@ -16,9 +17,10 @@ module NotionRubyMapping
16
17
  end
17
18
  end
18
19
 
19
- # @return [NotionRubyMapping::Property]
20
20
  # @param [Class] klass
21
21
  # @param [String] title
22
+ # @return [NotionRubyMapping::Property]
23
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#c9d24269123c444295af88b9b27074a9
22
24
  def add_property(klass, title)
23
25
  prop = assign_property klass, title
24
26
  yield prop if block_given?
@@ -29,27 +31,47 @@ module NotionRubyMapping
29
31
 
30
32
  # @param [Array<Property, Class, String>] assign
31
33
  # @return [NotionRubyMapping::Base]
32
- def create_child_page(*assign)
33
- assign = properties.map { |p| [p.class, p.name ] }.flatten if assign.empty?
34
- Page.new assign: assign, parent: {"database_id" => @id}
34
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
35
+ def build_child_page(*assign)
36
+ assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
37
+ page = Page.new assign: assign, parent: {"database_id" => @id}
38
+ pp = page.properties
39
+ pp.clear_will_update
40
+ yield page, pp if block_given?
41
+ page
42
+ end
43
+
44
+ # @param [Array<Property, Class, String>] assign
45
+ # @return [NotionRubyMapping::Base]
46
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#c217ce78020a4de79b720790fce3092d
47
+ def create_child_page(*assign, dry_run: false)
48
+ assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
49
+ page = Page.new assign: assign, parent: {"database_id" => @id}
50
+ pp = page.properties
51
+ pp.clear_will_update
52
+ yield page, pp if block_given?
53
+ page.save dry_run: dry_run
35
54
  end
36
55
 
37
56
  # @return [NotionRubyMapping::RichTextArray]
57
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#217a7d988c404d68b270c4874a9117b4
38
58
  def database_title
39
59
  @database_title ||= RichTextArray.new "title", json: (self["title"]), will_update: new_record?
40
60
  end
41
61
 
62
+ # @return [Hash] created json for property schemas (for create database)
42
63
  def property_schema_json
43
64
  @payload.property_schema_json @property_cache, database_title
44
65
  end
45
66
 
46
- # @return [Hash] created json for update database
67
+ # @return [Hash] created json for property values
47
68
  def property_values_json
48
69
  @payload.property_values_json @property_cache, database_title
49
70
  end
50
71
 
51
72
  # @param [NotionRubyMapping::Query] query object
52
73
  # @return [NotionRubyMapping::List, String]
74
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#6bd9acf62c454f64bc555c8828057e6b
53
75
  def query_database(query = Query.new, dry_run: false)
54
76
  if dry_run
55
77
  Base.dry_run_script :post, @nc.query_database_path(@id), query.query_json
@@ -59,13 +81,22 @@ module NotionRubyMapping
59
81
  end
60
82
  end
61
83
 
62
- # @param [Array] names
84
+ # @param [Array] property_names
63
85
  # @return [Array<NotionRubyMapping::Property>]
64
- def remove_properties(*names)
65
- names.map { |n| properties[n] }.each(&:remove)
86
+ # @see https://www.notion.so/hkob/Database-1462b24502424539a4231bedc07dc2f5#5d15354be2604141adfbf78d14d49942
87
+ def remove_properties(*property_names)
88
+ property_names.map { |n| properties[n] }.each(&:remove)
89
+ end
90
+
91
+ # @param [String] old_property_name
92
+ # @param [String] new_property_name
93
+ # @return [NotionRubyMapping::Database]
94
+ def rename_property(old_property_name, new_property_name)
95
+ properties[old_property_name].new_name = new_property_name
96
+ self
66
97
  end
67
98
 
68
- # @return [Hash] created json for update database
99
+ # @return [Hash] created json for property schemas (for update database)
69
100
  def update_property_schema_json
70
101
  @payload.update_property_schema_json @property_cache, database_title
71
102
  end