Class: Ankit::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/ankit/runtime.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Runtime

Returns a new instance of Runtime.



111
112
113
# File 'lib/ankit/runtime.rb', line 111

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



67
68
69
# File 'lib/ankit/runtime.rb', line 67

def config
  @config
end

#lineObject



69
# File 'lib/ankit/runtime.rb', line 69

def line; @line ||= HighLine.new; end

#stderrObject



72
# File 'lib/ankit/runtime.rb', line 72

def stderr; @stderr ||= STDERR; end

#stdinObject



70
# File 'lib/ankit/runtime.rb', line 70

def stdin; @stdin ||= STDIN; end

#stdoutObject



71
# File 'lib/ankit/runtime.rb', line 71

def stdout; @stdout ||= STDOUT; end

Class Method Details

.parse_options(args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/ankit/runtime.rb', line 84

def self.parse_options(args)
  options = {}
  OptionParser.new do |spec|
    spec.on("-c", "--config FILE", "Specifies config file") { |file| options[:config] = file }
  end.parse(args)

  options[:noconf]   = options[:config].nil?
  options[:config] ||= Config::DEFAULT_PATH
  options
end

.run(args) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/ankit/runtime.rb', line 101

def self.run(args)
  splitted = self.split_subcommand(args)
  r = self.setup(splitted[:global])
  if splitted[:subcommand].empty?
    r.dispatch([ChallengeCommand.command_name])
  else
    r.dispatch(splitted[:subcommand])
  end
end

.setup(args) ⇒ Object



95
96
97
98
99
# File 'lib/ankit/runtime.rb', line 95

def self.setup(args)
  options = self.parse_options(args)
  Config.prepare_default if options[:noconf] and not File.exist?(Config::DEFAULT_PATH)
  r = self.new(Config.open(options[:config]))
end

.split_subcommand(args) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/ankit/runtime.rb', line 74

def self.split_subcommand(args)
  names = Command.by_name.keys
  i = args.find_index { |a| names.include?(a) }
  unless i.nil?
    { global: args[0 ... i], subcommand: args[i .. -1] }
  else
    { global: [], subcommand: [] }
  end
end

Instance Method Details

#clear_screenObject



138
139
140
141
142
143
144
# File 'lib/ankit/runtime.rb', line 138

def clear_screen
  w = HighLine::SystemExtensions.terminal_size[1]
  stdout.print("\033[#{w}D")
  stdout.print("\033[2J")
  h = HighLine::SystemExtensions.terminal_size[0]
  stdout.print("\033[#{h}A")
end

#dispatch(args) ⇒ Object



120
121
122
123
124
# File 'lib/ankit/runtime.rb', line 120

def dispatch(args)
  command = make_command(args)
  command.execute()
  command
end

#dispatch_then(args) ⇒ Object

To encourage one-liner



127
128
129
130
# File 'lib/ankit/runtime.rb', line 127

def dispatch_then(args)
  dispatch(args)
  self
end

#make_command(args) ⇒ Object



115
116
117
118
# File 'lib/ankit/runtime.rb', line 115

def make_command(args)
  name = args.shift
  Command.by_name[name].new(self, args)
end

#supress_ioObject



132
133
134
135
136
# File 'lib/ankit/runtime.rb', line 132

def supress_io
  saved = [@stdin, @stdout, @stderr]
  ["stdin=", "stdout=", "stderr="].each { |m| self.send(m, StringIO.new) }
  saved
end

#unsupress_io(saved) ⇒ Object



146
147
148
# File 'lib/ankit/runtime.rb', line 146

def unsupress_io(saved)
  @stdin, @stdout, @stderr =  saved
end

#with_supressing_io(&block) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/ankit/runtime.rb', line 150

def with_supressing_io(&block)
  saved = supress_io
  begin
    block.call
  ensure
    unsupress_io(saved)
  end
end