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_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_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



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

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_or_data_source?
            {}
          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



61
62
63
64
65
66
67
68
69
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 61

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>

Adds a person to this people property.

This method marks the property as changed. This method does not remove duplicate users.

Parameters:

  • user_id_or_uo (String, UserObject)

    user ID or user object to add

Returns:

See Also:



36
37
38
39
40
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 36

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

#peopleArray<UserObject>, Hash

Returns the people assigned to this property.

For a page property, this returns an array of UserObject instances. For a database or data source property, this returns the schema payload.

Do not modify the returned array directly. Use people= or add_person to update the property.



22
23
24
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 22

def people
  @json
end

#people=(people) ⇒ Array<UserObject>

Replaces all people in this property.

Passing nil or an empty array clears the people property. This method marks the property as changed.

Parameters:

  • people (String, UserObject, Array<String>, Array<UserObject>, nil)

    user ID, user object, array of them, or nil

Returns:

See Also:



51
52
53
54
55
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 51

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



92
93
94
95
96
97
98
99
100
# File 'lib/notion_ruby_mapping/properties/people_property.rb', line 92

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)


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

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