Class: StructuredParams::Type::Object

Inherits:
ActiveModel::Type::Value
  • Object
show all
Defined in:
lib/structured_params/type/object.rb

Overview

Custom type for single StructuredParams::Params objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_class:, **_options) ⇒ Object

: (value_class: singleton(StructuredParams::Params), **untyped) -> void



16
17
18
19
20
# File 'lib/structured_params/type/object.rb', line 16

def initialize(value_class:, **_options)
  super()
  validate_value_class!(value_class)
  @value_class = value_class
end

Instance Attribute Details

#value_classObject (readonly)

: singleton(StructuredParams::Params)



8
9
10
# File 'lib/structured_params/type/object.rb', line 8

def value_class
  @value_class
end

Instance Method Details

#cast(value) ⇒ Object

Cast value to StructuredParams::Params instance : (untyped) -> StructuredParams::Params?



29
30
31
32
33
34
# File 'lib/structured_params/type/object.rb', line 29

def cast(value)
  return nil if value.nil?
  return value if value.is_a?(@value_class)

  @value_class.new(value) # call new directly instead of cast_value method
end

#serialize(value) ⇒ Object

Serialize (convert to Hash) the object : (untyped) -> untyped



38
39
40
41
42
# File 'lib/structured_params/type/object.rb', line 38

def serialize(value)
  return nil if value.nil?

  value.is_a?(@value_class) ? value.attributes : value
end

#typeObject

: () -> Symbol



23
24
25
# File 'lib/structured_params/type/object.rb', line 23

def type
  :object
end