Class: GeneSystem::StepCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gene_system/step_collection.rb

Overview

Filterable and mergable collection of steps

Constant Summary collapse

DEFAULT_QUERY =

Default filter will match each step

lambda do |_step, _query = {}|
  true
end
STEP_INCLUDE_ANY_TAG =

Function that returns true if step has any of the provided tags

Returns:

  • (Boolean)
lambda do |step, query = {}|
  raise 'query must include tags' unless query[:tags].is_a?(Array)

  results = query[:tags].map do |tag|
    step.tags.include?(tag)
  end

  results.any?(true)
end
STEP_EXCLUDE_ANY_TAG =

Function that returns true if step does not have any of the provided tags

Returns:

  • (Boolean)
lambda do |step, query = {}|
  raise 'query must include tags' unless query[:tags].is_a?(Array)

  results = query[:tags].map do |tag|
    step.tags.include?(tag)
  end

  results.all?(false)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps = []) ⇒ StepCollection

Returns a new instance of StepCollection.



55
56
57
# File 'lib/gene_system/step_collection.rb', line 55

def initialize(steps = [])
  @steps = steps
end

Instance Attribute Details

#stepsObject (readonly)

Returns the value of attribute steps.



6
7
8
# File 'lib/gene_system/step_collection.rb', line 6

def steps
  @steps
end

Instance Method Details

#filter(matcher = DEFAULT_FILTER, query = {}) ⇒ Object

Filters steps by given matcher function

The matcher function must take a step as an argument and return true or false as to whether it should be in the returned collection.

Parameters:

  • matcher (Proc) (defaults to: DEFAULT_FILTER)
  • query (Hash) (defaults to: {})


73
74
75
76
77
78
79
# File 'lib/gene_system/step_collection.rb', line 73

def filter(matcher = DEFAULT_FILTER, query = {})
  GeneSystem::StepCollection.new(
    @steps.select do |step|
      matcher.call(step, query)
    end
  )
end

#merge(collection) ⇒ GeneSystem::StepCollection

Creates a collection which is a union of the given collection and this collection.

Parameters:

Returns:



90
91
92
93
94
# File 'lib/gene_system/step_collection.rb', line 90

def merge(collection)
  GeneSystem::StepCollection.new(
    @steps + collection.steps
  )
end