Class: Truffle::Tool::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/truffle/tool.rb

Overview

Collects param declarations and the run block into a JSON Schema + handler.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



60
61
62
63
64
# File 'lib/truffle/tool.rb', line 60

def initialize
  @properties = {}
  @required = []
  @handler = ->(**) { "" }
end

Instance Attribute Details

#handlerObject (readonly)

Returns the value of attribute handler.



58
59
60
# File 'lib/truffle/tool.rb', line 58

def handler
  @handler
end

Instance Method Details

#param(name, type, description = nil, required: false, **extra) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/truffle/tool.rb', line 66

def param(name, type, description = nil, required: false, **extra)
  spec = { type: type.to_s }
  spec[:description] = description if description
  spec.merge!(extra)
  @properties[name.to_s] = spec
  @required << name.to_s if required
  self
end

#run(&block) ⇒ Object

Raises:

  • (ArgumentError)


75
76
77
78
79
# File 'lib/truffle/tool.rb', line 75

def run(&block)
  raise ArgumentError, "run requires a block" unless block

  @handler = block
end

#schemaObject



81
82
83
84
85
86
87
# File 'lib/truffle/tool.rb', line 81

def schema
  {
    type: "object",
    properties: @properties,
    required: @required
  }
end