Class: Steppe::Serializer
- Inherits:
-
Object
- Object
- Steppe::Serializer
- Extended by:
- Plumb::Composable
- Includes:
- Plumb::Attributes
- Defined in:
- lib/steppe/serializer.rb
Overview
Serializers automatically generate attribute reader methods that delegate to the @object instance variable (the response data)
The #result method provides access to the full Result context including request, params, errors, and response data
Type definitions support .example() for OpenAPI documentation generation
Base class for response serialization in Steppe endpoints.
Serializers transform response data into structured output using Plumb types and attributes. They provide a declarative way to define the structure of response bodies with type validation and examples for OpenAPI documentation.
Direct Known Subclasses
Constant Summary collapse
- RenderError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
-
.__plumb_define_attribute_reader_method__(name) ⇒ Object
Serialize an object using this serializer class.
-
.call(conn) ⇒ Result
Internal method called during endpoint processing to serialize results.
-
.render(conn) ⇒ String?
JSON string of serialized value or nil if no value.
Instance Method Summary collapse
- #conn ⇒ Object
-
#initialize(result) ⇒ Serializer
constructor
Initialize a new serializer instance.
-
#serialize ⇒ Hash
Serialize the object to a hash using defined attributes.
-
#serialize_attribute(key, type) ⇒ Object
Serialize a single attribute using its defined type.
Constructor Details
#initialize(result) ⇒ Serializer
Initialize a new serializer instance.
142 143 144 145 |
# File 'lib/steppe/serializer.rb', line 142 def initialize(result) @result = result @object = result.value end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
|
|
# File 'lib/steppe/serializer.rb', line 131
|
#result ⇒ Object (readonly)
Returns the value of attribute result.
136 |
# File 'lib/steppe/serializer.rb', line 136 attr_reader :object, :result |
Class Method Details
.__plumb_define_attribute_reader_method__(name) ⇒ Object
Serialize an object using this serializer class.
Internal method that defines attribute reader methods. Automatically creates methods that delegate to @object.attribute_name
103 104 105 106 107 |
# File 'lib/steppe/serializer.rb', line 103 def __plumb_define_attribute_reader_method__(name) class_eval <<~RUBY, __FILE__, __LINE__ + 1 def #{name} = @object.#{name} RUBY end |
.call(conn) ⇒ Result
Internal method called during endpoint processing to serialize results.
125 126 127 128 |
# File 'lib/steppe/serializer.rb', line 125 def call(conn) hash = new(conn).serialize conn.copy(value: hash) end |
.render(conn) ⇒ String?
Returns JSON string of serialized value or nil if no value.
113 114 115 116 117 118 |
# File 'lib/steppe/serializer.rb', line 113 def render(conn) result = call(conn) raise RenderError, result.errors if result.invalid? result.value ? JSON.dump(result.value) : nil end |
Instance Method Details
#conn ⇒ Object
147 |
# File 'lib/steppe/serializer.rb', line 147 def conn = result |
#serialize ⇒ Hash
Serialize the object to a hash using defined attributes.
Iterates through all defined attributes, calls the corresponding method (either auto-generated or custom), and applies the attribute’s type transformation and validation.
160 161 162 163 164 |
# File 'lib/steppe/serializer.rb', line 160 def serialize self.class._schema._schema.each.with_object({}) do |(key, type), ret| ret[key.to_sym] = serialize_attribute(key.to_sym, type) end end |
#serialize_attribute(key, type) ⇒ Object
Serialize a single attribute using its defined type.
174 175 176 177 178 |
# File 'lib/steppe/serializer.rb', line 174 def serialize_attribute(key, type) # Ex. value = self.name value = send(key) type.call(result.copy(value:)).value end |