Class: Ankit::Runtime
- Inherits:
-
Object
- Object
- Ankit::Runtime
- Defined in:
- lib/ankit/runtime.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
- #line ⇒ Object
- #stderr ⇒ Object
- #stdin ⇒ Object
- #stdout ⇒ Object
Class Method Summary collapse
- .parse_options(args) ⇒ Object
- .run(args) ⇒ Object
- .setup(args) ⇒ Object
- .split_subcommand(args) ⇒ Object
Instance Method Summary collapse
- #clear_screen ⇒ Object
- #dispatch(args) ⇒ Object
-
#dispatch_then(args) ⇒ Object
To encourage one-liner.
-
#initialize(config) ⇒ Runtime
constructor
A new instance of Runtime.
- #make_command(args) ⇒ Object
- #supress_io ⇒ Object
- #unsupress_io(saved) ⇒ Object
- #with_supressing_io(&block) ⇒ Object
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
#config ⇒ Object (readonly)
Returns the value of attribute config.
67 68 69 |
# File 'lib/ankit/runtime.rb', line 67 def config @config end |
#line ⇒ Object
69 |
# File 'lib/ankit/runtime.rb', line 69 def line; @line ||= HighLine.new; end |
#stderr ⇒ Object
72 |
# File 'lib/ankit/runtime.rb', line 72 def stderr; @stderr ||= STDERR; end |
#stdin ⇒ Object
70 |
# File 'lib/ankit/runtime.rb', line 70 def stdin; @stdin ||= STDIN; end |
#stdout ⇒ Object
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.(args) = {} OptionParser.new do |spec| spec.on("-c", "--config FILE", "Specifies config file") { |file| [:config] = file } end.parse(args) [:noconf] = [:config].nil? [:config] ||= Config::DEFAULT_PATH 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) = self.(args) Config.prepare_default if [:noconf] and not File.exist?(Config::DEFAULT_PATH) r = self.new(Config.open([: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_screen ⇒ Object
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_io ⇒ Object
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 |