Class: Riffer::Tools::Params
- Inherits:
-
Object
- Object
- Riffer::Tools::Params
- Defined in:
- lib/riffer/tools/params.rb
Overview
Riffer::Tools::Params provides a DSL for defining tool parameters.
Used within a Tool’s params block to define required and optional parameters.
params do
required :city, String, description: "The city name"
optional :units, String, default: "celsius", enum: ["celsius", "fahrenheit"]
end
Instance Attribute Summary collapse
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
Instance Method Summary collapse
-
#initialize ⇒ Params
constructor
A new instance of Params.
-
#optional(name, type, description: nil, enum: nil, default: nil) ⇒ Object
Defines an optional parameter.
-
#required(name, type, description: nil, enum: nil) ⇒ Object
Defines a required parameter.
-
#to_json_schema ⇒ Object
Converts all parameters to JSON Schema format.
-
#validate(arguments) ⇒ Object
Validates arguments against parameter definitions.
Constructor Details
#initialize ⇒ Params
Returns a new instance of Params.
15 16 17 |
# File 'lib/riffer/tools/params.rb', line 15 def initialize @parameters = [] end |
Instance Attribute Details
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
13 14 15 |
# File 'lib/riffer/tools/params.rb', line 13 def parameters @parameters end |
Instance Method Details
#optional(name, type, description: nil, enum: nil, default: nil) ⇒ Object
Defines an optional parameter.
- name
-
Symbol - the parameter name
- type
-
Class - the expected Ruby type
- description
-
String or nil - optional description
- enum
-
Array or nil - optional list of allowed values
- default
-
Object or nil - default value when not provided
Returns void.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/riffer/tools/params.rb', line 46 def optional(name, type, description: nil, enum: nil, default: nil) @parameters << Riffer::Tools::Param.new( name: name, type: type, required: false, description: description, enum: enum, default: default ) end |
#required(name, type, description: nil, enum: nil) ⇒ Object
Defines a required parameter.
- name
-
Symbol - the parameter name
- type
-
Class - the expected Ruby type
- description
-
String or nil - optional description
- enum
-
Array or nil - optional list of allowed values
Returns void.
27 28 29 30 31 32 33 34 35 |
# File 'lib/riffer/tools/params.rb', line 27 def required(name, type, description: nil, enum: nil) @parameters << Riffer::Tools::Param.new( name: name, type: type, required: true, description: description, enum: enum ) end |
#to_json_schema ⇒ Object
Converts all parameters to JSON Schema format.
Returns Hash - the JSON Schema representation.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/riffer/tools/params.rb', line 102 def to_json_schema properties = {} required_params = [] @parameters.each do |param| properties[param.name.to_s] = param.to_json_schema required_params << param.name.to_s if param.required end { type: "object", properties: properties, required: required_params, additionalProperties: false } end |
#validate(arguments) ⇒ Object
Validates arguments against parameter definitions.
- arguments
-
Hash - the arguments to validate
Returns Hash - validated arguments with defaults applied.
Raises Riffer::ValidationError if validation fails.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/riffer/tools/params.rb', line 64 def validate(arguments) validated = {} errors = [] @parameters.each do |param| value = arguments[param.name] if value.nil? && param.required errors << "#{param.name} is required" next end if value.nil? validated[param.name] = param.default next end unless param.valid_type?(value) errors << "#{param.name} must be a #{param.type_name}" next end if param.enum && !param.enum.include?(value) errors << "#{param.name} must be one of: #{param.enum.join(", ")}" next end validated[param.name] = value end raise Riffer::ValidationError, errors.join("; ") if errors.any? validated end |