Class: Treaty::Action::Inventory::Factory

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

Overview

Factory for building inventory collections from controller DSL.

Purpose

Provides the provide DSL method used in controller treaty blocks to define inventory items. Inventory allows controllers to pass data (current_user, loaded records, etc.) to services.

Usage

Created by:

  • Controller::DSL (when treaty block is evaluated)

DSL Method

The only supported method is provide:

treaty :index do
provide :current_user                    # Shorthand: same name as method
provide :posts, from: :load_posts        # Symbol source
provide :meta, from: -> { build_meta }   # Proc source
provide :limit, from: 10                 # Direct value
end

Source Types

Type Description Evaluation
Symbol Controller method name context.send(source)
Proc Lambda/block context.instance_exec(&source)
Other Direct value Returned as-is

Example

factory = Factory.new(:index)
factory.provide :current_user
factory.provide :posts, from: :load_posts
factory.collection  # => Collection with 2 items

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action_name) ⇒ Factory

Creates a new factory instance

Parameters:

  • action_name (Symbol)

    Controller action name (for error messages)



51
52
53
54
# File 'lib/treaty/action/inventory/factory.rb', line 51

def initialize(action_name)
  @action_name = action_name
  @collection = Collection.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **options, &_block) ⇒ Treaty::Action::Inventory::Collection

Handles DSL method calls (only provide is supported)

Creates an Inventory item and adds it to the collection. Validates that only provide method is called and name is a Symbol.

Parameters:

  • method_name (Symbol)

    Method name (must be :provide)

  • args (Array)

    Arguments (first must be inventory name as Symbol)

  • options (Hash)

    Options (:from for source)

Returns:

Raises:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/treaty/action/inventory/factory.rb', line 67

def method_missing(method_name, *args, **options, &_block) # rubocop:disable Metrics/MethodLength
  unless method_name == :provide
    raise Treaty::Exceptions::Inventory,
          I18n.t(
            "treaty.inventory.unknown_method",
            method: method_name,
            action: @action_name
          )
  end

  inventory_name = args.first

  unless inventory_name.is_a?(Symbol)
    raise Treaty::Exceptions::Inventory,
          I18n.t(
            "treaty.inventory.name_must_be_symbol",
            name: inventory_name.inspect
          )
  end

  source = if options.key?(:from)
             options.fetch(:from)
           else
             inventory_name
           end

  @collection << Inventory.new(name: inventory_name, source:)

  @collection
end

Instance Attribute Details

#collectionTreaty::Action::Inventory::Collection (readonly)

Returns Collection of inventory items.

Returns:



46
47
48
# File 'lib/treaty/action/inventory/factory.rb', line 46

def collection
  @collection
end

Instance Method Details

#respond_to_missing?(method_name) ⇒ Boolean

Checks if method should be handled by method_missing

Parameters:

  • method_name (Symbol)

    Method name

Returns:

  • (Boolean)

    True only for :provide



102
103
104
# File 'lib/treaty/action/inventory/factory.rb', line 102

def respond_to_missing?(method_name, *)
  method_name == :provide || super
end