Class: Servactory::Actions::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/servactory/actions/collection.rb

Overview

Mutable collection manager for Action objects.

## Purpose

Stores Action objects and provides query methods to retrieve actions sorted by position. Wraps a Set for unique action storage with position-based ordering capabilities.

## Usage

“‘ruby actions = Servactory::Actions::Collection.new actions << Action.new(:first, position: 1) actions << Action.new(:second, position: 2)

actions.sorted_by_position # => Collection with sorted actions actions.empty? # => false actions.size # => 2 “‘

## Integration

Used by Servactory::Actions::Stages::Stage to store actions within a stage, and by Servactory::Actions::Tools::Runner to execute actions in the correct order.

Instance Method Summary collapse

Constructor Details

#initialize(collection = Set.new) ⇒ Collection

Creates a new actions collection.

Parameters:

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

    Initial collection of actions (default: empty Set)



58
59
60
# File 'lib/servactory/actions/collection.rb', line 58

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

Instance Method Details

#<<(action) ⇒ Set

Adds an action to the collection.

Parameters:

  • action (Action)

    The action to add

Returns:

  • (Set)

    The updated collection



53
# File 'lib/servactory/actions/collection.rb', line 53

def_delegators :@collection, :<<, :each, :to_h, :sort_by, :size, :empty?

#each {|Action| ... } ⇒ Object

Iterates over all actions in the collection.

Yields:

  • (Action)

    Each action in the collection



53
# File 'lib/servactory/actions/collection.rb', line 53

def_delegators :@collection, :<<, :each, :to_h, :sort_by, :size, :empty?

#empty?Boolean

Checks if the collection is empty.

Returns:

  • (Boolean)

    true if no actions registered



53
# File 'lib/servactory/actions/collection.rb', line 53

def_delegators :@collection, :<<, :each, :to_h, :sort_by, :size, :empty?

#sizeInteger

Returns the number of actions in the collection.

Returns:

  • (Integer)

    Number of actions



53
# File 'lib/servactory/actions/collection.rb', line 53

def_delegators :@collection, :<<, :each, :to_h, :sort_by, :size, :empty?

#sort_by {|Action| ... } ⇒ Array<Action>

Sorts actions by the given block.

Yields:

  • (Action)

    Each action for comparison

Returns:

  • (Array<Action>)

    Sorted array of actions



53
# File 'lib/servactory/actions/collection.rb', line 53

def_delegators :@collection, :<<, :each, :to_h, :sort_by, :size, :empty?

#sorted_by_positionCollection

Returns a new collection with actions sorted by position.

Examples:

sorted = actions.sorted_by_position
sorted.each { |action| puts action.position }

Returns:

  • (Collection)

    New collection with actions in position order



69
70
71
# File 'lib/servactory/actions/collection.rb', line 69

def sorted_by_position
  Collection.new(sort_by(&:position))
end

#to_hHash

Converts collection to a hash.

Returns:

  • (Hash)

    Hash representation



53
# File 'lib/servactory/actions/collection.rb', line 53

def_delegators :@collection, :<<, :each, :to_h, :sort_by, :size, :empty?