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.
-
#assign_property(klass, title) ⇒ Property
Generated property.
- #children ⇒ NotionRubyMapping::List
-
#icon ⇒ Hash?
Obtained Hash.
-
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
constructor
A new instance of Base.
-
#json_properties ⇒ Hash
Json properties.
- #new_record? ⇒ Boolean
-
#properties ⇒ NotionRubyMapping::PropertyCache
Get or created PropertyCache object.
-
#property_values_json ⇒ Hash
Created json.
-
#reload ⇒ NotionRubyMapping::Base
Reloaded self.
- #restore_from_json ⇒ Object
- #save ⇒ NotionRubyMapping::Base
- #set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
-
#title ⇒ String
Title.
- #update_json(json) ⇒ NotionRubyMapping::Base
Constructor Details
#initialize(json: nil, id: nil, assign: [], parent: nil) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/notion_ruby_mapping/base.rb', line 9 def initialize(json: nil, id: nil, assign: [], parent: nil) @nc = NotionCache.instance @json = json @id = @nc.hex_id(id || json && @json["id"]) @new_record = true unless parent.nil? raise StandardError, "Unknown id" if !is_a?(List) && @id.nil? && parent.nil? @payload = Payload.new(parent && {"parent" => parent}) @property_cache = nil assign.each_slice(2) { |(klass, key)| assign_property(klass, key) } end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
20 21 22 |
# File 'lib/notion_ruby_mapping/base.rb', line 20 def id @id end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
20 21 22 |
# File 'lib/notion_ruby_mapping/base.rb', line 20 def json @json end |
Class Method Details
.create_from_json(json) ⇒ NotionRubyMapping::Base
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/notion_ruby_mapping/base.rb', line 24 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 raise StandardError, json.inspect end end |
Instance Method Details
#[](key) ⇒ NotionRubyMapping::PropertyCache, Hash
Returns obtained Page value or PropertyCache.
117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/notion_ruby_mapping/base.rb', line 117 def [](key) unless @json raise StandardError, "Unknown id" if @id.nil? reload end case key when "properties" properties else @json[key] end end |
#assign_property(klass, title) ⇒ Property
Returns generated property.
139 140 141 142 143 144 145 |
# File 'lib/notion_ruby_mapping/base.rb', line 139 def assign_property(klass, title) @property_cache ||= PropertyCache.new {} property = klass.new(title, will_update: true) @property_cache.add_property property property end |
#children ⇒ NotionRubyMapping::List
78 79 80 |
# File 'lib/notion_ruby_mapping/base.rb', line 78 def children @children ||= @nc.block_children(id) end |
#icon ⇒ Hash?
Returns obtained Hash.
132 133 134 |
# File 'lib/notion_ruby_mapping/base.rb', line 132 def icon self["icon"] end |
#json_properties ⇒ Hash
Returns json properties.
73 74 75 |
# File 'lib/notion_ruby_mapping/base.rb', line 73 def json_properties @json && @json["properties"] end |
#new_record? ⇒ Boolean
39 40 41 |
# File 'lib/notion_ruby_mapping/base.rb', line 39 def new_record? @new_record end |
#properties ⇒ NotionRubyMapping::PropertyCache
Returns get or created PropertyCache object.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/notion_ruby_mapping/base.rb', line 44 def properties unless @property_cache unless @json raise StandardError, "Unknown id" if @id.nil? reload end @property_cache = PropertyCache.new json_properties end @property_cache end |
#property_values_json ⇒ Hash
Returns created json.
148 149 150 |
# File 'lib/notion_ruby_mapping/base.rb', line 148 def property_values_json @payload.property_values_json @property_cache&.property_values_json end |
#reload ⇒ NotionRubyMapping::Base
Returns reloaded self.
62 63 64 65 |
# File 'lib/notion_ruby_mapping/base.rb', line 62 def reload update_json reload_json self end |
#restore_from_json ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/notion_ruby_mapping/base.rb', line 93 def restore_from_json @payload.clear return if (ps = @json["properties"]).nil? properties.json = json_properties return unless is_a? Page ps.each do |key, json| properties[key].update_from_json json end end |
#save ⇒ NotionRubyMapping::Base
68 69 70 |
# File 'lib/notion_ruby_mapping/base.rb', line 68 def save @new_record ? create : update end |
#set_icon(emoji: nil, url: nil) ⇒ NotionRubyMapping::Base
108 109 110 111 112 113 |
# File 'lib/notion_ruby_mapping/base.rb', line 108 def set_icon(emoji: nil, url: nil) if is_a?(Page) || is_a?(Database) @payload.set_icon(emoji: emoji, url: url) end self end |
#title ⇒ String
Returns title.
57 58 59 |
# File 'lib/notion_ruby_mapping/base.rb', line 57 def title properties.select { |p| p.is_a? TitleProperty }.map(&:full_text).join "" end |
#update_json(json) ⇒ NotionRubyMapping::Base
84 85 86 87 88 89 90 91 |
# File 'lib/notion_ruby_mapping/base.rb', line 84 def update_json(json) raise StandardError, json.inspect unless json["object"] != "error" && @json.nil? || @json["type"] == json["type"] @json = json @id = @nc.hex_id(@json["id"]) restore_from_json self end |