Module: ConsoleUtil
- Defined in:
- lib/console_util.rb,
lib/console_util/color.rb,
lib/console_util/version.rb
Defined Under Namespace
Classes: Color
Constant Summary collapse
- VERSION =
"0.3.0"
Class Attribute Summary collapse
-
.suppressed_output ⇒ Object
readonly
Returns the value of attribute suppressed_output.
Class Method Summary collapse
-
.grep_stdout(expression) ⇒ Object
Allows you to filter output to the console using grep Ex: def foo puts “Some debugging output here” puts “The value of x is y” puts “The value of foo is bar” end.
-
.import_mysql_dump(filename) ⇒ Object
Import a MySQL dump file.
-
.load_all_models ⇒ Object
Load all models into the environment.
-
.output_sql_to_console(options = {}) ⇒ Object
Monkey-patch the ActiveRecord connection to output the SQL query before executing it.
-
.suppress_stdout ⇒ Object
Allows you to suppress $stdout but allows you to send certain messages to $stdout Ex: def foo puts “lots of stuff directed to $stdout that I don’t want to see.” end suppress_stdout do |stdout| stdout.puts “About to call #foo” foo stdout.puts “Called foo” end # => About to call #foo # => Called foo # => <# The result of #foo >.
Class Attribute Details
.suppressed_output ⇒ Object (readonly)
Returns the value of attribute suppressed_output.
5 6 7 |
# File 'lib/console_util.rb', line 5 def suppressed_output @suppressed_output end |
Class Method Details
.grep_stdout(expression) ⇒ Object
Allows you to filter output to the console using grep Ex:
def foo
puts "Some debugging output here"
puts "The value of x is y"
puts "The value of foo is bar"
end
grep_stdout(/value/) { foo }
# => The value of x is y
# => The value of foo is bar
# => nil
55 56 57 58 59 60 61 62 63 64 65 66 67 68 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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/console_util.rb', line 55 def grep_stdout(expression) # First we need to create a ruby "pipe" which is two sets of IO subclasses # the first is read only (which represents a fake $stdin) and the second is # write only (which represents a fake $stdout). placeholder_in, placeholder_out = IO.pipe # This child process handles the grep'ing. Its done in a child process so that # it can operate in parallel with the main process. pid = fork { # sync $stdout so we can report any matches asap $stdout.sync # replace $stdout with placeholder_out $stdin.reopen(placeholder_in) # we have to close both placeholder_out and placeholder_in because all instances # of an IO stream must be closed in order for it to ever reach EOF. There's two # in this method; one in the child process and one in the main process. placeholder_in.close placeholder_out.close # loop continuously until we reach EOF (which happens when all # instances of placeholder_out have closed) read_buffer = '' loop do begin match = nil next_read = $stdin.readpartial(4096) read_buffer << next_read if line_match = read_buffer.match(/^(.*\n)(.*)$/m) match = line_match[1].grep(expression) # grep complete lines read_buffer = line_match[2] # save remaining partial line for the next iteration end rescue EOFError match = read_buffer.grep(expression) # grep any remaining partial line at EOF break end if match && !match.empty? print match end end } # Save the original stdout out to a variable so we can use it again after this # method is done original_stdout = $stdout # Redirect stdout to our pipe $stdout = placeholder_out # sync $stdout so that we can start operating on it as soon as possible $stdout.sync # allow the block to execute and save its return value return_value = yield # Set stdout back to the original so output will flow again $stdout = original_stdout # close the main instances of placeholder_in and placeholder_out placeholder_in.close placeholder_out.close # Wait for the child processes to finish Process.wait pid # Because the connection to the database has a tendency to go away when calling this, reconnect here # if we're using ActiveRecord if defined?(ActiveRecord) suppress_stdout { ActiveRecord::Base.verify_active_connections! } end # return the value of the block return_value end |
.import_mysql_dump(filename) ⇒ Object
Import a MySQL dump file.
- This may work with other databases, but has only been tested with MySQL.
28 29 30 31 32 33 |
# File 'lib/console_util.rb', line 28 def import_mysql_dump(filename) sql_dump_file = File.open(Rails.root.join('sql', "#{filename}.sql")) while sql_statement = sql_dump_file.gets(";\n") do ActiveRecord::Base.connection.execute(sql_statement) unless sql_statement.blank? end end |
.load_all_models ⇒ Object
Load all models into the environment
36 37 38 39 40 41 |
# File 'lib/console_util.rb', line 36 def load_all_models Rails.root.join('app/models').each_entry do |model_file| model_match = model_file.to_s.match(/^(.*)\.rb$/) model_match[1].classify.constantize if model_match end end |
.output_sql_to_console(options = {}) ⇒ Object
Monkey-patch the ActiveRecord connection to output the SQL query before executing it.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/console_util.rb', line 9 def output_sql_to_console( = {}) color = Color::CYAN if [:color] raise ArgumentError.new("invalid color option: #{[:color].inspect}") unless Color.valid_escape_code?([:color]) color = [:color] end ActiveRecord::Base.connection.class_eval do define_method(:execute) do |sql, *name| puts "#{color}#{sql}#{Color::RESET}" super end end true end |
.suppress_stdout ⇒ Object
Allows you to suppress $stdout but allows you to send certain messages to $stdout Ex:
def foo
puts "lots of stuff directed to $stdout that I don't want to see."
end
suppress_stdout do |stdout|
stdout.puts "About to call #foo"
foo
stdout.puts "Called foo"
end
# => About to call #foo
# => Called foo
# => <# The result of #foo >
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/console_util.rb', line 147 def suppress_stdout original_stdout = $stdout $stdout = output_buffer = StringIO.new begin return_value = yield(original_stdout) ensure $stdout = original_stdout @suppressed_output ||= "" @suppressed_output << output_buffer.string end return_value end |