Class: Toolchest::ParamDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/toolchest/param_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, description: "", optional: false, enum: nil, default: :__unset__, &block) ⇒ ParamDefinition

Returns a new instance of ParamDefinition.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/toolchest/param_definition.rb', line 5

def initialize(name:, type:, description: "", optional: false, enum: nil, default: :__unset__, &block)
  @name = name.to_sym
  @type = type
  @description = description
  @optional = optional
  @enum = enum
  @default = default
  @children = []

  if block
    builder = ToolBuilder.new
    builder.instance_eval(&block)
    @children = builder.params
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def children
  @children
end

#defaultObject (readonly)

Returns the value of attribute default.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def default
  @default
end

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def description
  @description
end

#enumObject (readonly)

Returns the value of attribute enum.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def enum
  @enum
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def optional
  @optional
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/toolchest/param_definition.rb', line 3

def type
  @type
end

Instance Method Details

#has_default?Boolean

Returns:

  • (Boolean)


23
# File 'lib/toolchest/param_definition.rb', line 23

def has_default? = @default != :__unset__

#required?Boolean

Returns:

  • (Boolean)


21
# File 'lib/toolchest/param_definition.rb', line 21

def required? = !@optional

#to_json_schemaObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/toolchest/param_definition.rb', line 25

def to_json_schema
  schema = case @type
  when :string
    { type: "string" }
  when :integer
    { type: "integer" }
  when :number
    { type: "number" }
  when :boolean
    { type: "boolean" }
  when :object
    object_schema
  when Array
    if @type.first == :object
      { type: "array", items: object_schema }
    else
      { type: "array", items: { type: @type.first.to_s } }
    end
  else
    { type: @type.to_s }
  end

  schema[:description] = @description if @description.present?
  schema[:enum] = @enum if @enum
  schema[:default] = @default if has_default?
  schema
end