Class: Toys::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup, logger: nil, binary_name: nil, tool_name: nil, args: nil, options: nil) ⇒ Context

Returns a new instance of Context.



3
4
5
6
7
8
9
10
# File 'lib/toys/context.rb', line 3

def initialize(lookup, logger: nil, binary_name: nil, tool_name: nil, args: nil, options: nil)
  @_lookup = lookup
  @logger = logger || Logger.new(STDERR)
  @binary_name = binary_name
  @tool_name = tool_name
  @args = args
  @options = options
end

Instance Attribute Details

#_lookupObject (readonly)

Returns the value of attribute _lookup.



32
33
34
# File 'lib/toys/context.rb', line 32

def _lookup
  @_lookup
end

#argsObject (readonly)

Returns the value of attribute args.



15
16
17
# File 'lib/toys/context.rb', line 15

def args
  @args
end

#binary_nameObject (readonly)

Returns the value of attribute binary_name.



13
14
15
# File 'lib/toys/context.rb', line 13

def binary_name
  @binary_name
end

#loggerObject (readonly)

Returns the value of attribute logger.



12
13
14
# File 'lib/toys/context.rb', line 12

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/toys/context.rb', line 16

def options
  @options
end

#tool_nameObject (readonly)

Returns the value of attribute tool_name.



14
15
16
# File 'lib/toys/context.rb', line 14

def tool_name
  @tool_name
end

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/toys/context.rb', line 18

def [](key)
  @options[key]
end

#_create_child(tool_name, args, options) ⇒ Object



34
35
36
37
# File 'lib/toys/context.rb', line 34

def _create_child(tool_name, args, options)
  Context.new(@_lookup, logger: @logger, binary_name: @binary_name,
    tool_name: tool_name, args: args, options: options)
end

#exit_with_code(code) ⇒ Object



28
29
30
# File 'lib/toys/context.rb', line 28

def exit_with_code(code)
  throw :result, code
end

#run(*args) ⇒ Object



22
23
24
25
26
# File 'lib/toys/context.rb', line 22

def run(*args)
  args = args.flatten
  tool = @_lookup.lookup(args)
  tool.execute(self, args.slice(tool.full_name.length..-1))
end