Class: NotionRubyMapping::Base
- Inherits:
-
Object
- Object
- NotionRubyMapping::Base
- Defined in:
- lib/notion_ruby_mapping/base.rb
Overview
Notion Base object (Parent of Page / Database / List)
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Obtained Page value or PropertyCache.
- #add_property_for_update(property) ⇒ NotionRubyMapping::Base
-
#assign_property(klass, title) ⇒ Property
Generated property.
- #children ⇒ NotionRubyMapping::List
- #clear_object ⇒ NotionRubyMapping::Base
-
#create_json ⇒ Hash
Created json.
-
#icon ⇒ Hash?
Obtained Hash.
-
#initialize(json: nil, id: nil) ⇒ Base
constructor
A new instance of Base.
-
#json_properties ⇒ Hash
Json properties.
-
#payload ⇒ NotionRubyMapping::Payload
Get or created Payload object.
-
#properties ⇒ NotionRubyMapping::PropertyCache
Get or created PropertyCache object.
-
#reload ⇒ NotionRubyMapping::Base
Reloaded self.
- #set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
- #update_json(json) ⇒ NotionRubyMapping::Base
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
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/notion_ruby_mapping/base.rb', line 13 def id @id end |
#json ⇒ Object (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
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.
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
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.
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 |
#children ⇒ NotionRubyMapping::List
62 63 64 |
# File 'lib/notion_ruby_mapping/base.rb', line 62 def children @children ||= @nc.block_children(id) end |
#clear_object ⇒ NotionRubyMapping::Base
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_json ⇒ Hash
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 |
#icon ⇒ Hash?
Returns obtained Hash.
112 113 114 |
# File 'lib/notion_ruby_mapping/base.rb', line 112 def icon self["icon"] end |
#json_properties ⇒ Hash
Returns json properties.
57 58 59 |
# File 'lib/notion_ruby_mapping/base.rb', line 57 def json_properties @json && @json["properties"] end |
#payload ⇒ NotionRubyMapping::Payload
Returns get or created Payload object.
33 34 35 |
# File 'lib/notion_ruby_mapping/base.rb', line 33 def payload @payload ||= Payload.new end |
#properties ⇒ NotionRubyMapping::PropertyCache
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 |
#reload ⇒ NotionRubyMapping::Base
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
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
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 |