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_id: nil, property_cache: nil, query: nil) ⇒ RelationProperty

Returns a new instance of RelationProperty.

Parameters:

  • name (String)
  • json (Hash, Array) (defaults to: nil)
  • relation (String, Array) (defaults to: nil)

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

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

#database_idObject


100
101
102
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 100

def database_id
  @json["database_id"]
end

#property_values_jsonHash

Returns created json.

Returns:

  • (Hash)

    created json


113
114
115
116
117
118
119
120
121
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 113

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

#relationHash, ...


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.

Parameters:

  • page_ids_or_jsons (String, Array<String>, Hash, Array<Hash>)

Returns:

  • (Array)

    settled relation Array

See Also:


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") ⇒ Object

Parameters:

  • database_id (String) (defaults to: nil)
  • synced_property_name (String)

See Also:


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

def replace_relation_database(database_id: nil, type: "dual_property")
  assert_database_property __method__
  @will_update = true
  @json["database_id"] = database_id if database_id
  @json["type"] = type
  @json[type] = {}
  @json.delete type == "dual_property" ? "single_property" : "dual_property"
  @json
end

#synced_property_idObject


104
105
106
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 104

def synced_property_id
  @json["type"] == "dual_property" ? @json["dual_property"]["synced_property_id"] : nil
end

#synced_property_nameObject


108
109
110
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 108

def synced_property_name
  @json["type"] == "dual_property" ? @json["dual_property"]["synced_property_name"] : nil
end

#update_property_schema_jsonHash

Returns:

  • (Hash)

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

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