Class: Notion::Objects::Base

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/notion/objects/base.rb', line 8

def initialize(raw)
  @raw = deep_freeze(raw)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



34
35
36
37
38
39
# File 'lib/notion/objects/base.rb', line 34

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

  name.end_with?("?") ? !!raw[key] : value_for(key)
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/notion/objects/base.rb', line 6

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  value_for(key.to_s)
end

#deconstruct_keys(keys) ⇒ Object



16
17
18
19
# File 'lib/notion/objects/base.rb', line 16

def deconstruct_keys(keys)
  selected = keys || raw.keys
  selected.to_h { |key| [key.to_sym, value_for(key.to_s)] }
end

#in_trash?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/notion/objects/base.rb', line 21

def in_trash?
  !!(raw["in_trash"] || raw["archived"])
end

#inspectObject



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

def inspect
  attributes = %w[object id type].filter_map { |key| "#{key}=#{raw[key].inspect}" if raw.key?(key) }
  "#<#{self.class} #{attributes.join(' ')}>"
end

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

Returns:

  • (Boolean)


30
31
32
# File 'lib/notion/objects/base.rb', line 30

def respond_to_missing?(name, include_private = false)
  raw.key?(attribute_name(name)) || super
end