Class: StructuredParams::Type::Object
- Inherits:
-
ActiveModel::Type::Value
- Object
- ActiveModel::Type::Value
- StructuredParams::Type::Object
- Defined in:
- lib/structured_params/type/object.rb
Overview
Custom type for single StructuredParams::Params objects
Instance Attribute Summary collapse
-
#value_class ⇒ Object
readonly
: singleton(StructuredParams::Params).
Instance Method Summary collapse
-
#cast(value) ⇒ Object
Cast value to StructuredParams::Params instance : (untyped) -> StructuredParams::Params?.
-
#initialize(value_class:, **_options) ⇒ Object
constructor
: (value_class: singleton(StructuredParams::Params), **untyped) -> void.
-
#serialize(value) ⇒ Object
Serialize (convert to Hash) the object : (untyped) -> untyped.
-
#type ⇒ Object
: () -> Symbol.
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:, **) super() validate_value_class!(value_class) @value_class = value_class end |
Instance Attribute Details
#value_class ⇒ Object (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 |
#type ⇒ Object
: () -> Symbol
23 24 25 |
# File 'lib/structured_params/type/object.rb', line 23 def type :object end |