Class: Piggly::Command::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/piggly/command/base.rb

Direct Known Subclasses

Report, Trace, Untrace

Class Method Summary collapse

Class Method Details

.command(argv) ⇒ (Class, Array<String>)

Returns:

  • ((Class, Array<String>))


20
21
22
23
24
25
26
27
28
29
# 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) ⇒ PGconn

Returns:

  • (PGconn)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Enumerable<SkeletonProcedure>

Returns:

  • (Enumerable<SkeletonProcedure>)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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) ⇒ Object



9
10
11
12
13
14
15
16
17
# 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) ⇒ Object



83
84
85
# File 'lib/piggly/command/base.rb', line 83

def o_accumulate(config)
  lambda{|x| config.accumulate = x }
end

.o_cache_root(config) ⇒ Object



87
88
89
# File 'lib/piggly/command/base.rb', line 87

def o_cache_root(config)
  lambda{|x| config.cache_root = x }
end

.o_connection_name(config) ⇒ Object



103
104
105
# File 'lib/piggly/command/base.rb', line 103

def o_connection_name(config)
  lambda{|x| config.connection_name = x }
end

.o_database_yml(config) ⇒ Object



99
100
101
# File 'lib/piggly/command/base.rb', line 99

def o_database_yml(config)
  lambda{|x| config.database_yml = x }
end

.o_dry_run(config) ⇒ Object



111
112
113
# File 'lib/piggly/command/base.rb', line 111

def o_dry_run(config)
  lambda {|x| config.dry_run = true }
end

.o_include_paths(config) ⇒ Object



95
96
97
# 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) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



91
92
93
# File 'lib/piggly/command/base.rb', line 91

def o_report_root(config)
  lambda{|x| config.report_root = x }
end

.o_select(config) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



107
108
109
# File 'lib/piggly/command/base.rb', line 107

def o_version(config)
  lambda {|x| puts "piggly #{VERSION} #{VERSION::RELEASE_DATE}"; exit! }
end