Class: Treaty::Executor::Inventory

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

Constructor Details

#initialize(inventory, context) ⇒ Inventory

Creates a new inventory instance

Parameters:



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

Parameters:

  • method_name (Symbol)

    The inventory item name

  • _args (Array)

    Arguments (not used, for compatibility)

Returns:

  • (Object)

    The evaluated inventory item value

Raises:



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

#inspectString

Returns string representation

Returns:

  • (String)

    Inventory description



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

Parameters:

  • method_name (Symbol)

    The method name to check

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods

Returns:

  • (Boolean)

    True if inventory has the item



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_hHash

Converts inventory to hash, evaluating all items

Returns:

  • (Hash)

    Hash of all evaluated inventory 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