Class: NotionRubyMapping::Database

Inherits:
Base
  • Object
show all
Defined in:
lib/notion_ruby_mapping/blocks/database.rb

Overview

Notion database

Instance Attribute Summary

Attributes inherited from Base

#archived, #id, #json

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#append_block_children, #assert_parent_children_pair, #assign_property, #block?, #children, #comments, create_from_json, #created_time, #database?, dry_run_script, #get, #icon, #initialize, #inspect, #json_properties, #last_edited_time, #new_record?, #page?, #parent, #properties, #reload, #restore_from_json, #save, #set_icon, #synced_block_original?, #update_json

Constructor Details

This class inherits a constructor from NotionRubyMapping::Base

Class Method Details

.find(id, dry_run: false) ⇒ NotionRubyMapping::Database, String



11
12
13
14
15
16
17
18
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 11

def self.find(id, dry_run: false)
  nc = NotionCache.instance
  if dry_run
    dry_run_script :get, nc.database_path(id)
  else
    nc.database id
  end
end

Instance Method Details

#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash

Returns obtained Page value or PropertyCache.

Parameters:

  • key (String)

Returns:



22
23
24
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 22

def [](key)
  get key
end

#add_property(klass, title) {|prop| ... } ⇒ NotionRubyMapping::Property



30
31
32
33
34
35
36
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 30

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_page(*assign) {|page, pp| ... } ⇒ NotionRubyMapping::Base



41
42
43
44
45
46
47
48
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 41

def build_child_page(*assign)
  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
end

#create_child_page(*assign, dry_run: false) {|page, pp| ... } ⇒ NotionRubyMapping::Base



53
54
55
56
57
58
59
60
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 53

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

#database_titleNotionRubyMapping::RichTextArray



64
65
66
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 64

def database_title
  @database_title ||= RichTextArray.new "title", json: (self["title"]), will_update: new_record?
end

#descriptionNotionRubyMapping::RichTextArray



69
70
71
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 69

def description
  RichTextArray.new "description", json: self["description"]
end

#description=(text_info) ⇒ Object

Parameters:



74
75
76
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 74

def description=(text_info)
  @payload.description = text_info
end

#is_inlineBoolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 79

def is_inline
  self["is_inline"]
end

#is_inline=(flag) ⇒ Object

Parameters:

  • flag (Boolean)


84
85
86
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 84

def is_inline=(flag)
  @payload.is_inline = flag
end

#property_schema_jsonHash

Returns created json for property schemas (for create database).

Returns:

  • (Hash)

    created json for property schemas (for create database)



89
90
91
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 89

def property_schema_json
  @payload.property_schema_json @property_cache, database_title
end

#property_values_jsonHash

Returns created json for property values.

Returns:

  • (Hash)

    created json for property values



94
95
96
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 94

def property_values_json
  @payload.property_values_json @property_cache, database_title
end

#query_database(query = Query.new, dry_run: false) ⇒ NotionRubyMapping::List, String



101
102
103
104
105
106
107
108
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 101

def query_database(query = Query.new, dry_run: false)
  if dry_run
    Base.dry_run_script :post, @nc.query_database_path(@id), query.query_json
  else
    response = @nc.database_query_request @id, query
    List.new json: response, database: self, query: query
  end
end

#remove_properties(*property_names) ⇒ Array<NotionRubyMapping::Property>



113
114
115
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 113

def remove_properties(*property_names)
  property_names.map { |n| properties[n] }.each(&:remove)
end

#rename_property(old_property_name, new_property_name) ⇒ NotionRubyMapping::Database

Parameters:

  • old_property_name (String)
  • new_property_name (String)

Returns:



120
121
122
123
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 120

def rename_property(old_property_name, new_property_name)
  properties[old_property_name].new_name = new_property_name
  self
end

#update_property_schema_jsonHash

Returns created json for property schemas (for update database).

Returns:

  • (Hash)

    created json for property schemas (for update database)



126
127
128
# File 'lib/notion_ruby_mapping/blocks/database.rb', line 126

def update_property_schema_json
  @payload.update_property_schema_json @property_cache, database_title
end