Module: Main::Program::ClassMethods
- Included in:
- Main::Program
- Defined in:
- lib/main/program/class_methods.rb
Defined Under Namespace
Classes: Factory
Instance Method Summary collapse
- #argument(*a, &b) ⇒ Object
- #can_has(ptype, *a, &b) ⇒ Object
-
#chunkname ⇒ Object
TODO - for some reason these hork the usage!.
- #config(*args, &block) ⇒ Object
- #config_path(*config_path) ⇒ Object
- #db(*args, &block) ⇒ Object
- #db_path(*db_path) ⇒ Object
- #default_options! ⇒ Object
- #dotdir(*dotdir, &block) ⇒ Object
-
#dynamically_extend_via_commandline_modes! ⇒ Object
extend the class based on modules given in argv.
- #environment(*a, &b) ⇒ Object
- #evaluate(&block) ⇒ Object
- #factory(&block) ⇒ Object (also: #create)
- #factory=(factory) ⇒ Object
-
#fully_qualified_mode ⇒ Object
TODO.
- #has(key, *keys) ⇒ Object
- #input(*args, &block) ⇒ Object
- #io(*args) ⇒ Object
- #keyword(*a, &b) ⇒ Object
- #mixin(name, *names, &block) ⇒ Object
- #mode(name, &block) ⇒ Object
- #mode_name ⇒ Object
- #new ⇒ Object
- #option(*a, &b) ⇒ Object
- #output(*args, &block) ⇒ Object
- #parameter(*a, &b) ⇒ Object
- #params ⇒ Object
- #run(&block) ⇒ Object
- #set_default_options! ⇒ Object
- #usage(*args, &block) ⇒ Object
-
#wrap_run! ⇒ Object
wrap up users run method to handle errors, etc.
Instance Method Details
#argument(*a, &b) ⇒ Object
196 197 198 |
# File 'lib/main/program/class_methods.rb', line 196 def argument(*a, &b) (parameters << Parameter.create(:argument, main=self, *a, &b)).last end |
#can_has(ptype, *a, &b) ⇒ Object
224 225 226 227 228 |
# File 'lib/main/program/class_methods.rb', line 224 def can_has(ptype, *a, &b) key = a.map{|s| s.to_s}.sort_by{|s| -s.size }.first can_has_hash.update key => [ptype, a, b] key end |
#chunkname ⇒ Object
TODO - for some reason these hork the usage!
255 256 257 258 259 260 261 262 |
# File 'lib/main/program/class_methods.rb', line 255 %w[ examples samples api ].each do |chunkname| module_eval <<-code def #{ chunkname } *a, &b txt = b ? b.call : a.join("\\n") usage['#{ chunkname }'] = txt end code end |
#config(*args, &block) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/main/program/class_methods.rb', line 301 def config(*args, &block) unless defined?(@config) require 'yaml' unless defined?(YAML) if test(?s, config_path) @config = Map.for(YAML.load(IO.read(config_path))) else config = args.last.is_a?(Hash) ? args.last : {} lines = config.to_yaml.split(/\n/) dash = lines.shift open(config_path, 'w') do |fd| fd.puts '### you may need to edit this config!' fd.puts fd.puts lines end editor = ENV['EDITOR'] || ENV['EDIT'] || 'vi' system("#{ editor.inspect } #{ config_path }") @config = Map.for(YAML.load(IO.read(config_path))) end end @config end |
#config_path(*config_path) ⇒ Object
323 324 325 326 327 |
# File 'lib/main/program/class_methods.rb', line 323 def config_path(*config_path) @config_path = File.join(dotdir, 'config.yml') unless defined?(@config_path) @config_path = File.join(*config_path) unless config_path.empty? @config_path end |
#db(*args, &block) ⇒ Object
285 286 287 288 289 290 291 292 293 |
# File 'lib/main/program/class_methods.rb', line 285 def db(*args, &block) unless defined?(@db) require 'sequel' unless defined?(Sequel) require 'amalgalite' unless defined?(Amalgalite) @db = dotdir{ Sequel.amalgalite(db_path) } @db.instance_eval(&block) if block end @db end |
#db_path(*db_path) ⇒ Object
295 296 297 298 299 |
# File 'lib/main/program/class_methods.rb', line 295 def db_path(*db_path) @db_path = File.join(dotdir, 'db.sqlite') unless defined?(@db_path) @db_path = File.join(*db_path) unless db_path.empty? @db_path end |
#default_options! ⇒ Object
212 213 214 |
# File 'lib/main/program/class_methods.rb', line 212 def option 'help', 'h' unless parameters.has_option?('help', 'h') end |
#dotdir(*dotdir, &block) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/main/program/class_methods.rb', line 271 def dotdir(*dotdir, &block) @dotdir = File.join(Util.home, ".#{ name }") unless defined?(@dotdir) @dotdir = dotdir.join('/') unless dotdir.empty? if block require 'fileutils' unless defined?(FileUtils) FileUtils.mkdir_p(@dotdir) unless test(?d, @dotdir) Dir.chdir(&block) else @dotdir end end |
#dynamically_extend_via_commandline_modes! ⇒ Object
extend the class based on modules given in argv
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/main/program/class_methods.rb', line 107 def dynamically_extend_via_commandline_modes! self.breadth_first_modes = modes.dup size = modes.size loop do modes.each do |mode| arg = argv.first && %r/^#{ argv.first }/ if arg and mode.name =~ arg argv.shift modes.clear() breadth_first_modes.clear() evaluate(&mode) self.breadth_first_modes = modes.dup depth_first_modes[mode.name] = mode break end end arg = argv.first && %r/^#{ argv.first }/ more_modes = ( !modes.empty? and modes.any?{|mode| arg && mode.name =~ arg} ) break unless more_modes end self.modes = depth_first_modes.dup end |
#environment(*a, &b) ⇒ Object
208 209 210 |
# File 'lib/main/program/class_methods.rb', line 208 def environment(*a, &b) (parameters << Parameter.create(:environment, main=self, *a, &b)).last end |
#evaluate(&block) ⇒ Object
95 96 97 |
# File 'lib/main/program/class_methods.rb', line 95 def evaluate(&block) module_eval(&block) end |
#factory(&block) ⇒ Object Also known as: create
33 34 35 |
# File 'lib/main/program/class_methods.rb', line 33 def factory(&block) Factory.new(&block) end |
#factory=(factory) ⇒ Object
38 39 40 |
# File 'lib/main/program/class_methods.rb', line 38 def factory=(factory) @factory = factory end |
#fully_qualified_mode ⇒ Object
TODO
174 175 176 |
# File 'lib/main/program/class_methods.rb', line 174 def fully_qualified_mode modes.map{|mode| mode.name} end |
#has(key, *keys) ⇒ Object
230 231 232 233 234 235 236 237 238 |
# File 'lib/main/program/class_methods.rb', line 230 def has(key, *keys) keys = [key, *keys].flatten.compact.map{|k| k.to_s} keys.map do |key| ptype, a, b = can_has_hash[key] abort "yo - can *not* has #{ key.inspect }!?" unless(ptype and a and b) send ptype, *a, &b key end end |
#input(*args, &block) ⇒ Object
329 330 331 332 333 334 335 |
# File 'lib/main/program/class_methods.rb', line 329 def input(*args, &block) first = args.first args.push(:input) unless(first.is_a?(Symbol) or first.is_a?(String)) param = argument(*args, &block) param.cast(:input) param end |
#io(*args) ⇒ Object
345 346 347 348 349 |
# File 'lib/main/program/class_methods.rb', line 345 def io(*args) i = input(*[args.shift].compact).default('-') o = output(*[args.shift].compact).default('-') [i, o] end |
#keyword(*a, &b) ⇒ Object
204 205 206 |
# File 'lib/main/program/class_methods.rb', line 204 def keyword(*a, &b) (parameters << Parameter.create(:keyword, main=self, *a, &b)).last end |
#mixin(name, *names, &block) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/main/program/class_methods.rb', line 240 def mixin(name, *names, &block) names = [name, *names].flatten.compact.map{|name| name.to_s} if block names.each do |name| mixin_table[name] = block end else names.each do |name| module_eval(&mixin_table[name]) end end end |
#mode(name, &block) ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/main/program/class_methods.rb', line 216 def mode(name, &block) name = name.to_s block.fattr(:name => name) modes[name] = block breadth_first_modes[name] = block block end |
#mode_name ⇒ Object
178 179 180 181 |
# File 'lib/main/program/class_methods.rb', line 178 def mode_name return 'main' if modes.empty? fully_qualified_mode.join(' ') end |
#new ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/main/program/class_methods.rb', line 78 def new() instance = allocate instance.instance_eval do pre_initialize() before_initialize() main_initialize() initialize() after_initialize() post_initialize() end instance end |
#option(*a, &b) ⇒ Object
200 201 202 |
# File 'lib/main/program/class_methods.rb', line 200 def option(*a, &b) (parameters << Parameter.create(:option, main=self, *a, &b)).last end |
#output(*args, &block) ⇒ Object
337 338 339 340 341 342 343 |
# File 'lib/main/program/class_methods.rb', line 337 def output(*args, &block) first = args.first args.push(:output) unless(first.is_a?(Symbol) or first.is_a?(String)) param = argument(*args, &block) param.cast(:output) param end |
#parameter(*a, &b) ⇒ Object
192 193 194 |
# File 'lib/main/program/class_methods.rb', line 192 def parameter(*a, &b) (parameters << Parameter.create(:parameter, main=self, *a, &b)).last end |
#params ⇒ Object
91 92 93 |
# File 'lib/main/program/class_methods.rb', line 91 def params parameters end |
#run(&block) ⇒ Object
266 267 268 269 |
# File 'lib/main/program/class_methods.rb', line 266 def run(&block) block ||= lambda{} define_method(:run, &block) if block end |
#set_default_options! ⇒ Object
99 100 101 |
# File 'lib/main/program/class_methods.rb', line 99 def option('help', 'h') unless parameters.has_option?('help', 'h') end |
#usage(*args, &block) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/main/program/class_methods.rb', line 184 def usage(*args, &block) usage! unless defined? @usage return @usage if args.empty? and block.nil? key, value, *ignored = args value = block.call if block @usage[key.to_s] = value.to_s end |
#wrap_run! ⇒ Object
wrap up users run method to handle errors, etc
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/main/program/class_methods.rb', line 138 def wrap_run! evaluate do alias_method 'run!', 'run' def run() exit_status = catch :exit do begin parse_parameters if help? puts(usage.to_s) exit end pre_run before_run run! after_run post_run finalize rescue Object => exception self.exit_status ||= exception.status if exception.respond_to?(:status) handle_exception(exception) end nil end self.exit_status ||= (exit_status || exit_success) handle_exit(self.exit_status) end end end |