remont 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ffe65eff0fe907d7834a94e028001396ad17937b313cb9197475b66c4938c33
4
- data.tar.gz: 7247441b4f59709a8c877da6ae9a9679f27c02916ca553f0f7f9d9f2808c9805
3
+ metadata.gz: cc06eb6d1dddd55ec43542c7ce574c3e4b2f775230d4005aa4e72a2352ce24df
4
+ data.tar.gz: 403813fb176d0c028075298808a897f149de6116bacb89e7bf92dce5633d743f
5
5
  SHA512:
6
- metadata.gz: 60ae2dc780851f4bef7436e65b2143cfd5b8454b2cd5f3dc512985f32ab061f59eed24ff769e34fc26adabe43a7346aaf411b824d8e9979153b7fac342792e05
7
- data.tar.gz: 745d82eac997e1bda274f2e5fc20f3f6b10b69043efe0de83746804725fea5b7a5b5bf99c585d9f866ca88e6bd9d1c1a901855c6b7cc034f4ebf1846b56e9446
6
+ metadata.gz: 71af67ff58ba821f6c12ca67ceb038024c88536afbeb1173c7cce0b34cca9fca63da47e9543932a2f744f508fb8c1aca7c4656ef9706c188956f658689ed1244
7
+ data.tar.gz: bf33a5f1ccbf9b88c0ce06852ec12fda9898c0186dfe1f5cb5a09408f680cb4e04eade3519791f1ac6dae5fd850105fb92ba645ec9b18bca2a4838126d5c7cdb
data/CHANGELOG.md CHANGED
@@ -0,0 +1,6 @@
1
+ # Remont Change Log
2
+
3
+ ## 0.1.1 (Mar 23rd, 2022)
4
+
5
+ - Add support for adding script path argument to the rake task (@cukoreva)
6
+ - Add support for configuring processing status attribute per schema (@vr4b4c)
data/README.md CHANGED
@@ -21,14 +21,12 @@ Add `config/initializers/remont.rb`
21
21
  ```ruby
22
22
  Remont.setup do |config|
23
23
  config.process_timestamp_attribute = :processed_at
24
- config.script_path = 'db/remont.rb'
25
24
  end
26
25
  ```
27
26
 
28
- | Option | Default value | Description |
29
- | --- | :---: | --- |
30
- | `process_timestamp_attribute` | `:processed_at` | processing status attribute identifier |
31
- | `script_path` | `'db/remont.rb'` | entry point for the schema processing task |
27
+ | Option | Default value | Description |
28
+ | --- | :---: | --- |
29
+ | `process_timestamp_attribute` | `nil` | processing status attribute identifier |
32
30
 
33
31
  On successful record processing, `process_timestamp_attribute` on the record will be set to the current processing time (`Time.now.getlocal`).
34
32
 
@@ -38,11 +36,10 @@ In the following example, the intention is to simulate anonymization of the `use
38
36
  ```ruby
39
37
  # config/initializers/remont.rb
40
38
  Remont.setup do |config|
41
- config.process_timestamp_attribute = :anonyzmied_at
42
- config.script_path = 'db/anonymize.rb'
39
+ config.process_timestamp_attribute = :anonymized_at
43
40
  end
44
41
  ```
45
- - define processing script
42
+ - define the processing script
46
43
  ```ruby
47
44
  # db/anonymize.rb
48
45
  schema model: User do
@@ -53,8 +50,12 @@ schema model: Order do
53
50
  attribute(:billing_address) { '23 Wall Street, NY' }
54
51
  end
55
52
  ```
56
- - and run `bundle exec rake remont`
53
+ - and run `remont` rake task with the path to the processing script
54
+ ```bash
55
+ bundle exec rake "remont[db/anonymize.rb]"
56
+ ```
57
57
 
58
+ Passing the script path to the rake task is mandatory.
58
59
  Running the rake task would result in updating `email` (and `anonymized_at`) column for each row in the `users` table, and `billing_address` column for each row in the `orders` table.
59
60
 
60
61
  ### Schema
@@ -77,14 +78,29 @@ schema model: Order do
77
78
  scope { |scope| scope.where(status: :active) }
78
79
  end
79
80
  ```
81
+ ### Recording the processing end time
82
+ Library supports an option to store the processing end time of a record. Enable the behavior by configuring a processing status attribute identifier for the schema. Configure the attribute identifier
83
+ - globally (`Remont::Config#process_timestamp_attribute`, for all schemas)
84
+ - or individually per schema (in the schema DSL, overrides the global setting)
85
+ ```ruby
86
+ schema model: User, process_timestamp_attribute: :anonymized_at do
87
+ end
88
+
89
+ schema model: Order do
90
+ with_process_timestamp_attribute :anonymized_at
91
+ end
92
+ ```
93
+
94
+ Set process status attribute to `nil` to disable the behavior.
80
95
  ### Skipping the processed records
81
- In some cases, it's desirable to skip already processed records. You can opt-in to this behavior by declaring `without_processed` within the `schema` block. When declared, the processing dataset query will be extended with a condition that excludes already processed records (ie. `where(Remont.process_timestamp_attribute => nil)`)
96
+ In some cases, it's desirable to skip already processed records. You can enable this behavior by declaring `without_processed` within the `schema` block. When declared, the processing dataset query will be extended with a condition that excludes already processed records.
82
97
  ```ruby
83
98
  schema model: User do
84
99
  without_processed
85
100
  # ...
86
101
  end
87
102
  ```
103
+ Configure processing status attribute before declaring the `without_processed`. An error will be raised otherwise.
88
104
  ### Callbacks
89
105
  Custom pre-processing or post-processing behavior can be declared using `before` and `after` callbacks.
90
106
  ```ruby
data/lib/remont/config.rb CHANGED
@@ -1,16 +1,10 @@
1
1
  module Remont
2
2
  class Config
3
- DEFAULT_SCRIPT_PATH = 'db/remont.rb'.freeze
4
-
5
3
  # @return [Symbol]
6
4
  attr_accessor :process_timestamp_attribute
7
5
 
8
- # @return [String]
9
- attr_accessor :script_path
10
-
11
6
  def initialize
12
- @process_timestamp_attribute = :processed_at
13
- @script_path = DEFAULT_SCRIPT_PATH
7
+ @process_timestamp_attribute = nil
14
8
  end
15
9
  end
16
10
  end
@@ -2,11 +2,11 @@ module Remont
2
2
  class RecordProcessor
3
3
  NOOP = proc {}
4
4
 
5
- # @param [Array<Remont::Attribute>] attributes
5
+ # @param [Remont::Schema] schema
6
6
  # @param [Proc] before
7
7
  # @param [Proc] after
8
- def initialize(attributes, before, after)
9
- @attributes = attributes
8
+ def initialize(schema, before, after)
9
+ @schema = schema
10
10
  @before = before || NOOP
11
11
  @after = after || NOOP
12
12
  end
@@ -25,12 +25,13 @@ module Remont
25
25
 
26
26
  private
27
27
 
28
- attr_reader :attributes
28
+ attr_reader :schema
29
29
  attr_reader :before
30
30
  attr_reader :after
31
31
 
32
32
  def processed_attributes(record)
33
- attributes
33
+ schema
34
+ .attributes
34
35
  .select { |attribute| record.send(attribute.name).present? }
35
36
  .reduce(default_processed_attributes) do |memo, attribute|
36
37
  memo.merge(attribute.name => attribute.process(record.send(attribute.name), record))
@@ -38,10 +39,10 @@ module Remont
38
39
  end
39
40
 
40
41
  def default_processed_attributes
41
- return {} unless Remont.config.process_timestamp_attribute
42
+ return {} unless schema.process_timestamp_attribute
42
43
 
43
44
  {
44
- Remont.config.process_timestamp_attribute => Time.now.getlocal
45
+ schema.process_timestamp_attribute => Time.now.getlocal
45
46
  }
46
47
  end
47
48
  end
data/lib/remont/schema.rb CHANGED
@@ -1,16 +1,23 @@
1
1
  module Remont
2
2
  class Schema
3
- DEFAULT_SCOPE = proc { |scope| scope }
4
- WITHOUT_PROCESSED = proc { |scope| scope.where(Remont.config.process_timestamp_attribute => nil) }
3
+ MISSING_PROCESSING_STATUS_ATTR = "Processing status attribute isn't configured".freeze
4
+
5
+ # @return [Array<Remont::Attribute>]
6
+ attr_reader :attributes
7
+
8
+ # @return [Nil, String, Symbol]
9
+ attr_reader :process_timestamp_attribute
5
10
 
6
11
  # @param [Hash] opts
7
12
  # @option [Class] :model
8
13
  # @option [Proc] :scope
14
+ # @option [String, Symbol] :process_timestamp_attribute
9
15
  # @param [Proc] block
10
16
  def initialize(opts = {}, &block)
11
17
  @model = opts.fetch(:model)
12
- @model_scope = opts.fetch(:scope, DEFAULT_SCOPE)
13
- @without_processed_scope = DEFAULT_SCOPE
18
+ @model_scope = opts.fetch(:scope, default_scope)
19
+ @without_processed_scope = default_scope
20
+ @process_timestamp_attribute = opts.fetch(:process_timestamp_attribute, Remont.config.process_timestamp_attribute)
14
21
  @before_cb = nil
15
22
  @after_cb = nil
16
23
  @attributes = []
@@ -20,7 +27,17 @@ module Remont
20
27
 
21
28
  # @return [Remont::Schema]
22
29
  def without_processed
23
- @without_processed_scope = WITHOUT_PROCESSED
30
+ raise MISSING_PROCESSING_STATUS_ATTR if @process_timestamp_attribute.nil?
31
+
32
+ @without_processed_scope = proc { |scope| scope.where(process_timestamp_attribute => nil) }
33
+
34
+ self
35
+ end
36
+
37
+ # @param [String, Symbol] attr_name
38
+ # @return [Remont::Schema]
39
+ def with_process_timestamp_attribute(attr_name)
40
+ @process_timestamp_attribute = attr_name
24
41
 
25
42
  self
26
43
  end
@@ -70,12 +87,11 @@ module Remont
70
87
  attr_reader :model
71
88
  attr_reader :without_processed_scope
72
89
  attr_reader :model_scope
73
- attr_reader :attributes
74
90
  attr_reader :before_cb
75
91
  attr_reader :after_cb
76
92
 
77
93
  def processor
78
- @processor ||= RecordProcessor.new(attributes, before_cb, after_cb)
94
+ @processor ||= RecordProcessor.new(self, before_cb, after_cb)
79
95
  end
80
96
 
81
97
  def records_for_processing
@@ -83,5 +99,9 @@ module Remont
83
99
  without_processed_scope.call(model.all)
84
100
  )
85
101
  end
102
+
103
+ def default_scope
104
+ proc { |scope| scope }
105
+ end
86
106
  end
87
107
  end
@@ -1,5 +1,13 @@
1
1
  desc 'Run schema processing'
2
- task remont: :environment do
3
- path = Rails.root.join(Remont.config.script_path)
2
+ task :remont, [:script_path] => :remont_env do |_, args|
3
+ script_path = args[:script_path]
4
+ abort 'Missing script path' unless script_path
5
+
6
+ path = Rails.root.join(script_path)
4
7
  Remont::Script.new(path).run!
5
8
  end
9
+
10
+ task :remont_env do # rubocop:disable Rails/RakeEnvironment
11
+ Rake::Task['environment'].invoke
12
+ rescue StandardError # rubocop:disable Lint/SuppressedException
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Remont
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: remont
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
  - Rails Team
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-08 00:00:00.000000000 Z
11
+ date: 2022-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord