Class: Treaty::Action::Inventory::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/treaty/action/inventory/collection.rb

Overview

Collection wrapper for sets of inventory items.

Purpose

Provides a unified interface for working with collections of inventory items. Uses Ruby Set internally for uniqueness but exposes Array-like interface.

Usage

Used internally by:

  • Inventory::Factory (to store inventory items)
  • Executor::Inventory (to evaluate and access items)

Methods

Delegates common collection methods to internal Set:

  • << - Add inventory item
  • each_with_object - Iteration with accumulator
  • find - Access by condition
  • empty? - Size check

Custom methods:

  • exists? - Returns true if collection is not empty
  • names - Returns array of inventory item names
  • evaluate - Evaluates all items with controller context

Example

collection = Collection.new
collection << Inventory.new(name: :current_user, source: :current_user)
collection << Inventory.new(name: :posts, source: :load_posts)
collection.exists?  # => true
collection.names    # => [:current_user, :posts]

Instance Method Summary collapse

Constructor Details

#initialize(collection = Set.new) ⇒ Collection

Creates a new collection instance

Parameters:

  • collection (Set) (defaults to: Set.new)

    Initial collection (default: empty Set)



47
48
49
# File 'lib/treaty/action/inventory/collection.rb', line 47

def initialize(collection = Set.new)
  @collection = collection
end

Instance Method Details

#evaluate(context) ⇒ Hash{Symbol => Object}

Evaluates all inventory items with controller context

Parameters:

  • context (Object)

    Controller context for evaluation

Returns:

  • (Hash{Symbol => Object})

    Hash of evaluated inventory values



69
70
71
72
73
# File 'lib/treaty/action/inventory/collection.rb', line 69

def evaluate(context)
  @collection.each_with_object({}) do |inventory_item, hash|
    hash[inventory_item.name] = inventory_item.evaluate(context)
  end
end

#exists?Boolean

Checks if collection has any elements

Returns:

  • (Boolean)

    True if collection is not empty



54
55
56
# File 'lib/treaty/action/inventory/collection.rb', line 54

def exists?
  !empty?
end

#namesArray<Symbol>

Returns array of all inventory item names

Returns:

  • (Array<Symbol>)

    Array of inventory item names



61
62
63
# File 'lib/treaty/action/inventory/collection.rb', line 61

def names
  @collection.each_with_object([]) { |item, names| names << item.name }
end