Class: Treaty::Action::Inventory::Factory
- Inherits:
-
Object
- Object
- Treaty::Action::Inventory::Factory
- 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: -> { } # 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
-
#collection ⇒ Treaty::Action::Inventory::Collection
readonly
Collection of inventory items.
Instance Method Summary collapse
-
#initialize(action_name) ⇒ Factory
constructor
Creates a new factory instance.
-
#method_missing(method_name, *args, **options, &_block) ⇒ Treaty::Action::Inventory::Collection
Handles DSL method calls (only
provideis supported). -
#respond_to_missing?(method_name) ⇒ Boolean
Checks if method should be handled by method_missing.
Constructor Details
#initialize(action_name) ⇒ Factory
Creates a new factory instance
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.
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, **, &_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 .key?(:from) .fetch(:from) else inventory_name end @collection << Inventory.new(name: inventory_name, source:) @collection end |
Instance Attribute Details
#collection ⇒ Treaty::Action::Inventory::Collection (readonly)
Returns Collection of inventory items.
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
102 103 104 |
# File 'lib/treaty/action/inventory/factory.rb', line 102 def respond_to_missing?(method_name, *) method_name == :provide || super end |