Class: Clomp::Track

Inherits:
Object
  • Object
show all
Includes:
CommonStates
Defined in:
lib/clomp/track.rb

Constant Summary collapse

VALID_TRACK_TYPES =
%I(track failed_track finally catch)

Constants included from CommonStates

CommonStates::FAILURE, CommonStates::PENDING, CommonStates::SUCCESS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonStates

#exception_raised?, #failure?, #mark_as_failure!, #mark_as_success!, #pending?, #success?

Constructor Details

#initialize(name: (raise Errors::NoTrackProvided), track_options: {}, right: true, track_from: nil, &block) ⇒ Track

Returns a new instance of Track.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/clomp/track.rb', line 10

def initialize(name: (raise Errors::NoTrackProvided), track_options: {}, right: true, track_from: nil, &block)
  @name          = name
  @track_from    = track_from
  @block         = block
  @track_options = track_options
  @state         = 'pending'
  @error         = nil
  @right         = right
  @left          = !@right
  @executed      = false
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



5
6
7
# File 'lib/clomp/track.rb', line 5

def block
  @block
end

#errorObject (readonly)

Returns the value of attribute error.



5
6
7
# File 'lib/clomp/track.rb', line 5

def error
  @error
end

#executedObject

Returns the value of attribute executed.



6
7
8
# File 'lib/clomp/track.rb', line 6

def executed
  @executed
end

#leftObject (readonly)

Returns the value of attribute left.



5
6
7
# File 'lib/clomp/track.rb', line 5

def left
  @left
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/clomp/track.rb', line 5

def name
  @name
end

#rightObject (readonly)

Returns the value of attribute right.



5
6
7
# File 'lib/clomp/track.rb', line 5

def right
  @right
end

#stateObject (readonly)

Returns the value of attribute state.



5
6
7
# File 'lib/clomp/track.rb', line 5

def state
  @state
end

#track_fromObject (readonly)

Returns the value of attribute track_from.



5
6
7
# File 'lib/clomp/track.rb', line 5

def track_from
  @track_from
end

#track_optionsObject (readonly)

Returns the value of attribute track_options.



5
6
7
# File 'lib/clomp/track.rb', line 5

def track_options
  @track_options
end

Instance Method Details

#exec!(object, options) ⇒ Object

Track#exec! executes the steps defined in the operation class



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/clomp/track.rb', line 41

def exec!(object, options)
  mark_as_failure! # going to execute! set to failure initially
  
  if object.method(name.to_sym).arity > 1
    mark_as_success! if object.public_send(name.to_sym, options, **options) != false
  else
    mark_as_success! if object.public_send(name.to_sym, options) != false
  end
  
  @block.(options) if success? && @block

  self.executed = true

  self

rescue => e
  @error = e.message

  self.executed = false
  
  mark_as_failure!
  
  self
end

#executed?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/clomp/track.rb', line 22

def executed?
  @executed == true
end

#left_track?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/clomp/track.rb', line 32

def left_track?
  !right_track?
end

#right_track?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/clomp/track.rb', line 36

def right_track?
  @right
end

#typeObject Also known as: track?



26
27
28
# File 'lib/clomp/track.rb', line 26

def type
  @right ? :right_track : :left_track
end