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: {}, track_type: VALID_TRACK_TYPES.first, track_from: nil, &block) ⇒ Track

Returns a new instance of Track.

Raises:

  • (UnknownTrackType)


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

def initialize(name: (raise Errors::NoTrackProvided), track_options: {}, track_type: VALID_TRACK_TYPES.first, track_from: nil, &block)
  raise UnknownTrackType, 'Please provide a valid track type' unless VALID_TRACK_TYPES.include?(track_type)
  
  @name          = name
  @track_from    = track_from
  @block         = block
  @track_options = track_options
  @type          = track_type
  @state         = 'pending'
  @error         = nil
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

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/clomp/track.rb', line 36

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 failure? && @block
  
  self

rescue => e
  @error = e.message
  
  self
end

#track?Boolean

Returns:

  • (Boolean)


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

def track?
  @type == :track
end

#typeObject



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

def type
  @type
end