Class: DatabaseRecorder::Recording
- Inherits:
-
Object
- Object
- DatabaseRecorder::Recording
- Extended by:
- Forwardable
- Defined in:
- lib/database_recorder/recording.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#entities ⇒ Object
Returns the value of attribute entities.
-
#from_cache ⇒ Object
readonly
Returns the value of attribute from_cache.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prepared_queries ⇒ Object
readonly
Returns the value of attribute prepared_queries.
-
#queries ⇒ Object
readonly
Returns the value of attribute queries.
-
#started ⇒ Object
readonly
Returns the value of attribute started.
Class Method Summary collapse
Instance Method Summary collapse
- #cached_query_for(sql) ⇒ Object
-
#initialize(options: {}) ⇒ Recording
constructor
A new instance of Recording.
- #new_entity(model:, id:) ⇒ Object
- #pull_entity ⇒ Object
- #push(sql:, name: nil, binds: nil, result: nil, source: nil) ⇒ Object
- #push_prepared(name: nil, sql: nil, binds: nil, result: nil, source: nil) ⇒ Object
- #start ⇒ Object
- #update_prepared(name: nil, sql: nil, binds: nil, result: nil, source: nil) ⇒ Object
Constructor Details
#initialize(options: {}) ⇒ Recording
Returns a new instance of Recording.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/database_recorder/recording.rb', line 10 def initialize(options: {}) (@@instances ||= {})[Process.pid] = self @cache = nil @entities = [] @metadata = {} @options = @queries = [] @search_index = 0 @@prepared_queries ||= {} end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
7 8 9 |
# File 'lib/database_recorder/recording.rb', line 7 def cache @cache end |
#entities ⇒ Object
Returns the value of attribute entities.
7 8 9 |
# File 'lib/database_recorder/recording.rb', line 7 def entities @entities end |
#from_cache ⇒ Object (readonly)
Returns the value of attribute from_cache.
8 9 10 |
# File 'lib/database_recorder/recording.rb', line 8 def from_cache @from_cache end |
#metadata ⇒ Object
Returns the value of attribute metadata.
7 8 9 |
# File 'lib/database_recorder/recording.rb', line 7 def @metadata end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/database_recorder/recording.rb', line 8 def @options end |
#prepared_queries ⇒ Object (readonly)
Returns the value of attribute prepared_queries.
8 9 10 |
# File 'lib/database_recorder/recording.rb', line 8 def prepared_queries @prepared_queries end |
#queries ⇒ Object (readonly)
Returns the value of attribute queries.
8 9 10 |
# File 'lib/database_recorder/recording.rb', line 8 def queries @queries end |
#started ⇒ Object (readonly)
Returns the value of attribute started.
8 9 10 |
# File 'lib/database_recorder/recording.rb', line 8 def started @started end |
Class Method Details
.current_instance ⇒ Object
80 81 82 |
# File 'lib/database_recorder/recording.rb', line 80 def current_instance (@@instances ||= {})[Process.pid] end |
.started? ⇒ Boolean
84 85 86 |
# File 'lib/database_recorder/recording.rb', line 84 def started? current_instance&.started end |
Instance Method Details
#cached_query_for(sql) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/database_recorder/recording.rb', line 21 def cached_query_for(sql) current = @search_index match = cache[@search_index..].find do |item| current += 1 item[:sql] == sql end return unless match @search_index = current match # cache.shift # TMP end |
#new_entity(model:, id:) ⇒ Object
35 36 37 |
# File 'lib/database_recorder/recording.rb', line 35 def new_entity(model:, id:) @entities.push(model: model, id: id) end |
#pull_entity ⇒ Object
39 40 41 |
# File 'lib/database_recorder/recording.rb', line 39 def pull_entity @entities.shift end |
#push(sql:, name: nil, binds: nil, result: nil, source: nil) ⇒ Object
43 44 45 46 |
# File 'lib/database_recorder/recording.rb', line 43 def push(sql:, name: nil, binds: nil, result: nil, source: nil) query = { name: name, sql: sql, binds: binds, result: result }.compact @queries.push(query) end |
#push_prepared(name: nil, sql: nil, binds: nil, result: nil, source: nil) ⇒ Object
48 49 50 51 |
# File 'lib/database_recorder/recording.rb', line 48 def push_prepared(name: nil, sql: nil, binds: nil, result: nil, source: nil) query = { name: name, sql: sql, binds: binds, result: result }.compact @@prepared_queries[name || sql] = query end |
#start ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/database_recorder/recording.rb', line 53 def start @started = true storage = Config.storage&.new(self, name: [:name], options: Config.) @from_cache = storage&.load yield storage&.save unless from_cache @started = false result = { current_queries: queries.map { |query| query[:sql] } } result[:stored_queries] = cache.map { |query| query[:sql] } if from_cache result end |
#update_prepared(name: nil, sql: nil, binds: nil, result: nil, source: nil) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/database_recorder/recording.rb', line 65 def update_prepared(name: nil, sql: nil, binds: nil, result: nil, source: nil) query = @@prepared_queries[name || sql] query[:sql] = sql if sql query[:binds] = binds if binds query[:result] = result if result @queries.push(query) query end |