Main::Program::ClassMethods

Public Instance Methods

argument(*a, &b) click to toggle source
# File lib/main/program/class_methods.rb, line 188
def argument(*a, &b)
  (parameters << Parameter.create(:argument, self, *a, &b)).last
end
can_has(ptype, *a, &b) click to toggle source
# File lib/main/program/class_methods.rb, line 216
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
default_options!() click to toggle source
# File lib/main/program/class_methods.rb, line 204
def default_options!
  option 'help', 'h' unless parameters.has_option?('help', 'h')
end
dynamically_extend_via_commandline_modes!() click to toggle source

extend the class based on modules given in argv

# File lib/main/program/class_methods.rb, line 99
def dynamically_extend_via_commandline_modes!
  self.breadth_first_modes = modes.dup
  size = modes.size

  loop do
    modes.each do |mode|
      arg = argv.first && /^#{ 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 && /^#{ 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) click to toggle source
# File lib/main/program/class_methods.rb, line 200
def environment(*a, &b)
  (parameters << Parameter.create(:environment, self, *a, &b)).last
end
evaluate(&block) click to toggle source
# File lib/main/program/class_methods.rb, line 87
def evaluate(&block)
  module_eval(&block)
end
factory(&block) click to toggle source
# File lib/main/program/class_methods.rb, line 33
def factory(&block)
  Factory.new(&block)
end
fully_qualified_mode() click to toggle source

TODO

# File lib/main/program/class_methods.rb, line 166
def fully_qualified_mode
  modes.map{|mode| mode.name}
end
has(key, *keys) click to toggle source
# File lib/main/program/class_methods.rb, line 222
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
keyword(*a, &b) click to toggle source
# File lib/main/program/class_methods.rb, line 196
def keyword(*a, &b)
  (parameters << Parameter.create(:keyword, self, *a, &b)).last
end
mixin(name, *names, &block) click to toggle source
# File lib/main/program/class_methods.rb, line 232
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) click to toggle source
# File lib/main/program/class_methods.rb, line 208
def mode(name, &block)
  name = name.to_s
  block.fattr(:name => name)
  modes[name] = block
  breadth_first_modes[name] = block
  block
end
mode_name() click to toggle source
# File lib/main/program/class_methods.rb, line 170
def mode_name
  return 'main' if modes.empty?
  fully_qualified_mode.join(' ')
end
new() click to toggle source
# File lib/main/program/class_methods.rb, line 74
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) click to toggle source
# File lib/main/program/class_methods.rb, line 192
def option(*a, &b)
  (parameters << Parameter.create(:option, self, *a, &b)).last
end
parameter(*a, &b) click to toggle source
# File lib/main/program/class_methods.rb, line 184
def parameter(*a, &b)
  (parameters << Parameter.create(:parameter, self, *a, &b)).last
end
run() click to toggle source
# File lib/main/program/class_methods.rb, line 134
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
set_default_options!() click to toggle source
# File lib/main/program/class_methods.rb, line 91
def set_default_options!
  option('help', 'h') unless parameters.has_option?('help', 'h')
end
usage(*args, &block) click to toggle source
# File lib/main/program/class_methods.rb, line 176
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!() click to toggle source

wrap up users run method to handle errors, etc

# File lib/main/program/class_methods.rb, line 130
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.