class Piggly::Command::Base

Public Class Methods

command(argv) click to toggle source

@return [(Class, Array<String>)]

# File lib/piggly/command/base.rb, line 20
def command(argv)
  return if argv.empty?
  head, *tail = argv

  case head.downcase
  when "report";  [Report,  tail]
  when "trace";   [Trace,   tail]
  when "untrace"; [Untrace, tail]
  end
end
connect(config) click to toggle source

@return [PGconn]

# File lib/piggly/command/base.rb, line 32
def connect(config)
  require "pg"
  require "erb"

  files = Array(config.database_yml ||
    %w(piggly/database.yml
       config/database.yml
       piggly/database.json
       config/database.json))

  path = files.find{|x| File.exists?(x) } or
    raise "No database config files found: #{files.join(", ")}"

  specs =
    if File.extname(path) == ".json"
      require "json"
      JSON.load(ERB.new(IO.read(path)).result)
    else
      require "yaml"
      YAML.load(ERB.new(IO.read(path)).result)
    end

  spec = (specs.is_a?(Hash) and specs[config.connection_name]) or
    raise "Database '#{config.connection_name}' is not configured in #{path}"

  PGconn.connect(spec["host"], spec["port"], nil, nil,
    spec["database"], spec["username"], spec["password"])
end
filter(config, index) click to toggle source

@return [Enumerable<SkeletonProcedure>]

# File lib/piggly/command/base.rb, line 62
def filter(config, index)
  if config.filters.empty?
    index.procedures
  else
    head, _ = config.filters

    start =
      case head.first
      when :+; []
      when :-; index.procedures
      end

    config.filters.inject(start) do |s, pair|
      case pair.first
      when :+; s | index.procedures.select(&pair.last)
      when :-; s.reject(&pair.last)
      end
    end
  end
end
main(argv) click to toggle source
# File lib/piggly/command/base.rb, line 9
def main(argv)
  cmd, argv = command(argv)

  if cmd.nil?
    abort "usage: #{$0} {report|trace|untrace} --help"
  else
    cmd.main(argv)
  end
end
o_accumulate(config) click to toggle source
# File lib/piggly/command/base.rb, line 83
def o_accumulate(config)
  lambda{|x| config.accumulate = x }
end
o_cache_root(config) click to toggle source
# File lib/piggly/command/base.rb, line 87
def o_cache_root(config)
  lambda{|x| config.cache_root = x }
end
o_connection_name(config) click to toggle source
# File lib/piggly/command/base.rb, line 103
def o_connection_name(config)
  lambda{|x| config.connection_name = x }
end
o_database_yml(config) click to toggle source
# File lib/piggly/command/base.rb, line 99
def o_database_yml(config)
  lambda{|x| config.database_yml = x }
end
o_dry_run(config) click to toggle source
# File lib/piggly/command/base.rb, line 111
def o_dry_run(config)
  lambda {|x| config.dry_run = true }
end
o_include_paths(config) click to toggle source
# File lib/piggly/command/base.rb, line 95
def o_include_paths(config)
  lambda{|x| config.include_paths.concat(x.split(":")) }
end
o_reject(config) click to toggle source
# File lib/piggly/command/base.rb, line 128
def o_reject(config)
  lambda do |x|
    filter =
      if m = x.match(%r{^/([^/]+)/$})
        lambda{|p| p.name.to_s.match(m.captures.first) }
      else
        lambda{|p| p.name.to_s === x }
      end

    config.filters << [:-, filter]
  end
end
o_report_root(config) click to toggle source
# File lib/piggly/command/base.rb, line 91
def o_report_root(config)
  lambda{|x| config.report_root = x }
end
o_select(config) click to toggle source
# File lib/piggly/command/base.rb, line 115
def o_select(config)
  lambda do |x|
    filter =
      if m = x.match(%r{^/([^/]+)/$})
        lambda{|p| p.name.to_s.match(m.captures.first) }
      else
        lambda{|p| p.name.to_s === x }
      end

    config.filters << [:+, filter]
  end
end
o_version(config) click to toggle source
# File lib/piggly/command/base.rb, line 107
def o_version(config)
  lambda {|x| puts "piggly #{VERSION} #{VERSION::RELEASE_DATE}"; exit! }
end