Class: Treaty::Response::Factory

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

Overview

Factory for creating response definitions.

Supports two modes:

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

  2. Entity mode: Uses a provided Entity class directly

## Block Mode

“‘ruby response 200 do

object :post do
  string :id
end

end “‘

## Entity Mode

“‘ruby response 200, PostResponseEntity “`

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status) ⇒ Factory



29
30
31
# File 'lib/treaty/response/factory.rb', line 29

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) ⇒ 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 Response::Entity class on first use.



57
58
59
60
61
62
63
# File 'lib/treaty/response/factory.rb', line 57

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 Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



27
28
29
# File 'lib/treaty/response/factory.rb', line 27

def status
  @status
end

Instance Method Details

#collection_of_attributesCollection

Returns collection of attributes from the entity class



46
47
48
49
50
# File 'lib/treaty/response/factory.rb', line 46

def collection_of_attributes
  return Treaty::Attribute::Collection.new if @entity_class.nil?

  @entity_class.collection_of_attributes
end

#respond_to_missing?(name) ⇒ Boolean



65
66
67
# File 'lib/treaty/response/factory.rb', line 65

def respond_to_missing?(name, *)
  super
end

#use_entity(entity_class) ⇒ void

This method returns an undefined value.

Uses a provided Entity class

Raises:



38
39
40
41
# File 'lib/treaty/response/factory.rb', line 38

def use_entity(entity_class)
  validate_entity_class!(entity_class)
  @entity_class = entity_class
end