Class: EacCli::Runner::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/eac_cli/runner/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(runner, *context_args) ⇒ Context

Returns a new instance of Context.



10
11
12
13
14
15
16
# File 'lib/eac_cli/runner/context.rb', line 10

def initialize(runner, *context_args)
  options = context_args.extract_options!
  @argv = (context_args[0] || options.delete(:argv) || ARGV).dup.freeze
  @parent = context_args[1] || options.delete(:parent)
  @program_name = options.delete(:program_name)
  @runner = runner
end

Instance Attribute Details

#argvObject (readonly)

Returns the value of attribute argv.



8
9
10
# File 'lib/eac_cli/runner/context.rb', line 8

def argv
  @argv
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/eac_cli/runner/context.rb', line 8

def parent
  @parent
end

#program_nameObject (readonly)

Returns the value of attribute program_name.



8
9
10
# File 'lib/eac_cli/runner/context.rb', line 8

def program_name
  @program_name
end

#runnerObject (readonly)

Returns the value of attribute runner.



8
9
10
# File 'lib/eac_cli/runner/context.rb', line 8

def runner
  @runner
end

Instance Method Details

#call(method_name, *args, &block) ⇒ Object

Call a method in the runner or in one of it ancestors.

Raises:

  • (::NameError)


19
20
21
22
23
24
# File 'lib/eac_cli/runner/context.rb', line 19

def call(method_name, *args, &block)
  return runner.send(method_name, *args, &block) if runner.respond_to?(method_name)
  return parent_call(method_name, *args, &block) if parent_respond_to?(method_name)

  raise ::NameError, "No method \"#{method_name}\" found in #{runner} or in its ancestors"
end

#parent_call(method_name, *args, &block) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/eac_cli/runner/context.rb', line 36

def parent_call(method_name, *args, &block)
  if parent.respond_to?(:runner_context)
    return parent.runner_context.call(method_name, *args, &block)
  end

  raise "Parent #{parent} do not respond to .context or .runner_context (Runner: #{runner})"
end

#parent_respond_to?(method_name) ⇒ Boolean

Parameters:

  • method_name (Symbol)

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/eac_cli/runner/context.rb', line 28

def parent_respond_to?(method_name)
  parent.if_present(false) do |v|
    next true if v.respond_to?(method_name)

    v.if_respond(:runner_context, false) { |w| w.parent_respond_to?(method_name) }
  end
end