Class: Treaty::Action::Request::Factory

Inherits:
Object
  • Object
show all
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 ... end is 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

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.

Parameters:

  • type (Symbol)

    Attribute type (method name)

  • helpers (Array)

    Helper symbols and arguments

  • options (Hash)

    Attribute options

  • block (Proc)

    Block for nested attributes



82
83
84
85
86
# File 'lib/treaty/action/request/factory.rb', line 82

def method_missing(type, *helpers, **options, &block)
  @entity_class ||= Class.new(Entity)

  @entity_class.public_send(type, *helpers, **options, &block)
end

Instance Method Details

#collection_of_attributesTreaty::Entity::Attribute::Collection

Returns the collection of defined attributes

Returns:



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

Parameters:

  • name (Symbol)

    Method name

Returns:

  • (Boolean)


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.

Parameters:

  • entity_class (Class)

    Must be Treaty::Entity::Base subclass

Raises:



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