data_snapshots 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 583f6e7d574b9396d4bbc8463bf0fc35f84cc7bf2657d0fe176ade97b4c0dfbb
4
- data.tar.gz: c47701a19cad25187cd87fac48340ec8f994e70987b7a4f0074ba3d4fba9c80e
3
+ metadata.gz: 40cbe4d1f45ed3d90d838d1b427a5e9276b614f06036a9b6f009896aed8501b8
4
+ data.tar.gz: e1466ef30d15e726cee9ab95b21cf3df287a0855f8f15896aac0affab095b6f0
5
5
  SHA512:
6
- metadata.gz: 2db23361ff2066cf6d38a797acbe669f0b68dce20cd314d49bc36b597c2f64bef0342f9fafd0644501508f6fef9ee1d433091e9a73d1286f14400a41493b24a7
7
- data.tar.gz: 2dcdb729c511035c5ad3018e32ff89a12a6ca9533291eb96eb47fab922c473ee3ac20a87da64f639dc2777a9216fbc8245dc819e7ff692c3a2978f8a0ad3ead7
6
+ metadata.gz: 7284a6584066c19a6130c53a1eeb13f7961118ea5f9bf73ed218b2d7097172c33f6322146b1a64b332112e2c8ba124e97eca2e6adaa308c0be67493d2f612bb5
7
+ data.tar.gz: 23561e9dbb1ce28a998c3d46dac97bcead9bd09d3ee44c9ae3159880777c058c4a427a3e5a7e82b983ad7c8b88115a14927d3a8301226ed68455151369660f43
data/README.md CHANGED
@@ -1,28 +1,79 @@
1
- # DataSnapshots
2
- Short description and motivation.
1
+ # data_snapshots
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ Simple, flexible data snapshotting for your Rails app.
6
4
 
7
5
  ## Installation
8
- Add this line to your application's Gemfile:
6
+ Run the following command to add `data_snapshots` to your Gemfile:
7
+ ``` shell
8
+ bundle add data_snapshots
9
+ ```
10
+ Once installed, copy and run the migration:
11
+ ```shell
12
+ bundle exec rails data_snapshots_engine:install:migrations
13
+ bundle exec rails db:migrate
14
+ ```
15
+ ## Registering a model snapshot
16
+ A model snapshot is a collection of methods that run against a model instance. Methods will be passed the instance when the snapshot is generated.
17
+
18
+ You can register your snapshots in an initializer:
9
19
 
10
20
  ```ruby
11
- gem 'data_snapshots'
21
+ DataSnapshots.configure do |c|
22
+ c.register_snapshot name: :user_assignments do |methods|
23
+ methods[:total] = ->(instance) { instance.assignments.count }
24
+ methods[:total_complete] = ->(instance) { instance.assignments.where(complete: true).count }
25
+ end
26
+ end
12
27
  ```
13
28
 
14
- And then execute:
15
- ```bash
16
- $ bundle
29
+ ## Registering a generic snapshot
30
+ A generic snapshot is a collection of methods that will not be passed a model instance when the snapshot is generated. You can declare a snapshot as generic by passing `model: false` as an argument when registering the snapshot:
31
+
32
+ ```ruby
33
+ DataSnapshots.configure do |c|
34
+ c.register_snapshot name: :totals, model: false do |methods|
35
+ methods[:total_users] = ->() { User.count }
36
+ methods[:total_assignments] = ->() { Assignments.count }
37
+ end
38
+ end
17
39
  ```
18
40
 
19
- Or install it yourself as:
20
- ```bash
21
- $ gem install data_snapshots
41
+ ## Generating model snapshots
42
+ Model snapshots can either be generated on the individual instance:
43
+ ```ruby
44
+ user = User.last
45
+ user.generate_snapshot(name: :user_assignments)
46
+ ```
47
+ Or you can generate them with a collection of records:
48
+ ```ruby
49
+ DataSnapshots.generate_snapshots(name: :user_assignments, collection: User.all)
22
50
  ```
23
51
 
24
- ## Contributing
25
- Contribution directions go here.
52
+ ## Generating generic snapshots
53
+ Generic snapshots can be created by passing the name of the snapshot to `DataSnapshots.generate_snapshot`
54
+ ```ruby
55
+ DataSnapshots.generate_snapshot(name: :total_users)
56
+ ```
57
+
58
+ ## Fetching snapshots
59
+ `data_snapshots` comes with a `<NAME>_snapshots` method so its easy to fetch all the snapshots taken against a particular instance:
60
+ ```ruby
61
+ user.user_assignments_snapshots
62
+ ```
63
+ You can fetch your generic snapshots by calling `DataSnapshots.fetch_snapshots` and passing the name of your snapshot:
64
+ ```ruby
65
+ DataSnapshots.fetch_snapshots(name: :total_users)
66
+ ```
67
+ This method can also be used to fetch _ALL_ model snapshots with the given name.
68
+
69
+ ## Viewing snapshot data
70
+
71
+ Snapshot data is stored as JSON in the database and can be accessed easily:
72
+
73
+ ```ruby
74
+ snapshot.data # => { 'key' => 'value' }
75
+ ```
26
76
 
27
77
  ## License
28
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,6 +1,12 @@
1
1
  require 'data_snapshots/engine'
2
2
  require 'data_snapshots/configuration'
3
3
 
4
+ class UnregisteredSnapshotError < StandardError
5
+ end
6
+
7
+ class IncompatibleSnapshotError < StandardError
8
+ end
9
+
4
10
  module DataSnapshots
5
11
  class << self
6
12
  attr_accessor :configuration
@@ -20,4 +26,31 @@ module DataSnapshots
20
26
  instance.generate_snapshot(name: name)
21
27
  end
22
28
  end
29
+
30
+ def self.generate_snapshot(name:)
31
+ snapshot = DataSnapshots.configuration.snapshots[name]
32
+
33
+ unless snapshot
34
+ raise UnregisteredSnapshotError.new("Snapshot: #{name} has not been registered")
35
+ end
36
+
37
+ if snapshot[:model]
38
+ raise IncompatibleSnapshotError.new(
39
+ "Snapshot: #{name} is a model snapshot, this method is for generic snapshots." \
40
+ "Try calling #generate_snapshot(name: #{name}) on an instance of the appropriate model." \
41
+ "Alternatively, call DataSnapshots.generate_snapshots(name: #{name}, collection: [Array of instances])"
42
+ )
43
+ end
44
+
45
+ data = {}
46
+ snapshot[:methods].each do |key, method|
47
+ data[key] = method.call()
48
+ end
49
+
50
+ DataSnapshots::Snapshot.create!(name: name, data: data)
51
+ end
52
+
53
+ def self.fetch_snapshots(name:)
54
+ DataSnapshots::Snapshot.where(name: name).order(:created_at)
55
+ end
23
56
  end
@@ -2,9 +2,6 @@
2
2
 
3
3
  require 'active_support/concern'
4
4
 
5
- class UnregisteredSnapshotError < StandardError
6
- end
7
-
8
5
  module DataSnapshots
9
6
  module ActiveRecordExtension
10
7
  extend ActiveSupport::Concern
@@ -8,9 +8,9 @@ module DataSnapshots
8
8
  @snapshots = {}
9
9
  end
10
10
 
11
- def register_snapshot(name:)
11
+ def register_snapshot(name:, model: true)
12
12
  return unless block_given?
13
- snapshots[name] = { methods: {} }
13
+ snapshots[name] = { methods: {}, model: model }
14
14
  yield snapshots[name][:methods]
15
15
  end
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module DataSnapshots
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_snapshots
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - k-p-jones
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-07 00:00:00.000000000 Z
11
+ date: 2021-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails