Class: Cassava::Main
- Inherits:
-
Object
- Object
- Cassava::Main
- Defined in:
- lib/cassava/main.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#by ⇒ Object
Returns the value of attribute by.
-
#cmd ⇒ Object
Returns the value of attribute cmd.
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#exit_code ⇒ Object
Returns the value of attribute exit_code.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#output ⇒ Object
Returns the value of attribute output.
-
#progname ⇒ Object
Returns the value of attribute progname.
-
#result ⇒ Object
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #_cat! ⇒ Object
- #_cut! ⇒ Object
- #_format! ⇒ Object
- #_join! ⇒ Object
- #_sort! ⇒ Object
- #_where! ⇒ Object (also: #_select!)
-
#initialize(args = nil) ⇒ Main
constructor
A new instance of Main.
- #next_cmd! ⇒ Object
- #next_document! ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(args = nil) ⇒ Main
Returns a new instance of Main.
11 12 13 14 15 16 17 18 19 |
# File 'lib/cassava/main.rb', line 11 def initialize args = nil @progname = File.basename($0) @args = (args || ARGV).dup @opts = { } @exit_code = 0 @output = "/dev/stdout" @debug = true @select_where = { } end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
7 8 9 |
# File 'lib/cassava/main.rb', line 7 def args @args end |
#by ⇒ Object
Returns the value of attribute by.
9 10 11 |
# File 'lib/cassava/main.rb', line 9 def by @by end |
#cmd ⇒ Object
Returns the value of attribute cmd.
7 8 9 |
# File 'lib/cassava/main.rb', line 7 def cmd @cmd end |
#columns ⇒ Object
Returns the value of attribute columns.
9 10 11 |
# File 'lib/cassava/main.rb', line 9 def columns @columns end |
#exit_code ⇒ Object
Returns the value of attribute exit_code.
7 8 9 |
# File 'lib/cassava/main.rb', line 7 def exit_code @exit_code end |
#opts ⇒ Object
Returns the value of attribute opts.
7 8 9 |
# File 'lib/cassava/main.rb', line 7 def opts @opts end |
#output ⇒ Object
Returns the value of attribute output.
8 9 10 |
# File 'lib/cassava/main.rb', line 8 def output @output end |
#progname ⇒ Object
Returns the value of attribute progname.
6 7 8 |
# File 'lib/cassava/main.rb', line 6 def progname @progname end |
#result ⇒ Object
Returns the value of attribute result.
8 9 10 |
# File 'lib/cassava/main.rb', line 8 def result @result end |
Class Method Details
.define_command(name, doc, &blk) ⇒ Object
107 108 109 110 111 |
# File 'lib/cassava/main.rb', line 107 def self.define_command name, doc, &blk meth = :"_#{name}!" define_method meth, &blk @@commands << { :name => name, :doc => doc } end |
Instance Method Details
#_cat! ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/cassava/main.rb', line 113 def _cat! self.result = next_document! until args.empty? other = next_document! result.append_rows!(other) end end |
#_cut! ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/cassava/main.rb', line 136 def _cut! self.columns = nil self.result = next_document! self.columns ||= [ result.columns[0] ] # pp result.columns result.to_column_names! columns new_result = result.dup.empty_rows! new_result.columns = columns & result.columns # pp new_result.columns # pp new_result.column_offset rows = self.result.rows.map do | r | r = r.dup r.keep_if { | c | new_result.column_offset[c] } # pp r r end new_result.append_rows! rows self.result = new_result end |
#_format! ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/cassava/main.rb', line 191 def _format! rows = result.to_text # $stderr.puts rows rows = rows.split("\n").map { | r | { :_ => r } } # require 'pp'; pp rows self.result = Cassava::Document.new(:name => "#{@result.name}->format", :columns => [ :_ ], :rows => rows) end |
#_join! ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/cassava/main.rb', line 156 def _join! self.result = next_document! until args.empty? left_key = args.shift.to_sym right_key = args.shift.to_sym right = next_document! new_result = Document.new result.columns.each do | c | new_result.add_column!(c) end right.columns.each do | c | new_result.add_column!(c) end result.rows.each do | lr | rrows = right.get(right_key, lr[left_key]) unless rrows.empty? r = lr.dup rrows.each do | rr | r.merge(rr) end new_result.rows << r end end self.result = new_result end end |
#_sort! ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/cassava/main.rb', line 183 def _sort! self.by = nil self.result = next_document! by = self.by || args by.map! { | c | c.to_sym } result.sort!(by) end |
#_where! ⇒ Object Also known as: _select!
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cassava/main.rb', line 121 def _where! @select_where = { } self.result = next_document! result.coerce_to_strings! @select_where.each do | col, val | new_result = result.dup new_result.empty_rows! found_rows = result.get(col, val) # puts " #{col}=#{val} => #{found_rows.size} rows" new_result.append_rows!(found_rows) self.result = new_result end end |
#next_cmd! ⇒ Object
62 63 64 65 66 67 |
# File 'lib/cassava/main.rb', line 62 def next_cmd! @cmd = @args.shift sel = :"_#{cmd}!" raise ArgumentError, "Invalid command: #{cmd.inspect}" unless respond_to?(sel) send(sel) end |
#next_document! ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cassava/main.rb', line 69 def next_document! doc = nil opts = { } until args.empty? case arg = args.shift when '-cv' k = args.shift.to_sym v = args.shift @select_where[k] = v when /\A([^=]+)=(.*)\Z/ k = $1.to_sym v = $2 @select_where[k] = v when '--result' doc = @result break when '-c' self.columns = args.shift.split(/\s*,\s*|\s+/) when '-by' self.by = args.shift.split(/\s*,\s*|\s+/) when '-o' self.output = args.shift when '-FS' opts[:col_sep] = args.shift when '-IGNORE' opts[:ignore] = args.shift when '-C' opts[:columns] = args.shift.split(/\s*,\s*|\s+/) else opts[:name] = arg doc = Document.new(opts) doc.parse! break end end doc end |
#run! ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 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 |
# File 'lib/cassava/main.rb', line 21 def run! cmds = [ ] last_arg = nil cmd = [ ] args = @args.dup while arg = args.shift if arg == '-' if last_arg cmd << last_arg last_arg = nil end last_arg = '--result' cmds << cmd cmd = [ ] else cmd << arg end end if last_arg cmd << last_arg last_arg = nil end cmds << cmd # pp cmds # Run each command: cmds.each do | cmd | @args = cmd.dup next_cmd! end result.emit!(@output) if result self rescue ::Exception => exc $stderr.puts "#{progname}: ERROR: #{exc.inspect}" $stderr.puts " #{exc.backtrace * "\n "}" if @debug || $DEBUG @exit_code = 1 self end |