Class: Riffer::Tools::Param
- Inherits:
-
Object
- Object
- Riffer::Tools::Param
- Defined in:
- lib/riffer/tools/param.rb
Overview
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 =
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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#enum ⇒ Object
readonly
Returns the value of attribute enum.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#initialize(name:, type:, required:, description: nil, enum: nil, default: nil) ⇒ Param
constructor
Creates a new parameter definition.
-
#to_json_schema ⇒ Object
Converts this parameter to JSON Schema format.
-
#type_name ⇒ Object
Returns the JSON Schema type name for this parameter.
-
#valid_type?(value) ⇒ Boolean
Validates that a value matches the expected type.
Constructor Details
#initialize(name:, type:, required:, description: nil, enum: nil, default: nil) ⇒ Param
Creates a new parameter definition.
- name
-
Symbol - the parameter name
- type
-
Class - the expected Ruby type
- required
-
Boolean - whether the parameter is required
- description
-
String or nil - optional description for the parameter
- enum
-
Array or nil - optional list of allowed values
- default
-
Object or nil - optional default value for optional parameters
28 29 30 31 32 33 34 35 |
# File 'lib/riffer/tools/param.rb', line 28 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
#default ⇒ Object (readonly)
Returns the value of attribute default.
18 19 20 |
# File 'lib/riffer/tools/param.rb', line 18 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
18 19 20 |
# File 'lib/riffer/tools/param.rb', line 18 def description @description end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
18 19 20 |
# File 'lib/riffer/tools/param.rb', line 18 def enum @enum end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/riffer/tools/param.rb', line 18 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
18 19 20 |
# File 'lib/riffer/tools/param.rb', line 18 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
18 19 20 |
# File 'lib/riffer/tools/param.rb', line 18 def type @type end |
Instance Method Details
#to_json_schema ⇒ Object
Converts this parameter to JSON Schema format.
Returns Hash - the JSON Schema representation.
62 63 64 65 66 67 |
# File 'lib/riffer/tools/param.rb', line 62 def to_json_schema schema = {type: type_name} schema[:description] = description if description schema[:enum] = enum if enum schema end |
#type_name ⇒ Object
Returns the JSON Schema type name for this parameter.
Returns String - the JSON Schema type.
55 56 57 |
# File 'lib/riffer/tools/param.rb', line 55 def type_name TYPE_MAPPINGS[type] || type.to_s.downcase end |
#valid_type?(value) ⇒ Boolean
Validates that a value matches the expected type.
- value
-
Object - the value to validate
Returns Boolean - true if valid, false otherwise.
42 43 44 45 46 47 48 49 50 |
# File 'lib/riffer/tools/param.rb', line 42 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 |