Class: NotionRubyMapping::DataSource

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

Overview

Notion data_source

Instance Attribute Summary

Attributes inherited from Base

#has_children, #id, #in_trash, #json

Class Method Summary collapse

Instance Method Summary collapse

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, #markdown, #new_record?, #page?, page_id, #parent, #parent_id, #properties, #reload, #restore_from_json, #save, #set_cover, #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::DataSource, String

Parameters:

  • id (String)
  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:

See Also:



12
13
14
15
16
17
18
19
20
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 12

def self.find(id, dry_run: false)
  nc = NotionCache.instance
  data_source_id = Base.data_source_id id
  if dry_run
    dry_run_script :get, nc.data_source_path(data_source_id)
  else
    nc.data_source data_source_id
  end
end

Instance Method Details

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

Returns obtained Page value or PropertyCache.

Parameters:

  • key (String)

Returns:



24
25
26
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 24

def [](key)
  get key
end

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



32
33
34
35
36
37
38
# File 'lib/notion_ruby_mapping/blocks/data_source.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_page(*assign, template_page: nil, markdown: nil) {|page, pp| ... } ⇒ NotionRubyMapping::Base

Parameters:

  • assign (Array<Property, Class, String>)
  • template_page (String, NilClass) (defaults to: nil)
  • markdown (String, NilClass) (defaults to: nil)

Yields:

  • (page, pp)

Returns:

See Also:



45
46
47
48
49
50
51
52
53
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 45

def build_child_page(*assign, template_page: nil, markdown: nil)
  assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
  page = Page.new assign: assign, parent: {"data_source_id" => @id}, template_page: template_page,
                  markdown: markdown
  pp = page.properties
  pp.clear_will_update
  yield page, pp if block_given?
  page
end

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

Parameters:

  • assign (Array<Property, Class, String>)
  • template_page (String, NilClass) (defaults to: nil)
  • markdown (String, NilClass) (defaults to: nil)
  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Yields:

  • (page, pp)

Returns:

See Also:



61
62
63
64
65
66
67
68
69
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 61

def create_child_page(*assign, template_page: nil, markdown: nil, dry_run: false)
  assign = properties.map { |p| [p.class, p.name] }.flatten if assign.empty?
  page = Page.new assign: assign, parent: {"data_source_id" => @id}, template_page: template_page,
                  markdown: markdown
  pp = page.properties
  pp.clear_will_update
  yield page, pp if block_given?
  page.save dry_run: dry_run
end

#data_source_titleNotionRubyMapping::RichTextArray



73
74
75
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 73

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

#descriptionNotionRubyMapping::RichTextArray



78
79
80
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 78

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

#description=(text_info) ⇒ Object

Parameters:



83
84
85
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 83

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

#is_inlineBoolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 88

def is_inline
  self["is_inline"]
end

#is_inline=(flag) ⇒ Object

Parameters:

  • flag (Boolean)


93
94
95
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 93

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

#property_schema_jsonHash

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

Returns:

  • (Hash)

    created json for property schemas (for create data_source)



98
99
100
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 98

def property_schema_json
  @payload.property_schema_json @property_cache, data_source_title
end

#property_values_jsonHash

Returns created json for property values.

Returns:

  • (Hash)

    created json for property values



103
104
105
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 103

def property_values_json
  @payload.property_values_json @property_cache, data_source_title
end

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

Parameters:

  • query (NotionRubyMapping::Query) (defaults to: Query.new)

    object

  • dry_run (Boolean) (defaults to: false)

    true if dry_run

Returns:

See Also:



111
112
113
114
115
116
117
118
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 111

def query_data_source(query = Query.new, dry_run: false)
  if dry_run
    Base.dry_run_script :post, @nc.query_data_source_path(@id), query.query_json
  else
    response = @nc.data_source_query_request @id, query
    List.new json: response, type: "data_source", value: self, query: query
  end
end

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



123
124
125
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 123

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

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

Parameters:

  • old_property_name (String)
  • new_property_name (String)

Returns:



130
131
132
133
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 130

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

#templates(page_size: nil) ⇒ NotionRubyMapping::List

Returns List of Template object.

Parameters:

  • page_size (Integer) (defaults to: nil)

Returns:



137
138
139
140
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 137

def templates(page_size: nil)
  json = @nc.list_data_source_templates_request(id, page_size: page_size)
  List.new type: "template", value: id, json: json, query: Query.new
end

#update_property_schema_jsonHash

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

Returns:

  • (Hash)

    created json for property schemas (for update data_source)



143
144
145
# File 'lib/notion_ruby_mapping/blocks/data_source.rb', line 143

def update_property_schema_json
  @payload.update_property_schema_json @property_cache, data_source_title
end