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 17 18 19 |
# File 'lib/database_recorder/core.rb', line 7 def log_query(sql, source = nil) log = case DatabaseRecorder::Config.print_queries when true DatabaseRecorder::Config.log_format.sub('%name', source.to_s).sub('%sql', sql) when :color code_ray_sql = CodeRay.scan(sql, :sql).term DatabaseRecorder::Config.log_format.sub('%name', source.to_s).sub('%sql', code_ray_sql || '') end puts log if log log end |
.setup ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/database_recorder/core.rb', line 21 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
29 30 31 32 33 34 35 |
# File 'lib/database_recorder/core.rb', line 29 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
37 38 39 40 41 42 43 |
# File 'lib/database_recorder/core.rb', line 37 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
45 46 47 48 49 50 51 |
# File 'lib/database_recorder/core.rb', line 45 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 |