Class: Toolchest::ParamDefinition
- Inherits:
-
Object
- Object
- Toolchest::ParamDefinition
- Defined in:
- lib/toolchest/param_definition.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#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.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #has_default? ⇒ Boolean
-
#initialize(name:, type:, description: "", optional: false, enum: nil, default: :__unset__, &block) ⇒ ParamDefinition
constructor
A new instance of ParamDefinition.
- #required? ⇒ Boolean
- #to_json_schema ⇒ Object
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
#children ⇒ Object (readonly)
Returns the value of attribute children.
3 4 5 |
# File 'lib/toolchest/param_definition.rb', line 3 def children @children end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
3 4 5 |
# File 'lib/toolchest/param_definition.rb', line 3 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/toolchest/param_definition.rb', line 3 def description @description end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
3 4 5 |
# File 'lib/toolchest/param_definition.rb', line 3 def enum @enum end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/toolchest/param_definition.rb', line 3 def name @name end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
3 4 5 |
# File 'lib/toolchest/param_definition.rb', line 3 def optional @optional end |
#type ⇒ Object (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
23 |
# File 'lib/toolchest/param_definition.rb', line 23 def has_default? = @default != :__unset__ |
#required? ⇒ Boolean
21 |
# File 'lib/toolchest/param_definition.rb', line 21 def required? = !@optional |
#to_json_schema ⇒ Object
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 |