Class: EasyTalk::SchemaDefinition
- Inherits:
-
Object
- Object
- EasyTalk::SchemaDefinition
- Defined in:
- lib/easy_talk/schema_definition.rb
Overview
EasyTalk SchemaDefinition
SchemaDefinition provides the methods for defining a schema within the define_schema block. The @schema is a hash that contains the unvalidated schema definition for the model. A SchemaDefinition instanace is the passed to the Builder.build_schema method to validate and compile the schema.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #compose(*subschemas) ⇒ Object
-
#initialize(name, schema = {}) ⇒ SchemaDefinition
constructor
A new instance of SchemaDefinition.
- #optional? ⇒ Boolean
- #property(name, type, **constraints, &blk) ⇒ Object
Methods included from T::AnyOf
Methods included from T::OneOf
Methods included from T::AllOf
Constructor Details
#initialize(name, schema = {}) ⇒ SchemaDefinition
Returns a new instance of SchemaDefinition.
19 20 21 22 |
# File 'lib/easy_talk/schema_definition.rb', line 19 def initialize(name, schema = {}) @schema = schema @name = name end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/easy_talk/schema_definition.rb', line 17 def name @name end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
17 18 19 |
# File 'lib/easy_talk/schema_definition.rb', line 17 def schema @schema end |
Instance Method Details
#compose(*subschemas) ⇒ Object
30 31 32 33 |
# File 'lib/easy_talk/schema_definition.rb', line 30 def compose(*subschemas) @schema[:subschemas] ||= [] @schema[:subschemas] += subschemas end |
#optional? ⇒ Boolean
50 51 52 |
# File 'lib/easy_talk/schema_definition.rb', line 50 def optional? @schema[:optional] end |
#property(name, type, **constraints, &blk) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/easy_talk/schema_definition.rb', line 38 def property(name, type, **constraints, &blk) @schema[:properties] ||= {} if block_given? property_schema = SchemaDefinition.new(name, constraints) property_schema.instance_eval(&blk) @schema[:properties][name] = property_schema else @schema[:properties][name] = { type:, constraints: } end end |