Class: Treaty::Action::Executor::Inventory

Inherits:
Object
  • Object
show all
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 inventory parameter)
  • 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

Constructor Details

#initialize(inventory, context) ⇒ Inventory

Creates a new inventory executor instance

Parameters:

  • inventory (Treaty::Action::Inventory::Collection, nil)

    Collection of inventory items

  • context (Object)

    Controller context for evaluating items (typically ActionController 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.

Parameters:

  • method_name (Symbol)

    Name of the inventory item to access

Returns:

  • (Object)

    Evaluated value from the inventory item

Raises:



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

#inspectString

Returns human-readable representation for debugging

Returns:

  • (String)

    Inspection string with available item names



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

Parameters:

  • method_name (Symbol)

    Method name to check

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods

Returns:

  • (Boolean)

    True if inventory contains item with given name



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_hHash{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.

Returns:

  • (Hash{Symbol => Object})

    Hash of all evaluated inventory values



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