Class: Notion::Objects::PropertyValue

Inherits:
Base
  • Object
show all
Defined in:
lib/notion/objects/property_value.rb

Constant Summary collapse

TYPES =
%w[
  button checkbox created_by created_time date email files formula last_edited_by
  last_edited_time multi_select number people phone_number place relation rich_text
  rollup select status title unique_id url verification
].freeze

Instance Attribute Summary

Attributes inherited from Base

#raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #deconstruct_keys, #in_trash?, #initialize, #inspect

Constructor Details

This class inherits a constructor from Notion::Objects::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



30
31
32
33
34
35
# File 'lib/notion/objects/property_value.rb', line 30

def method_missing(name, *args)
  key = attribute_name(name)
  return super unless args.empty? && value.is_a?(Hash) && value.key?(key)

  name.end_with?("?") ? !!value[key] : ObjectFactory.wrap_value(value[key])
end

Class Method Details

.build(raw) ⇒ Object



41
42
43
44
45
# File 'lib/notion/objects/property_value.rb', line 41

def self.build(raw)
  type = raw["type"].to_s
  klass = const_defined?(class_name(type), false) ? const_get(class_name(type), false) : self
  klass.new(raw)
end

.class_name(type) ⇒ Object



47
48
49
# File 'lib/notion/objects/property_value.rb', line 47

def self.class_name(type)
  type.split("_").map(&:capitalize).join
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/notion/objects/property_value.rb', line 26

def respond_to_missing?(name, include_private = false)
  (value.is_a?(Hash) && value.key?(attribute_name(name))) || super
end

#to_sObject



16
17
18
19
20
21
22
23
24
# File 'lib/notion/objects/property_value.rb', line 16

def to_s
  case raw["type"]
  when "title", "rich_text"
    Array(value).map { |item| item["plain_text"] || item.dig("text", "content") }.join
  when "select", "status" then value&.fetch("name", "").to_s
  when "multi_select" then Array(value).filter_map { |item| item["name"] }.join(", ")
  else value.to_s
  end
end

#valueObject



12
13
14
# File 'lib/notion/objects/property_value.rb', line 12

def value
  raw[raw["type"]]
end