Class: NotionRubyMapping::Property

Inherits:
Object
  • Object
show all
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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/notion_ruby_mapping/properties/property.rb', line 36

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



10
11
12
# File 'lib/notion_ruby_mapping/properties/property.rb', line 10

def name
  @name
end

#property_cacheObject

Returns the value of attribute property_cache.



11
12
13
# File 'lib/notion_ruby_mapping/properties/property.rb', line 11

def property_cache
  @property_cache
end

#property_idObject (readonly)

Common methods



10
11
12
# File 'lib/notion_ruby_mapping/properties/property.rb', line 10

def property_id
  @property_id
end

#will_updateObject (readonly)

Common methods



10
11
12
# File 'lib/notion_ruby_mapping/properties/property.rb', line 10

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)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/notion_ruby_mapping/properties/property.rb', line 54

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?
    raise StandardError, "Property value is not returned: #{name}"
  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
    input_type = input_json["property_item"]["type"]
    case input_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, "Unsupported property type: #{input_type} (#{name}). Please update notion_ruby_mapping."
    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,
      place: PlaceProperty,
      select: SelectProperty,
      status: StatusProperty,
      title: TitleProperty,
      rich_text: RichTextProperty,
      unique_id: UniqueIdProperty,
      url: UrlProperty,
      verification: VerificationProperty,
    }[type.to_sym]
    unless klass
      raise StandardError,
            "Unsupported property type: #{type} (#{name}). Please update notion_ruby_mapping."
    end

    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

Parameters:

  • method (Symbol, nil)

Raises:

  • (StandardError)


191
192
193
# File 'lib/notion_ruby_mapping/properties/property.rb', line 191

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)


179
180
181
# File 'lib/notion_ruby_mapping/properties/property.rb', line 179

def assert_database_or_data_source_property(method)
  raise StandardError, "#{method} can execute only Database or DataSource property." unless database_or_data_source?
end

#assert_page_property(method) ⇒ Object

Parameters:

  • method (Symbol, nil)

Raises:

  • (StandardError)


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

def assert_page_property(method)
  raise StandardError, "#{method} can execute only Page property." unless page?
end

#clear_will_updateFalseClass

Returns:

  • (FalseClass)


124
125
126
127
128
# File 'lib/notion_ruby_mapping/properties/property.rb', line 124

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

#data_source?Boolean

Returns:

  • (Boolean)


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

def data_source?
  @base_type == "data_source"
end

#database?TrueClass, FalseClass

Returns true if database property.

Returns:

  • (TrueClass, FalseClass)

    true if database property



131
132
133
# File 'lib/notion_ruby_mapping/properties/property.rb', line 131

def database?
  @base_type == "database"
end

#database_or_data_source?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/notion_ruby_mapping/properties/property.rb', line 139

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 (String)

    query parameter

  • value (Object)

    query value

Returns:



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/notion_ruby_mapping/properties/property.rb', line 146

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)


16
17
18
19
20
# File 'lib/notion_ruby_mapping/properties/property.rb', line 16

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



159
160
161
# File 'lib/notion_ruby_mapping/properties/property.rb', line 159

def page?
  @base_type == "page"
end

#property_schema_jsonHash

Returns:

  • (Hash)


184
185
186
187
# File 'lib/notion_ruby_mapping/properties/property.rb', line 184

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

#removeNotionRubyMapping::Property

Returns self.

Returns:



23
24
25
26
27
28
# File 'lib/notion_ruby_mapping/properties/property.rb', line 23

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

#retrieve_page_propertyNotionRubyMapping::Property, ...

Returns:

Raises:

  • (StandardError)


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

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



172
173
174
# File 'lib/notion_ruby_mapping/properties/property.rb', line 172

def type
  self.class::TYPE
end

#update_from_json(json) ⇒ Object

Parameters:

  • json (Hash)


164
165
166
167
168
169
# File 'lib/notion_ruby_mapping/properties/property.rb', line 164

def update_from_json(json)
  @will_update = false
  return unless json.key?(type)

  @json = json[type]
end

#update_property_schema_jsonHash

Returns:

  • (Hash)


196
197
198
199
200
201
202
203
204
205
# File 'lib/notion_ruby_mapping/properties/property.rb', line 196

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