Class: Treaty::Action::Executor::Inventory
- Inherits:
-
Object
- Object
- Treaty::Action::Executor::Inventory
- Defined in:
- lib/treaty/action/executor/inventory.rb
Overview
Lazy-evaluating proxy for accessing inventory items.
Purpose
Provides lazy evaluation and caching of inventory items defined in controllers. Acts as a proxy that evaluates inventory items only when accessed, avoiding unnecessary computation for unused items.
Usage
Created internally by:
- Versions::Execution::Request (when executing treaty versions)
Passed to:
- Service classes (as
inventoryparameter) - Proc executors (as
inventory:keyword argument)
Access Patterns
Method-based access (lazy, cached):
inventory.current_user # Evaluates and caches on first call
inventory.current_user # Returns cached value
Hash-based access (evaluates all items):
inventory.to_h # => { current_user: <User>, posts: [...] }
Caching
Once an item is evaluated via method access, the result is cached
in @evaluated_cache. Subsequent calls return the cached value
without re-evaluation.
Example
# In controller:
treaty :index do
provide :current_user
provide :posts, from: :load_posts
end
# In service:
class Posts::IndexService
def call(inventory:, params:)
user = inventory.current_user # Lazy evaluation
posts = inventory.posts # Lazy evaluation
# ...
end
end
Instance Method Summary collapse
-
#initialize(inventory, context) ⇒ Inventory
constructor
Creates a new inventory executor instance.
-
#inspect ⇒ String
Returns human-readable representation for debugging.
-
#method_missing(method_name, *_args) ⇒ Object
Handles dynamic method calls to access inventory items.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Checks if method corresponds to an inventory item.
-
#to_h ⇒ Hash{Symbol => Object}
Evaluates all inventory items and returns as hash.
Constructor Details
#initialize(inventory, context) ⇒ Inventory
Creates a new inventory executor instance
59 60 61 62 63 |
# File 'lib/treaty/action/executor/inventory.rb', line 59 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
Handles dynamic method calls to access inventory items
Looks up the inventory item by method name, evaluates it with the controller context, and caches the result.
73 74 75 76 77 78 79 |
# File 'lib/treaty/action/executor/inventory.rb', line 73 def method_missing(method_name, *_args) return @evaluated_cache[method_name] if @evaluated_cache.key?(method_name) item = find_inventory_item(method_name) @evaluated_cache[method_name] = item.evaluate(@context) end |
Instance Method Details
#inspect ⇒ String
Returns human-readable representation for debugging
107 108 109 110 |
# File 'lib/treaty/action/executor/inventory.rb', line 107 def inspect items = @inventory&.names || [] "#<Treaty::Action::Executor::Inventory items=#{items.inspect}>" end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Checks if method corresponds to an inventory item
86 87 88 89 90 |
# File 'lib/treaty/action/executor/inventory.rb', line 86 def respond_to_missing?(method_name, include_private = false) return false if @inventory.nil? @inventory.names.include?(method_name) || super end |
#to_h ⇒ Hash{Symbol => Object}
Evaluates all inventory items and returns as hash
Unlike method-based access, this evaluates ALL items at once. Useful when you need all inventory data as a hash.
98 99 100 101 102 |
# File 'lib/treaty/action/executor/inventory.rb', line 98 def to_h return {} if @inventory.nil? @inventory.evaluate(@context) end |