Class: JsonModel::TypeSpec::Const

Inherits:
JsonModel::TypeSpec show all
Defined in:
lib/json_model/type_spec/const.rb

Instance Method Summary collapse

Methods inherited from JsonModel::TypeSpec

#cast, #referenced_schemas, resolve

Constructor Details

#initialize(value) ⇒ Const

Returns a new instance of Const.

Parameters:

  • value (String)


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

Parameters:

  • _options (Hash)

Returns:

  • (Hash)


18
19
20
21
22
# File 'lib/json_model/type_spec/const.rb', line 18

def as_schema(**_options)
  {
    const: @value,
  }.compact
end

#register_validations(name, klass) ⇒ Object

Parameters:

  • name (Symbol)
  • klass (ActiveModel::Validations)


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