Class: Treaty::Executor::Inventory
- Inherits:
-
Object
- Object
- Treaty::Executor::Inventory
- Defined in:
- lib/treaty/executor/inventory.rb
Overview
Inventory wrapper that provides method-based access to inventory items.
## Purpose
Wraps inventory collection and controller context, providing lazy evaluation of inventory items through method calls. This encapsulates all inventory logic within the class and provides a clean API for services.
## Usage
“‘ruby # Created internally by Treaty inventory = Treaty::Executor::Inventory.new(inventory_collection, controller_context)
# Access via method calls - evaluates lazily inventory.posts # => Calls controller method or evaluates proc inventory.current_user # => Returns evaluated value
# Raises exception for missing items inventory.missing_item # => Treaty::Exceptions::Inventory
# Convert to hash - evaluates all items inventory.to_h # => { posts: […], current_user: … } “‘
## Architecture
The class encapsulates:
-
Inventory collection (from controller’s treaty block)
-
Controller context (for method calls and proc evaluation)
-
Lazy evaluation logic (items evaluated on access)
## Error Handling
If an inventory item is not found, raises ‘Treaty::Exceptions::Inventory` with an I18n-translated error message listing available items.
Instance Method Summary collapse
-
#initialize(inventory, context) ⇒ Inventory
constructor
Creates a new inventory instance.
-
#inspect ⇒ String
Returns string representation.
-
#method_missing(method_name, *_args) ⇒ Object
Provides method-based access to inventory items with lazy evaluation.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Checks if inventory responds to a method.
-
#to_h ⇒ Hash
Converts inventory to hash, evaluating all items.
Constructor Details
#initialize(inventory, context) ⇒ Inventory
Creates a new inventory instance
46 47 48 49 50 |
# File 'lib/treaty/executor/inventory.rb', line 46 def initialize(inventory, context) @inventory = inventory @context = context @evaluated_cache = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *_args) ⇒ Object
Provides method-based access to inventory items with lazy evaluation
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/treaty/executor/inventory.rb', line 58 def method_missing(method_name, *_args) # Check cache first return @evaluated_cache[method_name] if @evaluated_cache.key?(method_name) # Find inventory item item = find_inventory_item(method_name) # Evaluate and cache @evaluated_cache[method_name] = item.evaluate(@context) end |
Instance Method Details
#inspect ⇒ String
Returns string representation
92 93 94 95 |
# File 'lib/treaty/executor/inventory.rb', line 92 def inspect items = @inventory&.names || [] "#<Treaty::Executor::Inventory items=#{items.inspect}>" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Checks if inventory responds to a method
74 75 76 77 78 |
# File 'lib/treaty/executor/inventory.rb', line 74 def respond_to_missing?(method_name, include_private = false) return false if @inventory.nil? @inventory.names.include?(method_name) || super end |
#to_h ⇒ Hash
Converts inventory to hash, evaluating all items
83 84 85 86 87 |
# File 'lib/treaty/executor/inventory.rb', line 83 def to_h return {} if @inventory.nil? @inventory.evaluate(@context) end |