Class: Treaty::Action::Response::Factory
- Inherits:
-
Object
- Object
- Treaty::Action::Response::Factory
- Defined in:
- lib/treaty/action/response/factory.rb
Overview
Factory for creating response attribute collections.
Purpose
Captures response attribute definitions from treaty DSL and provides access to the resulting attribute collection. Supports both inline block syntax and Entity class references.
Difference from Request::Factory
Response::Factory stores HTTP status code along with attributes. This allows treaty to validate responses against expected status.
Usage
Created internally by:
- Versions::Factory (when
response STATUS do ... endis called)
Consumed by:
- Response::Validator (to validate service output)
- Info::Builder (to build response schema information)
- Versions::Execution::Base (to set response status)
Definition Modes
Inline Block Mode
response 201 do
object :post do
string :id
string :title
end
end
Entity Class Mode
response 201, Posts::Create::ResponseEntity
Example
factory = Response::Factory.new(201)
factory.status # => 201
factory.object :post do
factory.string :id
end
factory.collection_of_attributes # => Collection with post attribute
Instance Attribute Summary collapse
-
#status ⇒ Integer
readonly
HTTP status code for this response.
Instance Method Summary collapse
-
#collection_of_attributes ⇒ Treaty::Entity::Attribute::Collection
Returns the collection of defined attributes.
-
#initialize(status) ⇒ Factory
constructor
Creates new response factory with HTTP status.
-
#method_missing(type, *helpers, **options, &block) ⇒ void
Forwards DSL method calls to internal Entity class.
-
#respond_to_missing?(name) ⇒ Boolean
Checks if method should be handled by method_missing.
-
#use_entity(entity_class) ⇒ void
Registers an Entity class for response schema.
Constructor Details
#initialize(status) ⇒ Factory
Creates new response factory with HTTP status
59 60 61 |
# File 'lib/treaty/action/response/factory.rb', line 59 def initialize(status) @status = status end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, *helpers, **options, &block) ⇒ void
This method returns an undefined value.
Forwards DSL method calls to internal Entity class
Creates an anonymous Entity class on first call, then forwards all DSL methods (string, integer, object, etc.) to it.
95 96 97 98 99 |
# File 'lib/treaty/action/response/factory.rb', line 95 def method_missing(type, *helpers, **, &block) @entity_class ||= Class.new(Entity) @entity_class.public_send(type, *helpers, **, &block) end |
Instance Attribute Details
#status ⇒ Integer (readonly)
Returns HTTP status code for this response.
54 55 56 |
# File 'lib/treaty/action/response/factory.rb', line 54 def status @status end |
Instance Method Details
#collection_of_attributes ⇒ Treaty::Entity::Attribute::Collection
Returns the collection of defined attributes
79 80 81 82 83 |
# File 'lib/treaty/action/response/factory.rb', line 79 def collection_of_attributes return Treaty::Entity::Attribute::Collection.new if @entity_class.nil? @entity_class.collection_of_attributes end |
#respond_to_missing?(name) ⇒ Boolean
Checks if method should be handled by method_missing
105 106 107 |
# File 'lib/treaty/action/response/factory.rb', line 105 def respond_to_missing?(name, *) super end |
#use_entity(entity_class) ⇒ void
This method returns an undefined value.
Registers an Entity class for response schema
Use this to reference a pre-defined Entity class instead of inline attribute definitions.
71 72 73 74 |
# File 'lib/treaty/action/response/factory.rb', line 71 def use_entity(entity_class) validate_entity_class!(entity_class) @entity_class = entity_class end |