Module: Dronejob::Modules::Phases

Extended by:
ActiveSupport::Concern
Includes:
ActiveSupport::Callbacks
Included in:
Base
Defined in:
lib/dronejob/modules/phases.rb

Instance Method Summary collapse

Instance Method Details

#completed_phase?(phase) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
# File 'lib/dronejob/modules/phases.rb', line 73

def completed_phase?(phase)
  if self.class.stateful?
    @commits.include?(phase.to_s)
  else
    false
  end
end

#include_helper(helper) ⇒ Object



67
68
69
70
71
# File 'lib/dronejob/modules/phases.rb', line 67

def include_helper(helper)
  helper_path = "helpers/#{helper}"
  require "./app/#{helper_path}"
  self.class.send(:include, helper_path.camelcase.constantize)
end

#log_phaseObject



24
# File 'lib/dronejob/modules/phases.rb', line 24

def log_phase; end

#prev_phase(phase) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/dronejob/modules/phases.rb', line 94

def prev_phase(phase)
  phases = self.class.phases.keys
  index = phases.index(phase.to_sym)
  return nil if index.nil?
  return "start" if index == 0

  phases[index - 1]
end

#run_phasesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/dronejob/modules/phases.rb', line 26

def run_phases
  if self.respond_to?(:before)
    phases = before(self.class.phases)
  else
    phases = self.class.phases
  end

  phases.each do |phase, phase_config|
    @phase = phase
    @phase_config = phase_config

    # initialize phase
    fail "Phase not found: '#{phase}'" unless self.respond_to?(phase)

    run_callbacks :phase do
      if completed_phase?(phase) and !@phase_config[:always_run]
        info("already completed")
      elsif skip_phase?(phase, phase_config)
        info("skipping...")
      else
        info("running phase #{phase}")
        # Require libraries
        @phase_config[:require]&.each do |library|
          require library
        end

        # Include helpers
        @phase_config[:helpers]&.each do |helper|
          include_helper(helper)
        end

        # OLD CODE FROM CORE
        public_send(phase)
      end
    end
  end
  @phase = "complete"
  @dronejob_completed = true
  git_commit("dronejob_completed") if self.class.stateful?
end

#skip_phase?(phase, phase_config) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dronejob/modules/phases.rb', line 81

def skip_phase?(phase, phase_config)
  if phase_config[:if]
    if phase_config[:if].class == String or phase_config[:if].class == Symbol
      return true unless param(phase_config[:if])
    elsif phase_config[:if].class == Array
      return true unless phase_config[:if].all? { |p| param(p) == true }
    elsif phase_config[:if].class == Proc
      return true unless instance_eval(&phase_config[:if])
    end
  end
  param(:skip)&.include?(phase.to_s)
end