Class: Treaty::Inventory::Factory
- Inherits:
-
Object
- Object
- Treaty::Inventory::Factory
- Defined in:
- lib/treaty/inventory/factory.rb
Overview
Factory for building inventory collections via DSL.
## Purpose
Provides the ‘provide` DSL method for defining inventory items in controller blocks. Captures calls like `provide :posts, from: :load_posts` and builds a collection.
## Usage
Used internally by Controller::DSL when processing treaty blocks:
“‘ruby treaty :index do
provide :posts, from: :load_posts # Explicit source
provide :meta, from: -> { { count: 10 } } # Lambda source
provide :current_user # Shorthand: uses :current_user as source
end “‘
## Valid Sources
-
Symbol: Method name to call on controller (e.g., ‘:load_posts`)
-
Proc/Lambda: Callable object (e.g., ‘-> { Post.all }`)
-
Direct value: String, number, or any other value (e.g., ‘“Welcome”`)
-
Omitted: Uses inventory name as source (e.g., ‘provide :posts` → `from: :posts`)
## Invalid Sources
-
Direct method calls without symbol/proc (e.g., ‘from: load_posts`)
-
Explicit nil values (e.g., ‘from: nil`)
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
Instance Method Summary collapse
-
#initialize(action_name) ⇒ Factory
constructor
A new instance of Factory.
-
#method_missing(method_name, *args, **options, &_block) ⇒ Collection
Handles the ‘provide` DSL method via method_missing.
- #respond_to_missing?(method_name) ⇒ Boolean
Constructor Details
#initialize(action_name) ⇒ Factory
Returns a new instance of Factory.
38 39 40 41 |
# File 'lib/treaty/inventory/factory.rb', line 38 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) ⇒ Collection
Handles the ‘provide` DSL method via method_missing
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/treaty/inventory/factory.rb', line 50 def method_missing(method_name, *args, **, &_block) # rubocop:disable Metrics/MethodLength # Only handle 'provide' method unless method_name == :provide raise Treaty::Exceptions::Inventory, I18n.t( "treaty.inventory.unknown_method", method: method_name, action: @action_name ) end # Extract inventory name 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 # Extract source from options (default to inventory name if not provided) source = if .key?(:from) .fetch(:from) else inventory_name end # Create and add inventory item to collection @collection << Inventory.new(name: inventory_name, source:) # Return collection for potential chaining @collection end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
36 37 38 |
# File 'lib/treaty/inventory/factory.rb', line 36 def collection @collection end |
Instance Method Details
#respond_to_missing?(method_name) ⇒ Boolean
86 87 88 |
# File 'lib/treaty/inventory/factory.rb', line 86 def respond_to_missing?(method_name, *) method_name == :provide || super end |