Module: DatabaseRecorder::Core
- Defined in:
- lib/database_recorder/core.rb
Class Method Summary collapse
- .log_query(sql, source = nil) ⇒ Object
- .setup ⇒ Object
- .string_keys_recursive(hash) ⇒ Object
- .symbolize_recursive(hash) ⇒ Object
- .transform(value, source_method) ⇒ Object
Class Method Details
.log_query(sql, source = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/database_recorder/core.rb', line 7 def log_query(sql, source = nil) log = case DatabaseRecorder::Config.print_queries when true then "[DB] #{sql} [#{source}]" when :color then "[DB] #{CodeRay.scan(sql, :sql).term} [#{source}]" end puts log if log log end |
.setup ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/database_recorder/core.rb', line 18 def setup case DatabaseRecorder::Config.db_driver when :active_record then ActiveRecord::Recorder.setup when :mysql2 then Mysql2::Recorder.setup when :pg then PG::Recorder.setup end end |
.string_keys_recursive(hash) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/database_recorder/core.rb', line 26 def string_keys_recursive(hash) {}.tap do |h| hash.each do |key, value| h[key.to_s] = transform(value, :string_keys_recursive) end end end |
.symbolize_recursive(hash) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/database_recorder/core.rb', line 34 def symbolize_recursive(hash) {}.tap do |h| hash.each do |key, value| h[key.to_sym] = transform(value, :symbolize_recursive) end end end |
.transform(value, source_method) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/database_recorder/core.rb', line 42 def transform(value, source_method) case value when Hash then method(source_method).call(value) when Array then value.map { |v| transform(v, source_method) } else value end end |