Class: NotionRubyMapping::RelationProperty

Inherits:
MultiProperty show all
Defined in:
lib/notion_ruby_mapping/properties/relation_property.rb

Overview

MultiSelect property

Constant Summary collapse

TYPE =
"relation"

Instance Attribute Summary

Attributes inherited from Property

#name, #property_cache, #property_id, #will_update

Instance Method Summary collapse

Methods included from IsEmptyIsNotEmpty

#filter_is_empty, #filter_is_not_empty

Methods included from ContainsDoesNotContain

#filter_contains, #filter_does_not_contain

Methods inherited from Property

#assert_database_property, #assert_page_property, #clear_will_update, #contents?, create_from_json, #database?, #make_filter_query, #new_name=, #page?, #property_schema_json, #remove, #retrieve_page_property, #type, #update_from_json

Constructor Details

#initialize(name, will_update: false, json: nil, relation: nil, base_type: :page, property_cache: nil, query: nil) ⇒ RelationProperty



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 73

def initialize(name, will_update: false, json: nil, relation: nil, base_type: :page, property_cache: nil, query: nil)
  super name, will_update: will_update, base_type: base_type, property_cache: property_cache, query: query
  @json = if database?
            json || {}
          elsif relation
            Array(relation).map { |r| {"id" => r} }
          elsif json
            json
          else
            []
          end
end

Instance Method Details

#add_relation(page_id_or_json) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 22

def add_relation(page_id_or_json)
  assert_page_property __method__
  @will_update = true
  @json << if page_id_or_json.is_a? String
             {"id" => page_id_or_json}
           else
             page_id_or_json
           end
end

#property_values_jsonHash



100
101
102
103
104
105
106
107
108
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 100

def property_values_json
  assert_page_property __method__
  {
    @name => {
      "type" => "relation",
      "relation" => @json,
    },
  }
end

#relationHash, Array



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

def relation
  @json
end

#relation=(page_ids_or_jsons) ⇒ Array

Returns settled relation Array.



35
36
37
38
39
40
41
42
43
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 35

def relation=(page_ids_or_jsons)
  assert_page_property __method__
  @will_update = true
  page_ids_or_jsons = [page_ids_or_jsons] unless page_ids_or_jsons.is_a? Array

  @json = page_ids_or_jsons.map do |page_id_or_json|
    page_id_or_json.is_a?(String) ? {"id" => page_id_or_json} : page_id_or_json
  end
end

#relation_database_idString

Returns relation database_id.



49
50
51
52
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 49

def relation_database_id
  assert_database_property __method__
  @json["database_id"]
end

#replace_relation_database(database_id: nil, type: "dual_property", synced_property_name: nil) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 57

def replace_relation_database(database_id: nil, type: "dual_property", synced_property_name: nil)
  assert_database_property __method__
  @will_update = true
  @json["database_id"] = database_id if database_id
  @json["type"] = type
  @json["synced_property_name"] = synced_property_name if synced_property_name
  @json
end

#update_property_schema_jsonHash



89
90
91
92
93
94
95
96
97
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 89

def update_property_schema_json
  assert_database_property __method__
  ans = super
  return ans if ans != {} || !@will_update

  ans[@name] ||= {}
  ans[@name]["relation"] = @json
  ans
end