Class: NotionRubyMapping::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/notion_ruby_mapping/base.rb

Overview

Notion Base object (Parent of Page / Database / List)

Direct Known Subclasses

Block, Database, List, Page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json: nil, id: nil) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
# File 'lib/notion_ruby_mapping/base.rb', line 6

def initialize(json: nil, id: nil)
  @nc = NotionCache.instance
  @json = json
  @id = @nc.hex_id(id || @json["id"])
  @payload = nil
  @property_cache = nil
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/notion_ruby_mapping/base.rb', line 13

def id
  @id
end

#jsonObject (readonly)

Returns the value of attribute json.



13
14
15
# File 'lib/notion_ruby_mapping/base.rb', line 13

def json
  @json
end

Class Method Details

.create_from_json(json) ⇒ NotionRubyMapping::Base

Parameters:

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/notion_ruby_mapping/base.rb', line 17

def self.create_from_json(json)
  case json["object"]
  when "page"
    Page.new json: json
  when "database"
    Database.new json: json
  when "list"
    List.new json: json
  when "block"
    Block.new json: json
  else
    throw Notion::Api::Errors::ObjectNotFound
  end
end

Instance Method Details

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

Returns obtained Page value or PropertyCache.

Parameters:

Returns:

  • obtained Page value or PropertyCache



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/notion_ruby_mapping/base.rb', line 97

def [](key)
  unless @json
    return nil if @id.nil?

    update_json reload
  end
  case key
  when "properties"
    properties
  else
    @json[key]
  end
end

#add_property_for_update(property) ⇒ NotionRubyMapping::Base

Parameters:

  • Property object for udpate or create

Returns:



118
119
120
121
122
# File 'lib/notion_ruby_mapping/base.rb', line 118

def add_property_for_update(property)
  @property_cache ||= PropertyCache.new {}
  @property_cache.add_property property, will_update: true
  self
end

#assign_property(klass, title) ⇒ Property

Returns generated property.

Parameters:

Returns:

  • generated property



127
128
129
130
131
132
133
134
# File 'lib/notion_ruby_mapping/base.rb', line 127

def assign_property(klass, title)
  @property_cache ||= PropertyCache.new {}

  property = klass.new(title)
  property.will_update = true
  @property_cache.add_property property
  property
end

#childrenNotionRubyMapping::List

Returns:



62
63
64
# File 'lib/notion_ruby_mapping/base.rb', line 62

def children
  @children ||= @nc.block_children(id)
end

#clear_objectNotionRubyMapping::Base

Returns:



78
79
80
81
82
# File 'lib/notion_ruby_mapping/base.rb', line 78

def clear_object
  @payload = nil
  @property_cache = nil
  self
end

#create_jsonHash

Returns created json.

Returns:

  • created json



137
138
139
# File 'lib/notion_ruby_mapping/base.rb', line 137

def create_json
  payload.create_json @property_cache&.create_json
end

#iconHash?

Returns obtained Hash.

Returns:

  • obtained Hash



112
113
114
# File 'lib/notion_ruby_mapping/base.rb', line 112

def icon
  self["icon"]
end

#json_propertiesHash

Returns json properties.

Returns:

  • json properties



57
58
59
# File 'lib/notion_ruby_mapping/base.rb', line 57

def json_properties
  @json && @json["properties"]
end

#payloadNotionRubyMapping::Payload

Returns get or created Payload object.

Returns:

  • get or created Payload object



33
34
35
# File 'lib/notion_ruby_mapping/base.rb', line 33

def payload
  @payload ||= Payload.new
end

#propertiesNotionRubyMapping::PropertyCache

Returns get or created PropertyCache object.

Returns:

  • get or created PropertyCache object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/notion_ruby_mapping/base.rb', line 38

def properties
  unless @property_cache
    unless @json
      return nil if @id.nil?

      reload
    end
    @property_cache = PropertyCache.new json_properties
  end
  @property_cache
end

#reloadNotionRubyMapping::Base

Returns reloaded self.

Returns:

  • reloaded self



51
52
53
54
# File 'lib/notion_ruby_mapping/base.rb', line 51

def reload
  update_json reload_json
  self
end

#set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base

Parameters:

  • (defaults to: nil)
  • (defaults to: nil)

Returns:



87
88
89
90
91
92
93
# File 'lib/notion_ruby_mapping/base.rb', line 87

def set_icon(emoji: nil, url: nil)
  if self.is_a?(Page) || self.is_a?(Database)
    payload.set_icon(emoji: emoji, url: url)
    update
  end
  self
end

#update_json(json) ⇒ NotionRubyMapping::Base

Parameters:

Returns:



68
69
70
71
72
73
74
75
# File 'lib/notion_ruby_mapping/base.rb', line 68

def update_json(json)
  if @json.nil? || @json["type"] == json["type"]
    @json = json
    @id = @nc.hex_id(@json["id"])
    clear_object
  end
  self
end