Class: Commands::CommandContext

Inherits:
Object
  • Object
show all
Defined in:
lib/commands/commandcontext.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments, onerror, onoutput, onprompt, onexit) ⇒ CommandContext

Returns a new instance of CommandContext.



9
10
11
12
13
14
15
16
17
# File 'lib/commands/commandcontext.rb', line 9

def initialize(arguments, onerror, onoutput, onprompt, onexit)
  @arguments = arguments
  @headless = (@arguments.any? { |arg| arg == '--headless' })
  @onerror = onerror
  @onexit = onexit
  @onoutput = onoutput
  @onprompt = onprompt
  @now_lambda = lambda {|| DateTime.now }
end

Instance Attribute Details

#now_lambdaObject

Returns the value of attribute now_lambda.



7
8
9
# File 'lib/commands/commandcontext.rb', line 7

def now_lambda
  @now_lambda
end

#output_lambdaObject

Returns the value of attribute output_lambda.



7
8
9
# File 'lib/commands/commandcontext.rb', line 7

def output_lambda
  @output_lambda
end

#prompt_lambdaObject

Returns the value of attribute prompt_lambda.



7
8
9
# File 'lib/commands/commandcontext.rb', line 7

def prompt_lambda
  @prompt_lambda
end

Instance Method Details

#error(s) ⇒ Object



35
36
37
# File 'lib/commands/commandcontext.rb', line 35

def error(s)
  @onerror.call(s)
end

#get_nowObject

def prompt_as_lambda lambda {|field, text, defaultvalue| prompt field, text, defaultvalue } end



50
51
52
# File 'lib/commands/commandcontext.rb', line 50

def get_now
  @now_lambda.call
end

#number_of_argumentsObject



27
28
29
# File 'lib/commands/commandcontext.rb', line 27

def number_of_arguments
  @arguments.length
end

#output(s) ⇒ Object



31
32
33
# File 'lib/commands/commandcontext.rb', line 31

def output(s)
  @onoutput.call(s)
end

#pop_argument!(text_when_missing = nil) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/commands/commandcontext.rb', line 19

def pop_argument!(text_when_missing = nil)
  if number_of_arguments == 0
    @onerror.call(text_when_missing)
    @onexit.call(1)
  end
  @arguments.shift
end

#prompt(text) ⇒ Object



39
40
41
42
43
44
# File 'lib/commands/commandcontext.rb', line 39

def prompt(text)
  if @headless
    raise HeadLessCommandContextException.new()
  end
  @onprompt.call(text)
end