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_data_source_property, #assert_database_or_data_source_property, #assert_database_property, #assert_page_property, #clear_will_update, #contents?, create_from_json, #data_source?, #database?, #database_or_data_source?, #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, Symbol)
  • 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_or_data_source?
            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

#data_source_idObject



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

def data_source_id
  @json["data_source_id"]
end

#dual_property?TrueClass, FalseClass

Returns true if dual_property.

Returns:

  • (TrueClass, FalseClass)

    true if dual_property



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

def dual_property?
  assert_database_or_data_source_property __method__
  @json["type"] == "dual_property"
end

#property_values_jsonHash

Returns created json.

Returns:

  • (Hash)

    created json



125
126
127
128
129
130
131
132
133
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 125

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_data_source_idString

Returns relation data_source_id.



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

def relation_data_source_id
  assert_data_source_property __method__
  @json["data_source_id"]
end

#replace_relation_data_source(data_source_id: nil, type: "dual_property") ⇒ Object

Parameters:

  • data_source_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_data_source(data_source_id: nil, type: "dual_property")
  assert_database_or_data_source_property __method__
  @will_update = true
  @json["data_source_id"] = data_source_id if data_source_id
  @json["type"] = type
  @json[type] = {}
  @json.delete type == "dual_property" ? "single_property" : "dual_property"
  @json
end

#single_property?TrueClass, FalseClass

Returns true if single_property.

Returns:

  • (TrueClass, FalseClass)

    true if single_property



111
112
113
114
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 111

def single_property?
  assert_database_or_data_source_property __method__
  @json["type"] == "single_property"
end

#synced_property_idObject



116
117
118
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 116

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

#synced_property_nameObject



120
121
122
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 120

def synced_property_name
  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_or_data_source_property __method__
  ans = super
  return ans if ans != {} || !@will_update

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