Class: DatabaseRecorder::Recording

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/database_recorder/recording.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = options
  @queries = []
  @search_index = 0
  @@prepared_queries ||= {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



7
8
9
# File 'lib/database_recorder/recording.rb', line 7

def cache
  @cache
end

#entitiesObject

Returns the value of attribute entities.



7
8
9
# File 'lib/database_recorder/recording.rb', line 7

def entities
  @entities
end

#from_cacheObject (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

#metadataObject

Returns the value of attribute metadata.



7
8
9
# File 'lib/database_recorder/recording.rb', line 7

def 
  @metadata
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/database_recorder/recording.rb', line 8

def options
  @options
end

#prepared_queriesObject (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

#queriesObject (readonly)

Returns the value of attribute queries.



8
9
10
# File 'lib/database_recorder/recording.rb', line 8

def queries
  @queries
end

#startedObject (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_instanceObject



80
81
82
# File 'lib/database_recorder/recording.rb', line 80

def current_instance
  (@@instances ||= {})[Process.pid]
end

.started?Boolean

Returns:

  • (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_entityObject



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

#startObject



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: options[:name], options: Config.storage_options)
  @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