Class: Treaty::Action::Inventory::Collection
- Inherits:
-
Object
- Object
- Treaty::Action::Inventory::Collection
- 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 itemeach_with_object- Iteration with accumulatorfind- Access by conditionempty?- Size check
Custom methods:
exists?- Returns true if collection is not emptynames- Returns array of inventory item namesevaluate- 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
-
#evaluate(context) ⇒ Hash{Symbol => Object}
Evaluates all inventory items with controller context.
-
#exists? ⇒ Boolean
Checks if collection has any elements.
-
#initialize(collection = Set.new) ⇒ Collection
constructor
Creates a new collection instance.
-
#names ⇒ Array<Symbol>
Returns array of all inventory item names.
Constructor Details
#initialize(collection = Set.new) ⇒ Collection
Creates a new collection instance
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
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
54 55 56 |
# File 'lib/treaty/action/inventory/collection.rb', line 54 def exists? !empty? end |
#names ⇒ Array<Symbol>
Returns array of all 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 |