Class: JsonModel::TypeSpec::Const

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

Instance Attribute Summary collapse

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)


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

#valueObject (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

Parameters:

  • _options (Hash)

Returns:

  • (Hash)


20
21
22
23
24
# File 'lib/json_model/type_spec/const.rb', line 20

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

#register_validations(name, klass) ⇒ Object

Parameters:

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


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