Class: NotionRubyMapping::Property
- Inherits:
-
Object
- Object
- NotionRubyMapping::Property
- Extended by:
- Forwardable
- Defined in:
- lib/notion_ruby_mapping/properties/property.rb
Overview
abstract class for property
Direct Known Subclasses
CheckboxProperty, DateBaseProperty, EmailProperty, FilesProperty, MultiProperty, NumberProperty, PhoneNumberProperty, SelectProperty, TextProperty, UrlProperty
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Common methods.
-
#property_cache ⇒ Object
Returns the value of attribute property_cache.
-
#property_id ⇒ Object
readonly
Common methods.
-
#will_update ⇒ Object
readonly
Common methods.
Class Method Summary collapse
-
.create_from_json(name, input_json, base_type = :page, property_cache = nil, query = nil) ⇒ NotionRubyMapping::Property?
Generated Property object.
Instance Method Summary collapse
- #assert_database_property(method) ⇒ Object
- #assert_page_property(method) ⇒ Object
- #clear_will_update ⇒ FalseClass
-
#contents? ⇒ TrueClass, FalseClass
True if it has Property contents.
-
#database? ⇒ TrueClass, FalseClass
True if database property.
-
#initialize(name, will_update: false, base_type: :page, property_id: nil, property_cache: nil, query: nil) ⇒ Property
constructor
Generated Property object.
-
#make_filter_query(key, value, rollup = nil, rollup_type = nil) ⇒ NotionRubyMapping::Query
Generated Query object.
- #new_name=(new_name) ⇒ Object
-
#page? ⇒ TrueClass, FalseClass
True if page property.
- #property_schema_json ⇒ Hash
-
#remove ⇒ NotionRubyMapping::Property
Self.
- #retrieve_page_property ⇒ NotionRubyMapping::Property, ...
-
#type ⇒ Symbol
Property type.
- #update_from_json(json) ⇒ Object
- #update_property_schema_json ⇒ Hash
Constructor Details
#initialize(name, will_update: false, base_type: :page, property_id: nil, property_cache: nil, query: nil) ⇒ Property
Returns generated Property object.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 52 def initialize(name, will_update: false, base_type: :page, property_id: nil, property_cache: nil, query: nil) @name = name @will_update = will_update @base_type = base_type @create = false @remove = false @new_name = nil @json = nil @property_id = property_id @property_cache = property_cache @query = query end |
Instance Attribute Details
#name ⇒ Object (readonly)
Common methods
14 15 16 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 14 def name @name end |
#property_cache ⇒ Object
Returns the value of attribute property_cache.
15 16 17 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 15 def property_cache @property_cache end |
#property_id ⇒ Object (readonly)
Common methods
14 15 16 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 14 def property_id @property_id end |
#will_update ⇒ Object (readonly)
Common methods
14 15 16 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 14 def will_update @will_update end |
Class Method Details
.create_from_json(name, input_json, base_type = :page, property_cache = nil, query = nil) ⇒ NotionRubyMapping::Property?
Returns generated Property object.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 70 def self.create_from_json(name, input_json, base_type = :page, property_cache = nil, query = nil) raise StandardError, "Property not found: #{name}:#{input_json}" if input_json.nil? type = input_json["type"] if type.nil? new name, property_id: input_json["id"], base_type: base_type, property_cache: property_cache, query: query elsif type == "property_item" tmp = new name, property_id: input_json["property_item"]["id"], base_type: base_type, property_cache: property_cache, query: query objects = List.new(json: input_json, property: tmp, query: query).select { true } case input_json["property_item"]["type"] when "people" PeopleProperty.new name, people: objects, base_type: base_type, property_cache: property_cache, query: query when "relation" RelationProperty.new name, relation: objects, base_type: base_type, property_cache: property_cache, query: query when "rich_text" RichTextProperty.new name, text_objects: objects, base_type: base_type, property_cache: property_cache, query: query when "rollup" RollupProperty.new name, json: objects, base_type: base_type, property_cache: property_cache, query: query when "title" TitleProperty.new name, text_objects: objects, base_type: base_type, property_cache: property_cache, query: query end else klass = { "checkbox" => CheckboxProperty, "created_time" => CreatedTimeProperty, "date" => DateProperty, "formula" => FormulaProperty, "last_edited_time" => LastEditedTimeProperty, "rollup" => RollupProperty, "email" => EmailProperty, "files" => FilesProperty, "created_by" => CreatedByProperty, "last_edited_by" => LastEditedByProperty, "multi_select" => MultiSelectProperty, "relation" => RelationProperty, "number" => NumberProperty, "people" => PeopleProperty, "phone_number" => PhoneNumberProperty, "select" => SelectProperty, "title" => TitleProperty, "rich_text" => RichTextProperty, "url" => UrlProperty, }[type] raise StandardError, "Irregular property type: #{type}" unless klass klass.new name, json: input_json[type], base_type: base_type, property_cache: property_cache end end |
Instance Method Details
#assert_database_property(method) ⇒ Object
171 172 173 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 171 def assert_database_property(method) raise StandardError, "#{method} can execute only Database property." unless database? end |
#assert_page_property(method) ⇒ Object
196 197 198 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 196 def assert_page_property(method) raise StandardError, "#{method} can execute only Page property." unless @base_type == :page end |
#clear_will_update ⇒ FalseClass
121 122 123 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 121 def clear_will_update @will_update = false end |
#contents? ⇒ TrueClass, FalseClass
Returns true if it has Property contents.
131 132 133 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 131 def contents? !instance_of? Property end |
#database? ⇒ TrueClass, FalseClass
Returns true if database property.
126 127 128 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 126 def database? @base_type == :database end |
#make_filter_query(key, value, rollup = nil, rollup_type = nil) ⇒ NotionRubyMapping::Query
Returns generated Query object.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 138 def make_filter_query(key, value, rollup = nil, rollup_type = nil) if rollup Query.new filter: {"property" => @name, rollup => {rollup_type => {key => value}}} elsif rollup_type Query.new filter: {"property" => @name, rollup_type => {key => value}} elsif @name == "__timestamp__" Query.new filter: {"timestamp" => type, type => {key => value}} else Query.new filter: {"property" => @name, type => {key => value}} end end |
#new_name=(new_name) ⇒ Object
32 33 34 35 36 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 32 def new_name=(new_name) assert_database_property __method__ @will_update = true @new_name = new_name end |
#page? ⇒ TrueClass, FalseClass
Returns true if page property.
151 152 153 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 151 def page? @base_type == :page end |
#property_schema_json ⇒ Hash
176 177 178 179 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 176 def property_schema_json assert_database_property __method__ {@name => {type => property_schema_json_sub}} end |
#remove ⇒ NotionRubyMapping::Property
Returns self.
39 40 41 42 43 44 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 39 def remove assert_database_property __method__ @will_update = true @remove = true self end |
#retrieve_page_property ⇒ NotionRubyMapping::Property, ...
201 202 203 204 205 206 207 208 209 210 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 201 def retrieve_page_property assert_page_property __method__ raise StandardError, "property_cache.page_id is empty" if @property_cache.page_id.nil? json = NotionCache.instance.page_property_request @property_cache.page_id, @property_id, (@query&.query_json || {}) new_property = self.class.create_from_json @name, json, :page, @property_cache, @query @property_cache.add_property new_property new_property end |
#type ⇒ Symbol
Returns property type.
164 165 166 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 164 def type self.class::TYPE end |
#update_from_json(json) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 156 def update_from_json(json) @will_update = false return unless contents? @json = json[type] if json[type] && json[type] != "property_item" end |
#update_property_schema_json ⇒ Hash
182 183 184 185 186 187 188 189 190 191 |
# File 'lib/notion_ruby_mapping/properties/property.rb', line 182 def update_property_schema_json assert_database_property __method__ if @remove {@name => nil} elsif @new_name {@name => {"name" => @new_name}} else {} end end |