Class: NotionRubyMapping::RelationProperty
- Inherits:
-
MultiProperty
- Object
- Property
- MultiProperty
- NotionRubyMapping::RelationProperty
- Defined in:
- lib/notion_ruby_mapping/properties/relation_property.rb
Overview
Relation property
Constant Summary collapse
- TYPE =
"relation"- RELATION_PROPERTY_TYPES =
%w[dual_property single_property].freeze
Instance Attribute Summary
Attributes inherited from Property
#name, #property_cache, #property_id, #will_update
Instance Method Summary collapse
- #add_relation(page_id_or_json) ⇒ Object
-
#dual_property? ⇒ TrueClass, FalseClass
True if dual_property.
-
#initialize(name, will_update: false, json: nil, relation: nil, base_type: "page", property_id: nil, property_cache: nil, query: nil) ⇒ RelationProperty
constructor
A new instance of RelationProperty.
-
#property_values_json ⇒ Hash
Created json.
-
#relation ⇒ Array<Hash>, ...
Relation value or relation schema.
-
#relation=(page_ids_or_jsons) ⇒ Array
Settled relation Array.
-
#relation_data_source_id ⇒ String
Relation data_source_id.
- #replace_relation_data_source(data_source_id: nil, type: "dual_property") ⇒ Object
-
#single_property? ⇒ TrueClass, FalseClass
True if single_property.
- #synced_property_id ⇒ Object
- #synced_property_name ⇒ Object
- #update_property_schema_json ⇒ Hash
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_page_property, #clear_will_update, 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.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 78 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
23 24 25 26 27 28 29 30 31 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 23 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 |
#dual_property? ⇒ TrueClass, FalseClass
Returns 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_json ⇒ Hash
Returns 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 |
#relation ⇒ Array<Hash>, ...
Returns relation value or relation schema.
15 16 17 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 15 def relation @json end |
#relation=(page_ids_or_jsons) ⇒ Array
Returns settled relation Array.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 36 def relation=(page_ids_or_jsons) assert_page_property __method__ @will_update = true page_ids_or_jsons = [] if page_ids_or_jsons.nil? 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_id ⇒ String
Returns relation data_source_id.
51 52 53 54 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 51 def relation_data_source_id assert_database_or_data_source_property __method__ @json["data_source_id"] end |
#replace_relation_data_source(data_source_id: nil, type: "dual_property") ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 59 def replace_relation_data_source(data_source_id: nil, type: "dual_property") assert_database_or_data_source_property __method__ raise ArgumentError, "type must be dual_property or single_property" unless RELATION_PROPERTY_TYPES.include? type @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.
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_id ⇒ Object
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_name ⇒ Object
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_json ⇒ Hash
94 95 96 97 98 99 100 101 102 |
# File 'lib/notion_ruby_mapping/properties/relation_property.rb', line 94 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 |