module BinDiesel::InstanceMethods

Public Class Methods

new(args=["--help"]) click to toggle source
# File lib/bin_diesel.rb, line 55
def initialize args=["--help"]
  @args = args
  @options = OpenStruct.new(:dry_run => false, :verbose => false)

  begin
    parse_options
  rescue OptionParser::MissingArgument => e
    error_message e.message
    exit unhappy_ending
  end

  @verbose = @options.verbose
  @dry_run = @options.dry_run

  setup_option_accessors

  begin
    post_initialize
  rescue Exception => e
    error_message e.message
    exit unhappy_ending
  end
end

Public Instance Methods

dry_run?() click to toggle source
# File lib/bin_diesel.rb, line 79
def dry_run?
  @options.dry_run
end
error_message(text) click to toggle source
# File lib/bin_diesel.rb, line 99
def error_message text
  # Always print out error messages, regardless of verbose mode
  puts "!! #{text}"
end
info_message(text) click to toggle source
# File lib/bin_diesel.rb, line 95
def info_message text
  message "** #{text}"
end
message(text) click to toggle source
# File lib/bin_diesel.rb, line 91
def message text
  puts text if verbose
end
post_initialize() click to toggle source
# File lib/bin_diesel.rb, line 87
def post_initialize
  # TODO: EXPLAIN ME
end
run() click to toggle source
# File lib/bin_diesel.rb, line 83
def run
  raise NotImplementedError, "#{self.class} does not implement method run. You may do this explictly, or via the class level DSL (recommended):\nrun do\n ...\nend"
end

Private Instance Methods

happy_ending() click to toggle source
# File lib/bin_diesel.rb, line 106
def happy_ending
  0
end
optionize(string) click to toggle source
# File lib/bin_diesel.rb, line 176
def optionize string
  "--#{string.gsub('_', '-')}"
end
parse_options() click to toggle source
# File lib/bin_diesel.rb, line 126
def parse_options
  opts = OptionParser.new do |opt_parser|
    opt_parser.banner = OPTS[:banner]
    opt_parser.separator ""

    OPTS[:description].each{|description| opt_parser.separator description }
    opt_parser.separator ""

    opt_parser.separator "Specific options:"
    opt_parser.on("-d", "--dry-run", "Run script without any real changes.", "\tSets --verbose by default.") do |dry_run|
      options.dry_run = true
      options.verbose = true
    end

    opt_parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
      options.verbose = v
    end
    opt_parser.separator ""

    # USER SPECIFIED
    OPTS[:user_options].each do |option|
      # We allow the user to define the proc for the opts.on block via option[:block].
      # OptionParser can get confused by the last argument (the block), and sometimes defines it twice,
      # which blows up spectacularly with an ArgumentError
      option[:options].reject! {|o| o.class.to_s == 'Proc'}

      if option[:block]
        opt_parser.on(*(option[:options] << Proc.new{|*args| self.instance_exec(*args, &option[:block])}))
      else
        opt_parser.on(*option[:options])
      end
    end
    opt_parser.separator ""

    opt_parser.separator "Common options:"
    opt_parser.on_tail("-h", "--help", "Show this message") do
      puts opt_parser
      exit happy_ending
    end
  end

  the_options = opts.parse! args

  OPTS[:required_options].each do |required_option|
    raise OptionParser::MissingArgument.new("#{optionize(required_option.to_s)} - Run with --help for help.") if @options.send(required_option).nil?
  end

  the_options
end
setup_option_accessors() click to toggle source
# File lib/bin_diesel.rb, line 114
def setup_option_accessors
  OPTS[:accessible_options].each do |opt|
    define_singleton_method opt do
      @options.send(opt)
    end

    define_singleton_method "#{opt}=" do |val|
      @options.send("#{opt}=", val)
    end
  end
end
unhappy_ending() click to toggle source
# File lib/bin_diesel.rb, line 110
def unhappy_ending
  1
end