Class: JsonModel::TypeSpec::Const
- Inherits:
-
JsonModel::TypeSpec
- Object
- JsonModel::TypeSpec
- JsonModel::TypeSpec::Const
- Defined in:
- lib/json_model/type_spec/const.rb
Instance Method Summary collapse
- #as_schema(**_options) ⇒ Hash
-
#initialize(value) ⇒ Const
constructor
A new instance of Const.
- #register_validations(name, klass) ⇒ Object
Methods inherited from JsonModel::TypeSpec
#cast, #referenced_schemas, resolve
Constructor Details
#initialize(value) ⇒ Const
Returns a new instance of Const.
7 8 9 10 11 12 13 14 |
# File 'lib/json_model/type_spec/const.rb', line 7 def initialize(value) super() @value = value if value.blank? raise(ArgumentError, 'Const type spec requires a non-empty value') end end |
Instance Method Details
#as_schema(**_options) ⇒ Hash
18 19 20 21 22 |
# File 'lib/json_model/type_spec/const.rb', line 18 def as_schema(**) { const: @value, }.compact end |
#register_validations(name, klass) ⇒ Object
26 27 28 29 30 |
# File 'lib/json_model/type_spec/const.rb', line 26 def register_validations(name, klass) super klass.validates(name, inclusion: { in: @value }, allow_nil: true) end |