notion_ruby_mapping 0.3.3 → 0.4.0

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