Class: NotionRubyMapping::PeopleProperty

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

Overview

PeopleProperty class

Constant Summary collapse

TYPE =
"people"

Instance Attribute Summary

Attributes inherited from Property

#name, #property_cache, #property_id, #will_update

Class Method Summary collapse

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_property_schema_json

Constructor Details

#initialize(name, will_update: false, base_type: "page", json: nil, people: nil, property_id: nil, property_cache: nil, query: nil) ⇒ PeopleProperty

Returns a new instance of PeopleProperty.

Parameters:

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

    ids for people



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 55

def initialize(name, will_update: false, base_type: "page", json: nil, people: nil, 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?
            {}
          elsif people
            Array(people).map { |uo| UserObject.user_object(uo) }
          elsif json
            PeopleProperty.people_from_json json
          else
            []
          end
end

Class Method Details

.people_from_json(json) ⇒ Object

Common methods



42
43
44
45
46
47
48
49
50
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 42

def self.people_from_json(json)
  if json.is_a? Array
    json.map { |sub_json| UserObject.new json: sub_json }
  elsif json["object"] == "list"
    List.new(json: json, type: "property", value: self).select { true }
  else
    json["people"].map { |sub_json| UserObject.new json: sub_json }
  end
end

Instance Method Details

#add_person(user_id_or_uo) ⇒ Array<UserObject>



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

def add_person(user_id_or_uo)
  assert_page_property __method__
  @will_update = true
  @json << UserObject.user_object(user_id_or_uo)
end

#peopleArray, Hash



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

def people
  @json
end

#people=(people) ⇒ Array?

Returns replaced array.

Parameters:

  • people (Hash)

Returns:

  • (Array, nil)

    replaced array

See Also:



32
33
34
35
36
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 32

def people=(people)
  assert_page_property __method__
  @will_update = true
  @json = people ? Array(people).map { |uo| UserObject.user_object(uo) } : []
end

#property_values_jsonHash

Returns created json.

Returns:

  • (Hash)

    created json



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

def property_values_json
  assert_page_property __method__
  {
    @name => {
      "type" => "people",
      "people" => @json.map(&:property_values_json),
    },
  }
end

#update_from_json(json) ⇒ Hash, Array

Parameters:

  • json (Array)

Returns:

  • (Hash, Array)


85
86
87
88
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 85

def update_from_json(json)
  @will_update = false
  @json = database? ? {} : PeopleProperty.people_from_json(json)
end