Class: NotionRubyMapping::Database
- Inherits:
-
Base
- Object
- Base
- NotionRubyMapping::Database
show all
- Defined in:
- lib/notion_ruby_mapping/blocks/database.rb
Overview
Instance Attribute Summary
Attributes inherited from Base
#archived, #has_children, #id, #json
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Obtained Page value or PropertyCache.
-
#add_property(klass, title) {|prop| ... } ⇒ NotionRubyMapping::Property
-
#build_child_data_source(title, *assigns) {|ds, ds.properties| ... } ⇒ NotionRubyMapping::DataSource
-
#build_child_page(*assign) ⇒ NotionRubyMapping::Base
-
#create_child_data_source(title, *assigns, dry_run: false) {|ds, ds.properties| ... } ⇒ NotionRubyMapping::DataSource, String
-
#create_child_page(*assign, dry_run: false) {|page, pp| ... } ⇒ NotionRubyMapping::Base
-
#data_sources ⇒ Object
-
#database_title ⇒ NotionRubyMapping::RichTextArray
-
#description ⇒ NotionRubyMapping::RichTextArray
-
#description=(text_info) ⇒ Object
-
#is_inline ⇒ Boolean
-
#is_inline=(flag) ⇒ Object
-
#property_schema_json ⇒ Hash
Created json for property schemas (for create database).
-
#property_values_json ⇒ Hash
Created json for property values.
-
#remove_properties(*property_names) ⇒ Array<NotionRubyMapping::Property>
-
#rename_property(old_property_name, new_property_name) ⇒ NotionRubyMapping::Database
-
#update_property_schema_json ⇒ Hash
Created json for property schemas (for update database).
Methods inherited from Base
#append_block_children, #assert_parent_children_pair, #assign_property, #block?, block_id, #children, #comments, #cover, create_from_json, #created_time, #data_source?, data_source_id, #database?, database_id, dry_run_script, #get, #icon, #initialize, #inspect, #json_properties, #last_edited_time, #new_record?, #page?, page_id, #parent, #parent_id, #properties, #reload, #restore_from_json, #save, #set_cover, #set_icon, #synced_block_original?, #update_json
Class Method Details
12
13
14
15
16
17
18
19
20
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 12
def self.find(id, dry_run: false)
nc = NotionCache.instance
database_id = Base.database_id id
if dry_run
dry_run_script :get, nc.database_path(database_id)
else
nc.database database_id
end
end
|
Instance Method Details
Returns obtained Page value or PropertyCache.
24
25
26
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 24
def [](key)
get key
end
|
32
33
34
35
36
37
38
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 32
def add_property(klass, title)
prop = assign_property klass, title
yield prop if block_given?
@payload.merge_property prop.property_schema_json
prop.clear_will_update
prop
end
|
#build_child_data_source(title, *assigns) {|ds, ds.properties| ... } ⇒ NotionRubyMapping::DataSource
44
45
46
47
48
49
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 44
def build_child_data_source(title, *assigns)
ds = DataSource.new json: {"title" => [TextObject.new(title).property_values_json]},
assign: assigns, parent: {"type" => "database_id", "database_id" => @id}
yield ds, ds.properties if block_given?
ds
end
|
66
67
68
69
70
71
72
73
74
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 66
def build_child_page(*assign)
unless data_sources.length == 1
raise StandardError, "Database #{id} is linked with multiple data_sources, use data_source.build_child_page"
end
data_sources.first.build_child_page(*assign) do |page, pp|
yield page, pp if block_given?
end
end
|
#create_child_data_source(title, *assigns, dry_run: false) {|ds, ds.properties| ... } ⇒ NotionRubyMapping::DataSource, String
56
57
58
59
60
61
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 56
def create_child_data_source(title, *assigns, dry_run: false)
ds = DataSource.new json: {"title" => [TextObject.new(title).property_values_json]},
assign: assigns, parent: {"type" => "database_id", "database_id" => @id}
yield ds, ds.properties if block_given?
ds.save dry_run: dry_run
end
|
#create_child_page(*assign, dry_run: false) {|page, pp| ... } ⇒ NotionRubyMapping::Base
80
81
82
83
84
85
86
87
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 80
def create_child_page(*assign, dry_run: false)
assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
page = Page.new assign: assign, parent: {"database_id" => @id}
pp = page.properties
pp.clear_will_update
yield page, pp if block_given?
page.save dry_run: dry_run
end
|
#data_sources ⇒ Object
95
96
97
98
99
100
101
102
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 95
def data_sources
return @data_sources if @data_sources
reload if @json.nil? || !@json.key?("data_sources")
@data_sources = @json["data_sources"].map do |json|
DataSource.find json["id"]
end
end
|
91
92
93
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 91
def database_title
@database_title ||= RichTextArray.new "title", json: (self["title"]), will_update: new_record?
end
|
105
106
107
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 105
def description
RichTextArray.new "description", json: self["description"]
end
|
#description=(text_info) ⇒ Object
110
111
112
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 110
def description=(text_info)
@payload.description = text_info
end
|
#is_inline ⇒ Boolean
115
116
117
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 115
def is_inline
self["is_inline"]
end
|
#is_inline=(flag) ⇒ Object
120
121
122
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 120
def is_inline=(flag)
@payload.is_inline = flag
end
|
#property_schema_json ⇒ Hash
Returns created json for property schemas (for create database).
125
126
127
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 125
def property_schema_json
@payload.property_schema_json @property_cache, database_title
end
|
#property_values_json ⇒ Hash
Returns created json for property values.
130
131
132
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 130
def property_values_json
@payload.property_values_json @property_cache, database_title
end
|
137
138
139
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 137
def remove_properties(*property_names)
property_names.map { |n| properties[n] }.each(&:remove)
end
|
#rename_property(old_property_name, new_property_name) ⇒ NotionRubyMapping::Database
144
145
146
147
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 144
def rename_property(old_property_name, new_property_name)
properties[old_property_name].new_name = new_property_name
self
end
|
#update_property_schema_json ⇒ Hash
Returns created json for property schemas (for update database).
150
151
152
|
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 150
def update_property_schema_json
@payload.update_property_schema_json @property_cache, database_title
end
|