Class: KBuilder::Watch::Execute

Inherits:
Object
  • Object
show all
Defined in:
lib/k_builder/watch/execute.rb

Overview

Execute based on CLI options

Constant Summary collapse

DEFAULT_ACTIONS =
{
  help: KBuilder::Watch::ActionDisplayHelp,
  debug: KBuilder::Watch::ActionDisplayDebug,
  new_builder: KBuilder::Watch::ActionNewBuilder,
  watcher: KBuilder::Watch::ActionWatcher
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, actions: DEFAULT_ACTIONS) ⇒ Execute

Execute one or more actions based on supplied options

You can use dependency injection to provide your actions



28
29
30
31
# File 'lib/k_builder/watch/execute.rb', line 28

def initialize(options, actions: DEFAULT_ACTIONS)
  @actions = actions
  @options = options
end

Instance Attribute Details

#actionsObject

watcher = KBuilder::Watch::Watcher.new(cli.watch_path) watcher.start



22
23
24
# File 'lib/k_builder/watch/execute.rb', line 22

def actions
  @actions
end

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/k_builder/watch/execute.rb', line 23

def options
  @options
end

Instance Method Details

#runObject



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

def run
  if options.help?
    run_action(:help)

    exit
  end

  if options.debug?
    run_action(:debug)

    exit
  end

  if options.new?
    run_action(:new_builder)

    # Originally I allowed new to proceed straight into watcher, but it is problematic to mix
    # builder creation in with builder watching, they are two different tasks that should be
    # run separately
    exit
  end

  run_action(:watcher)
end

#run_action(action_key) ⇒ Object



33
34
35
36
37
38
# File 'lib/k_builder/watch/execute.rb', line 33

def run_action(action_key)
  raise KBuilder::Watch::Error, "Unknown action: #{action_key}" unless @actions.key?(action_key)

  action = actions[action_key].new(options)
  action.run
end