module DataSnapshots

 frozen_string_literal: true

Constants

VERSION

Attributes

configuration[RW]

Public Class Methods

configure() { |configuration| ... } click to toggle source
# File lib/data_snapshots.rb, line 19
def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end
fetch_snapshots(name:) click to toggle source
# File lib/data_snapshots.rb, line 53
def self.fetch_snapshots(name:)
  DataSnapshots::Snapshot.where(name: name).order(:created_at)
end
generate_snapshot(name:) click to toggle source
# File lib/data_snapshots.rb, line 30
def self.generate_snapshot(name:)
  snapshot = DataSnapshots.configuration.snapshots[name]

  unless snapshot
    raise UnregisteredSnapshotError.new("Snapshot: #{name} has not been registered")
  end

  if snapshot[:model]
    raise IncompatibleSnapshotError.new(
      "Snapshot: #{name} is a model snapshot, this method is for generic snapshots." \
      "Try calling #generate_snapshot(name: #{name}) on an instance of the appropriate model." \
      "Alternatively, call DataSnapshots.generate_snapshots(name: #{name}, collection: [Array of instances])"
    )
  end

  data = {}
  snapshot[:methods].each do |key, method|
    data[key] = method.call()
  end

  DataSnapshots::Snapshot.create!(name: name, data: data)
end
generate_snapshots(name:, collection:) click to toggle source
# File lib/data_snapshots.rb, line 24
def self.generate_snapshots(name:, collection:)
  Array(collection).each do |instance|
    instance.generate_snapshot(name: name)
  end
end
table_name_prefix() click to toggle source
# File lib/data_snapshots.rb, line 15
def self.table_name_prefix
  'data_snapshots_'
end