Class: Kernai::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/kernai/block.rb

Constant Summary collapse

TYPES =
%i[command json final plan result error].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, content:, name: nil) ⇒ Block

Returns a new instance of Block.



9
10
11
12
13
# File 'lib/kernai/block.rb', line 9

def initialize(type:, content:, name: nil)
  @type = type.to_sym
  @name = name
  @content = content
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/kernai/block.rb', line 7

def content
  @content
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/kernai/block.rb', line 7

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/kernai/block.rb', line 7

def type
  @type
end

Class Method Details

.define(type, &handler) ⇒ Object



22
23
24
# File 'lib/kernai/block.rb', line 22

def define(type, &handler)
  handlers[type.to_sym] = handler
end

.handler_for(type) ⇒ Object



26
27
28
# File 'lib/kernai/block.rb', line 26

def handler_for(type)
  handlers[type.to_sym]
end

.reset_handlers!Object



30
31
32
# File 'lib/kernai/block.rb', line 30

def reset_handlers!
  @handlers = {}
end

Instance Method Details

#to_hObject



15
16
17
18
19
# File 'lib/kernai/block.rb', line 15

def to_h
  h = { type: @type, content: @content }
  h[:name] = @name if @name
  h
end