data_snapshots 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 583f6e7d574b9396d4bbc8463bf0fc35f84cc7bf2657d0fe176ade97b4c0dfbb
4
+ data.tar.gz: c47701a19cad25187cd87fac48340ec8f994e70987b7a4f0074ba3d4fba9c80e
5
+ SHA512:
6
+ metadata.gz: 2db23361ff2066cf6d38a797acbe669f0b68dce20cd314d49bc36b597c2f64bef0342f9fafd0644501508f6fef9ee1d433091e9a73d1286f14400a41493b24a7
7
+ data.tar.gz: 2dcdb729c511035c5ad3018e32ff89a12a6ca9533291eb96eb47fab922c473ee3ac20a87da64f639dc2777a9216fbc8245dc819e7ff692c3a2978f8a0ad3ead7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2021 k-p-jones
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # DataSnapshots
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'data_snapshots'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install data_snapshots
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,32 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DataSnapshots'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+ require 'bundler/gem_tasks'
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'test'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = false
30
+ end
31
+
32
+ task default: :test
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DataSnapshots
4
+ class Snapshot < ApplicationRecord
5
+ validates :name, presence: true
6
+ validates :data, presence: true
7
+ end
8
+ end
@@ -0,0 +1 @@
1
+ require 'data_snapshots/active_record_extension'
@@ -0,0 +1,12 @@
1
+ class CreateSnapshots < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :data_snapshots_snapshots do |t|
4
+ t.string :name
5
+ t.integer :model_id
6
+ t.string :model_type
7
+ t.json :data
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ require 'data_snapshots/engine'
2
+ require 'data_snapshots/configuration'
3
+
4
+ module DataSnapshots
5
+ class << self
6
+ attr_accessor :configuration
7
+ end
8
+
9
+ def self.table_name_prefix
10
+ 'data_snapshots_'
11
+ end
12
+
13
+ def self.configure
14
+ self.configuration ||= Configuration.new
15
+ yield(configuration)
16
+ end
17
+
18
+ def self.generate_snapshots(name:, collection:)
19
+ Array(collection).each do |instance|
20
+ instance.generate_snapshot(name: name)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ class UnregisteredSnapshotError < StandardError
6
+ end
7
+
8
+ module DataSnapshots
9
+ module ActiveRecordExtension
10
+ extend ActiveSupport::Concern
11
+
12
+ def method_missing(method, *args, &block)
13
+ return super(method, *args, &block) unless method.to_s.end_with?('_snapshots')
14
+ match_data = method.to_s.match(/(?<snapshot_name>\w+)_snapshots/)
15
+ name = match_data[:snapshot_name]
16
+ fetch_snapshots(name)
17
+ end
18
+
19
+ def generate_snapshot(name:)
20
+ snapshot = DataSnapshots.configuration.snapshots[name]
21
+
22
+ unless snapshot
23
+ raise UnregisteredSnapshotError.new("Snapshot: #{name} has not been registered")
24
+ end
25
+
26
+ data = {}
27
+ snapshot[:methods].each do |name, method|
28
+ data[name] = method.call(self)
29
+ end
30
+
31
+ DataSnapshots::Snapshot.create!(
32
+ name: name,
33
+ model_id: self.id,
34
+ model_type: self.class.to_s,
35
+ data: data
36
+ )
37
+ end
38
+
39
+ private
40
+
41
+ def fetch_snapshots(name)
42
+ DataSnapshots::Snapshot.where(name: name)
43
+ .where(model_id: self.id)
44
+ .where(model_type: self.class.to_s)
45
+ .order(:created_at)
46
+ end
47
+ end
48
+ end
49
+
50
+ ActiveRecord::Base.send(:include, DataSnapshots::ActiveRecordExtension)
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DataSnapshots
4
+ class Configuration
5
+ attr_reader :snapshots
6
+
7
+ def initialize
8
+ @snapshots = {}
9
+ end
10
+
11
+ def register_snapshot(name:)
12
+ return unless block_given?
13
+ snapshots[name] = { methods: {} }
14
+ yield snapshots[name][:methods]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ module DataSnapshots
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module DataSnapshots
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :data_snapshots do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_snapshots
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - k-p-jones
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: database_cleaner
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Flexible data snapshotting for Ruby on Rails.
56
+ email:
57
+ - kenjones620@yahoo.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/models/data_snapshots/snapshot.rb
66
+ - config/initializers/extensions.rb
67
+ - db/migrate/20210206113359_create_snapshots.rb
68
+ - lib/data_snapshots.rb
69
+ - lib/data_snapshots/active_record_extension.rb
70
+ - lib/data_snapshots/configuration.rb
71
+ - lib/data_snapshots/engine.rb
72
+ - lib/data_snapshots/version.rb
73
+ - lib/tasks/data_snapshots_tasks.rake
74
+ homepage: https://github.com/k-p-jones/data_snapshots
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.0.4
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Data snapshots for Rails apps.
97
+ test_files: []