Class: JSON::Schema::Leaf
- Inherits:
-
Object
- Object
- JSON::Schema::Leaf
- Defined in:
- lib/json/schema/leaf.rb
Overview
The JSON::Schema::Leaf class is the superclass of all values that can appear in a JSON schema. See the instance methods of JSON::Schema for an example of how to create instances of JSON::Schema::Leaf through its subclasses.
Instance Method Summary collapse
-
#required? ⇒ Boolean
-
#description(str) ⇒ JSON::Schema::Leaf
Set the description of a leaf.
-
#default(value) ⇒ JSON::Schema::Leaf
Set the default value of a leaf.
-
#enum(*values) ⇒ JSON::Schema::Leaf
Set the allowed values of a leaf.
-
#const(value) ⇒ JSON::Schema::Leaf
Set the value of a leaf to be a constant value.
-
#required ⇒ JSON::Schema::Leaf
Denote a leaf as required.
-
#to_h ⇒ Hash
-
#to_json(options = {}) ⇒ String
-
#initialize ⇒ Leaf
constructor
A new instance of Leaf.
Constructor Details
#initialize ⇒ Leaf
Returns a new instance of Leaf.
11 12 13 14 15 16 17 |
# File 'lib/json/schema/leaf.rb', line 11 def initialize @description = nil @default = nil @enum = nil @required = nil @const = nil end |
Instance Method Details
#description(str) ⇒ JSON::Schema::Leaf
Set the description of a leaf
23 24 25 |
# File 'lib/json/schema/leaf.rb', line 23 def description(str) tap { @description = str } end |
#default(value) ⇒ JSON::Schema::Leaf
Set the default value of a leaf
31 32 33 |
# File 'lib/json/schema/leaf.rb', line 31 def default(value) tap { @default = value } end |
#enum(*values) ⇒ JSON::Schema::Leaf
Set the allowed values of a leaf
40 41 42 |
# File 'lib/json/schema/leaf.rb', line 40 def enum(*values) tap { @enum = values } end |
#const(value) ⇒ JSON::Schema::Leaf
Set the value of a leaf to be a constant value
49 50 51 |
# File 'lib/json/schema/leaf.rb', line 49 def const(value) tap { @const = value } end |
#required ⇒ JSON::Schema::Leaf
Denote a leaf as required
56 57 58 |
# File 'lib/json/schema/leaf.rb', line 56 def required tap { @required = true } end |
#to_h ⇒ Hash
62 63 64 |
# File 'lib/json/schema/leaf.rb', line 62 def to_h {description: @description, default: @default, enum: @enum}.compact end |
#to_json(options = {}) ⇒ String
68 69 70 |
# File 'lib/json/schema/leaf.rb', line 68 def to_json( = {}) to_h.to_json() end |