Class: Notion::Blocks::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/notion/blocks/builder.rb

Constant Summary collapse

ANNOTATIONS =
%i[bold italic strikethrough underline code color].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



10
11
12
# File 'lib/notion/blocks/builder.rb', line 10

def initialize
  @blocks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(type, content = nil, **options, &block) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/notion/blocks/builder.rb', line 14

def method_missing(type, content = nil, **options, &block)
  children = self.class.build(&block) if block
  annotations = options.slice(*ANNOTATIONS)
  payload = options.except(*ANNOTATIONS).merge("rich_text" => rich_text(content, annotations))
  payload["children"] = children if children
  @blocks << { "object" => "block", "type" => type.to_s, type.to_s => payload.compact }
  self
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



8
9
10
# File 'lib/notion/blocks/builder.rb', line 8

def blocks
  @blocks
end

Class Method Details

.buildObject



25
26
27
28
29
# File 'lib/notion/blocks/builder.rb', line 25

def self.build(&)
  builder = new
  builder.instance_eval(&)
  builder.blocks
end

Instance Method Details

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


23
# File 'lib/notion/blocks/builder.rb', line 23

def respond_to_missing?(_name, _include_private = false) = true