Class: Riffer::Tools::Param Private

Inherits:
Object
  • Object
show all
Defined in:
lib/riffer/tools/param.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Riffer::Tools::Param represents a single parameter definition for a tool.

Handles type validation and JSON Schema generation for individual parameters.

Constant Summary collapse

TYPE_MAPPINGS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maps Ruby types to JSON Schema type strings

{
  String => "string",
  Integer => "integer",
  Float => "number",
  TrueClass => "boolean",
  FalseClass => "boolean",
  Array => "array",
  Hash => "object"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, required:, description: nil, enum: nil, default: nil) ⇒ Param

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new parameter definition

Parameters:

  • name (Symbol)

    the parameter name

  • type (Class)

    the expected Ruby type

  • required (Boolean)

    whether the parameter is required

  • description (String, nil) (defaults to: nil)

    optional description for the parameter

  • enum (Array, nil) (defaults to: nil)

    optional list of allowed values

  • default (Object, nil) (defaults to: nil)

    optional default value for optional parameters



29
30
31
32
33
34
35
36
# File 'lib/riffer/tools/param.rb', line 29

def initialize(name:, type:, required:, description: nil, enum: nil, default: nil)
  @name = name.to_sym
  @type = type
  @required = required
  @description = description
  @enum = enum
  @default = default
end

Instance Attribute Details

#defaultObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/riffer/tools/param.rb', line 20

def default
  @default
end

#descriptionObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/riffer/tools/param.rb', line 20

def description
  @description
end

#enumObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/riffer/tools/param.rb', line 20

def enum
  @enum
end

#nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/riffer/tools/param.rb', line 20

def name
  @name
end

#requiredObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/riffer/tools/param.rb', line 20

def required
  @required
end

#typeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
# File 'lib/riffer/tools/param.rb', line 20

def type
  @type
end

Instance Method Details

#to_json_schemaHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts this parameter to JSON Schema format

Returns:

  • (Hash)

    the JSON Schema representation



59
60
61
62
63
64
# File 'lib/riffer/tools/param.rb', line 59

def to_json_schema
  schema = {type: type_name}
  schema[:description] = description if description
  schema[:enum] = enum if enum
  schema
end

#type_nameString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the JSON Schema type name for this parameter

Returns:

  • (String)

    the JSON Schema type



53
54
55
# File 'lib/riffer/tools/param.rb', line 53

def type_name
  TYPE_MAPPINGS[type] || type.to_s.downcase
end

#valid_type?(value) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Validates that a value matches the expected type

Parameters:

  • value (Object)

    the value to validate

Returns:

  • (Boolean)

    true if valid, false otherwise



41
42
43
44
45
46
47
48
49
# File 'lib/riffer/tools/param.rb', line 41

def valid_type?(value)
  return true if value.nil? && !required

  if type == TrueClass || type == FalseClass
    value == true || value == false
  else
    value.is_a?(type)
  end
end