Class: Treaty::Request::Factory
- Inherits:
-
Object
- Object
- Treaty::Request::Factory
- Defined in:
- lib/treaty/request/factory.rb
Overview
Factory for creating request definitions.
Supports two modes:
-
Block mode: Creates an anonymous Request::Entity class with the block
-
Entity mode: Uses a provided Entity class directly
## Block Mode
“‘ruby request do
object :post do
string :title
end
end “‘
## Entity Mode
“‘ruby request PostRequestEntity “`
Instance Method Summary collapse
-
#collection_of_attributes ⇒ Collection
Returns collection of attributes from the entity class.
-
#method_missing(type, *helpers, **options, &block) ⇒ Object
Handles DSL methods for defining attributes.
- #respond_to_missing?(name) ⇒ Boolean
-
#use_entity(entity_class) ⇒ void
Uses a provided Entity class.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(type, *helpers, **options, &block) ⇒ Object
Handles DSL methods for defining attributes
This allows the factory to be used with method_missing for backwards compatibility with direct method calls. Creates an anonymous Request::Entity class on first use.
51 52 53 54 55 56 57 |
# File 'lib/treaty/request/factory.rb', line 51 def method_missing(type, *helpers, **, &block) # If no entity class yet, create one @entity_class ||= Class.new(Entity) # Call the method on the entity class @entity_class.public_send(type, *helpers, **, &block) end |
Instance Method Details
#collection_of_attributes ⇒ Collection
Returns collection of attributes from the entity class
40 41 42 43 44 |
# File 'lib/treaty/request/factory.rb', line 40 def collection_of_attributes return Treaty::Attribute::Collection.new if @entity_class.nil? @entity_class.collection_of_attributes end |
#respond_to_missing?(name) ⇒ Boolean
59 60 61 |
# File 'lib/treaty/request/factory.rb', line 59 def respond_to_missing?(name, *) super end |
#use_entity(entity_class) ⇒ void
This method returns an undefined value.
Uses a provided Entity class
32 33 34 35 |
# File 'lib/treaty/request/factory.rb', line 32 def use_entity(entity_class) validate_entity_class!(entity_class) @entity_class = entity_class end |