Class: JsonModel::TypeSpec::Const
- Inherits:
-
JsonModel::TypeSpec
- Object
- JsonModel::TypeSpec
- JsonModel::TypeSpec::Const
- Defined in:
- lib/json_model/type_spec/const.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
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.
9 10 11 12 13 14 15 16 |
# File 'lib/json_model/type_spec/const.rb', line 9 def initialize(value) super() @value = value if value.blank? raise(ArgumentError, 'Const type spec requires a non-empty value') end end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/json_model/type_spec/const.rb', line 6 def value @value end |
Instance Method Details
#as_schema(**_options) ⇒ Hash
20 21 22 23 24 |
# File 'lib/json_model/type_spec/const.rb', line 20 def as_schema(**) { const: value, }.compact end |
#register_validations(name, klass) ⇒ Object
28 29 30 31 32 |
# File 'lib/json_model/type_spec/const.rb', line 28 def register_validations(name, klass) super klass.validates(name, inclusion: { in: value }, allow_nil: true) end |