Class: NotionRubyMapping::Property

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/notion_ruby_mapping/properties/property.rb

Overview

abstract class for property

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, will_update: false, base_type: "page", property_id: nil, property_cache: nil, query: nil) ⇒ Property

Returns generated Property object.

Parameters:

  • name (String, Symbol)

    Property name



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

#nameObject (readonly)

Common methods



14
15
16
# File 'lib/notion_ruby_mapping/properties/property.rb', line 14

def name
  @name
end

#property_cacheObject

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_idObject (readonly)

Common methods



14
15
16
# File 'lib/notion_ruby_mapping/properties/property.rb', line 14

def property_id
  @property_id
end

#will_updateObject (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.

Parameters:

  • name (String, Symbol)
  • input_json (Hash)
  • base_type (String) (defaults to: "page")

    “page” or “database”

  • page_id (String, nil)

Returns:

Raises:

  • (StandardError)


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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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, type: "property", value: tmp, query: query).to_a
    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
    else
      raise StandardError, "Irregular property type: #{input_json["property_item"]["type"]}"
    end
  else
    klass = {
      button: ButtonProperty,
      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,
      status: StatusProperty,
      title: TitleProperty,
      rich_text: RichTextProperty,
      unique_id: UniqueIdProperty,
      url: UrlProperty,
      verification: VerificationProperty,
    }[type.to_sym]
    raise StandardError, "Irregular property type: #{type}" unless klass

    answer = klass.new name, property_id: input_json["id"], json: input_json[type], base_type: base_type,
                             property_cache: property_cache
    answer = answer.retrieve_page_property if answer.is_a?(RelationProperty) && input_json["has_more"] == true
    answer
  end
end

Instance Method Details

#assert_data_source_property(method) ⇒ Object

DataSource property only methods

Raises:

  • (StandardError)


211
212
213
# File 'lib/notion_ruby_mapping/properties/property.rb', line 211

def assert_data_source_property(method)
  raise StandardError, "#{method} can execute only DataSource property." unless data_source?
end

#assert_database_or_data_source_property(method) ⇒ Object

Parameters:

  • method (Symbol, nil)

Raises:

  • (StandardError)


200
201
202
# File 'lib/notion_ruby_mapping/properties/property.rb', line 200

def assert_database_or_data_source_property(method)
  raise StandardError, "#{method} can execute only Database property." if page?
end

#assert_database_property(method) ⇒ Object

Parameters:

  • method (Symbol, nil)

Raises:

  • (StandardError)


195
196
197
# File 'lib/notion_ruby_mapping/properties/property.rb', line 195

def assert_database_property(method)
  raise StandardError, "#{method} can execute only Database property." unless database?
end

#assert_page_property(method) ⇒ Object

Parameters:

  • method (Symbol, nil)

Raises:

  • (StandardError)


230
231
232
# File 'lib/notion_ruby_mapping/properties/property.rb', line 230

def assert_page_property(method)
  raise StandardError, "#{method} can execute only Page property." if database? || data_source?
end

#clear_will_updateFalseClass

Returns:

  • (FalseClass)


135
136
137
138
139
# File 'lib/notion_ruby_mapping/properties/property.rb', line 135

def clear_will_update
  @new_name = nil
  @remove = nil
  @will_update = false
end

#contents?TrueClass, FalseClass

Returns true if it has Property contents.

Returns:

  • (TrueClass, FalseClass)

    true if it has Property contents



155
156
157
# File 'lib/notion_ruby_mapping/properties/property.rb', line 155

def contents?
  !instance_of? Property
end

#data_source?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/notion_ruby_mapping/properties/property.rb', line 146

def data_source?
  @base_type == "data_source"
end

#database?TrueClass, FalseClass

Returns true if database property.

Returns:

  • (TrueClass, FalseClass)

    true if database property



142
143
144
# File 'lib/notion_ruby_mapping/properties/property.rb', line 142

def database?
  @base_type == "database"
end

#database_or_data_source?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/notion_ruby_mapping/properties/property.rb', line 150

def database_or_data_source?
  %w[database data_source].include? @base_type
end

#make_filter_query(key, value, condition: nil, another_type: nil) ⇒ NotionRubyMapping::Query

Returns generated Query object.

Parameters:

  • key (Symbol)

    query parameter

  • value (Object)

    query value

Returns:



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/notion_ruby_mapping/properties/property.rb', line 162

def make_filter_query(key, value, condition: nil, another_type: nil)
  if condition
    Query.new filter: {"property" => @name, type => {condition => {another_type => {key => value}}}}
  elsif another_type
    Query.new filter: {"property" => @name, type => {another_type => {key => value}}}
  elsif @name == "__timestamp__"
    Query.new filter: {"timestamp" => type.to_s, type => {key => value}}
  else
    Query.new filter: {"property" => @name, type => {key => value}}
  end
end

#new_name=(new_name) ⇒ Object

Parameters:

  • new_name (String)


32
33
34
35
36
# File 'lib/notion_ruby_mapping/properties/property.rb', line 32

def new_name=(new_name)
  assert_data_source_property __method__
  @will_update = true
  @new_name = new_name
end

#page?TrueClass, FalseClass

Returns true if page property.

Returns:

  • (TrueClass, FalseClass)

    true if page property



175
176
177
# File 'lib/notion_ruby_mapping/properties/property.rb', line 175

def page?
  @base_type == "page"
end

#property_schema_jsonHash

Returns:

  • (Hash)


205
206
207
208
# File 'lib/notion_ruby_mapping/properties/property.rb', line 205

def property_schema_json
  assert_database_or_data_source_property __method__
  {@name => {type => property_schema_json_sub}}
end

#removeNotionRubyMapping::Property

Returns self.

Returns:



39
40
41
42
43
44
# File 'lib/notion_ruby_mapping/properties/property.rb', line 39

def remove
  assert_data_source_property __method__
  @will_update = true
  @remove = true
  self
end

#retrieve_page_propertyNotionRubyMapping::Property, ...

Returns:

Raises:

  • (StandardError)


235
236
237
238
239
240
241
242
243
244
# File 'lib/notion_ruby_mapping/properties/property.rb', line 235

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

#typeSymbol

Returns property type.

Returns:

  • (Symbol)

    property type



188
189
190
# File 'lib/notion_ruby_mapping/properties/property.rb', line 188

def type
  self.class::TYPE
end

#update_from_json(json) ⇒ Object

Parameters:

  • json (Hash)


180
181
182
183
184
185
# File 'lib/notion_ruby_mapping/properties/property.rb', line 180

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_jsonHash

Returns:

  • (Hash)


216
217
218
219
220
221
222
223
224
225
# File 'lib/notion_ruby_mapping/properties/property.rb', line 216

def update_property_schema_json
  assert_database_or_data_source_property __method__
  if @remove
    {@name => nil}
  elsif @new_name
    {@name => {"name" => @new_name}}
  else
    {}
  end
end