Class: Treaty::Request::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/treaty/request/factory.rb

Overview

Factory for creating request definitions.

Supports two modes:

  1. Block mode: Creates an anonymous Request::Entity class with the block

  2. 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

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, **options, &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, **options, &block)
end

Instance Method Details

#collection_of_attributesCollection

Returns collection of attributes from the entity class

Returns:

  • (Collection)

    Collection of attributes



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

Returns:

  • (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

Parameters:

  • entity_class (Class)

    Entity class to use

Raises:



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