Class: Piggly::Command::Base
Class Method Summary collapse
- .command(argv) ⇒ (Class, Array<String>)
- .connect(config) ⇒ PG::Connection
- .filter(config, index) ⇒ Enumerable<SkeletonProcedure>
- .main(argv) ⇒ Object
- .o_accumulate(config) ⇒ Object
- .o_cache_root(config) ⇒ Object
- .o_connection_name(config) ⇒ Object
- .o_database_yml(config) ⇒ Object
- .o_dry_run(config) ⇒ Object
- .o_include_paths(config) ⇒ Object
- .o_reject(config) ⇒ Object
- .o_report_root(config) ⇒ Object
- .o_select(config) ⇒ Object
- .o_version(config) ⇒ Object
Class Method Details
.command(argv) ⇒ (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) ⇒ PG::Connection
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 60 61 62 63 64 |
# 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.exist?(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.unsafe_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}" conn = PG::Connection.connect(spec["host"], spec["port"], nil, nil, spec["database"], spec["username"], spec["password"]) # Set client encoding to UTF-8 to properly handle Cyrillic and other Unicode characters conn.set_client_encoding('UTF8') conn end |
.filter(config, index) ⇒ Enumerable<SkeletonProcedure>
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/piggly/command/base.rb', line 67 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
88 89 90 |
# File 'lib/piggly/command/base.rb', line 88 def o_accumulate(config) lambda{|x| config.accumulate = x } end |
.o_cache_root(config) ⇒ Object
92 93 94 |
# File 'lib/piggly/command/base.rb', line 92 def o_cache_root(config) lambda{|x| config.cache_root = x } end |
.o_connection_name(config) ⇒ Object
108 109 110 |
# File 'lib/piggly/command/base.rb', line 108 def o_connection_name(config) lambda{|x| config.connection_name = x } end |
.o_database_yml(config) ⇒ Object
104 105 106 |
# File 'lib/piggly/command/base.rb', line 104 def o_database_yml(config) lambda{|x| config.database_yml = x } end |
.o_dry_run(config) ⇒ Object
116 117 118 |
# File 'lib/piggly/command/base.rb', line 116 def o_dry_run(config) lambda {|x| config.dry_run = true } end |
.o_include_paths(config) ⇒ Object
100 101 102 |
# File 'lib/piggly/command/base.rb', line 100 def o_include_paths(config) lambda{|x| config.include_paths.concat(x.split(":")) } end |
.o_reject(config) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/piggly/command/base.rb', line 133 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
96 97 98 |
# File 'lib/piggly/command/base.rb', line 96 def o_report_root(config) lambda{|x| config.report_root = x } end |
.o_select(config) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/piggly/command/base.rb', line 120 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
112 113 114 |
# File 'lib/piggly/command/base.rb', line 112 def o_version(config) lambda {|x| puts "piggly #{VERSION} #{VERSION::RELEASE_DATE}"; exit! } end |