Class: Treaty::Action::Request::Factory
- Inherits:
-
Object
- Object
- Treaty::Action::Request::Factory
- Defined in:
- lib/treaty/action/request/factory.rb
Overview
Factory for creating request attribute collections.
Purpose
Captures request attribute definitions from treaty DSL and provides access to the resulting attribute collection. Supports both inline block syntax and Entity class references.
Usage
Created internally by:
- Versions::Factory (when
request do ... endis called)
Consumed by:
- Request::Validator (to validate incoming params)
- Info::Builder (to build request schema information)
Definition Modes
Inline Block Mode
request do
object :post do
string :title, :required
end
end
Entity Class Mode
request Posts::Create::RequestEntity
Implementation
Uses method_missing to forward DSL calls to a dynamically created Entity class. The Entity class collects all attribute definitions.
Example
factory = Request::Factory.new
factory.object :post do
factory.string :title, :required
end
factory.collection_of_attributes # => Collection with post attribute
Instance Method Summary collapse
-
#collection_of_attributes ⇒ Treaty::Entity::Attribute::Collection
Returns the collection of defined attributes.
-
#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 request schema.
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.
82 83 84 85 86 |
# File 'lib/treaty/action/request/factory.rb', line 82 def method_missing(type, *helpers, **, &block) @entity_class ||= Class.new(Entity) @entity_class.public_send(type, *helpers, **, &block) end |
Instance Method Details
#collection_of_attributes ⇒ Treaty::Entity::Attribute::Collection
Returns the collection of defined attributes
66 67 68 69 70 |
# File 'lib/treaty/action/request/factory.rb', line 66 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
92 93 94 |
# File 'lib/treaty/action/request/factory.rb', line 92 def respond_to_missing?(name, *) super end |
#use_entity(entity_class) ⇒ void
This method returns an undefined value.
Registers an Entity class for request schema
Use this to reference a pre-defined Entity class instead of inline attribute definitions.
58 59 60 61 |
# File 'lib/treaty/action/request/factory.rb', line 58 def use_entity(entity_class) validate_entity_class!(entity_class) @entity_class = entity_class end |