Class: Plines::Job

Inherits:
Struct
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/plines/job.rb

Overview

An instance of a Step: a step class paired with some data for the job.

Constant Summary collapse

RemoveNonexistentDependencyError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, data) {|_self| ... } ⇒ Job

Returns a new instance of Job.

Yields:

  • (_self)

Yield Parameters:

  • _self (Plines::Job)

    the object that the method was called on



11
12
13
14
15
16
# File 'lib/plines/job.rb', line 11

def initialize(klass, data)
  super(klass, IndifferentHash.from(data))
  @dependencies = Set.new
  @dependents = Set.new
  yield self if block_given?
end

Instance Attribute Details

#dataObject

Returns the value of attribute data

Returns:

  • (Object)

    the current value of data



6
7
8
# File 'lib/plines/job.rb', line 6

def data
  @data
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



8
9
10
# File 'lib/plines/job.rb', line 8

def dependencies
  @dependencies
end

#dependentsObject (readonly)

Returns the value of attribute dependents.



8
9
10
# File 'lib/plines/job.rb', line 8

def dependents
  @dependents
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



6
7
8
# File 'lib/plines/job.rb', line 6

def klass
  @klass
end

Class Method Details

.accumulate_instancesObject

Ensures all “identical” instances (same klass and data) created within the block are in fact the same object. This is important when constructing the dependency graph, so that all the dependency/dependee relationships point to the right objects (rather than duplicate objects).



55
56
57
58
59
60
61
62
63
64
# File 'lib/plines/job.rb', line 55

def accumulate_instances
  self.repository = Hash.new { |h,k| h[k] = new(*k) }

  begin
    yield
    return repository.values
  ensure
    self.repository = nil
  end
end

.build(*args, &block) ⇒ Object



66
67
68
# File 'lib/plines/job.rb', line 66

def build(*args, &block)
  repository[args, &block]
end

Instance Method Details

#add_dependencies_for(batch_data) ⇒ Object



31
32
33
34
35
# File 'lib/plines/job.rb', line 31

def add_dependencies_for(batch_data)
  klass.dependencies_for(self, batch_data).each do |job|
    add_dependency(job)
  end
end

#add_dependency(step) ⇒ Object



18
19
20
21
# File 'lib/plines/job.rb', line 18

def add_dependency(step)
  dependencies << step
  step.dependents << self
end

#external_dependenciesObject



37
38
39
# File 'lib/plines/job.rb', line 37

def external_dependencies
  klass.external_dependencies_for(data)
end

#remove_dependency(step) ⇒ Object



24
25
26
27
28
29
# File 'lib/plines/job.rb', line 24

def remove_dependency(step)
  unless dependencies.delete?(step) && step.dependents.delete?(self)
    raise RemoveNonexistentDependencyError,
      "Attempted to remove nonexistent dependency #{step} from #{self}"
  end
end