lite-regulation 1.0.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.
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,21 @@
1
+ en:
2
+ lite:
3
+ regulation:
4
+ activation:
5
+ active: 'Active'
6
+ inactive: 'Inactive'
7
+ containment:
8
+ contained: 'Contained'
9
+ uncontained: 'Uncontained'
10
+ expiration:
11
+ expired: 'Expired'
12
+ unexpired: 'Unexpired'
13
+ quarantine:
14
+ quarantined: 'Quarantined'
15
+ unquarantined: 'Unquarantined'
16
+ suspension:
17
+ suspended: 'Suspended'
18
+ unsuspended: 'Unsuspended'
19
+ visibility:
20
+ invisible: 'Invisible'
21
+ visible: 'Visible'
@@ -0,0 +1,41 @@
1
+ # Activation
2
+
3
+ #### Usage
4
+
5
+ ```ruby
6
+ # 1. Add column to table
7
+ class AddRegulationTimestampColumn < ActiveRecord::Migration
8
+ def change
9
+ t.datetime :inactivated_at
10
+
11
+ # - or -
12
+
13
+ add_column :your_model, :inactivated_at, :datetime
14
+ end
15
+ end
16
+
17
+ # 2. Include module
18
+ class User < ActiveRecord::Base
19
+ include Lite::Regulation::Activation
20
+ end
21
+ ```
22
+
23
+ #### Methods
24
+
25
+ ```ruby
26
+ user = User.first
27
+ user.inactive!
28
+ user.inactive? #=> true
29
+
30
+ user.active!
31
+ user.active? #=> true
32
+
33
+ user.to_activation #=> Returns the visibility state locale string (ex: Active)
34
+ ```
35
+
36
+ #### Scopes
37
+
38
+ ```ruby
39
+ User.inactive # Returns all inactive records
40
+ User.active # Returns all active records
41
+ ```
@@ -0,0 +1,41 @@
1
+ # Containment
2
+
3
+ #### Usage
4
+
5
+ ```ruby
6
+ # 1. Add column to table
7
+ class AddRegulationTimestampColumn < ActiveRecord::Migration
8
+ def change
9
+ t.datetime :contained_at
10
+
11
+ # - or -
12
+
13
+ add_column :your_model, :contained_at, :datetime
14
+ end
15
+ end
16
+
17
+ # 2. Include module
18
+ class User < ActiveRecord::Base
19
+ include Lite::Regulation::Containment
20
+ end
21
+ ```
22
+
23
+ #### Methods
24
+
25
+ ```ruby
26
+ user = User.first
27
+ user.uncontain!
28
+ user.uncontained? #=> true
29
+
30
+ user.contain!
31
+ user.contained? #=> true
32
+
33
+ user.to_containment #=> Returns the visibility state locale string (ex: Contained)
34
+ ```
35
+
36
+ #### Scopes
37
+
38
+ ```ruby
39
+ User.contained # Returns all contained records
40
+ User.uncontained # Returns all uncontained records
41
+ ```
@@ -0,0 +1,45 @@
1
+ # Expiration
2
+
3
+ #### Usage
4
+
5
+ ```ruby
6
+ # 1. Add column to table
7
+ class AddRegulationTimestampColumn < ActiveRecord::Migration
8
+ def change
9
+ t.datetime :expires_at
10
+
11
+ # - or -
12
+
13
+ add_column :your_model, :expires_at, :datetime
14
+ end
15
+ end
16
+
17
+ # 2. Include module
18
+ class User < ActiveRecord::Base
19
+ include Lite::Regulation::Expiration
20
+ end
21
+ ```
22
+
23
+ #### Methods
24
+
25
+ ```ruby
26
+ user = User.first
27
+ user.unexpire!
28
+ user.unexpired? #=> true
29
+
30
+ user.expire!
31
+ user.expired? #=> true
32
+
33
+ user.extend!
34
+ user.extend!(20.days)
35
+ user.expired? #=> false
36
+
37
+ user.to_expiration #=> Returns the visibility state locale string (ex: Expired)
38
+ ```
39
+
40
+ #### Scopes
41
+
42
+ ```ruby
43
+ User.expired # Returns all expired records
44
+ User.unexpired # Returns all unexpired records
45
+ ```
@@ -0,0 +1,41 @@
1
+ # Quarantine
2
+
3
+ #### Usage
4
+
5
+ ```ruby
6
+ # 1. Add column to table
7
+ class AddRegulationTimestampColumn < ActiveRecord::Migration
8
+ def change
9
+ t.datetime :quarantined_at
10
+
11
+ # - or -
12
+
13
+ add_column :your_model, :quarantined_at, :datetime
14
+ end
15
+ end
16
+
17
+ # 2. Include module
18
+ class User < ActiveRecord::Base
19
+ include Lite::Regulation::Quarantine
20
+ end
21
+ ```
22
+
23
+ #### Methods
24
+
25
+ ```ruby
26
+ user = User.first
27
+ user.unquarantine!
28
+ user.unquarantined? #=> true
29
+
30
+ user.quarantine!
31
+ user.quarantined? #=> true
32
+
33
+ user.to_quarantine #=> Returns the visibility state locale string (ex: Quarantined)
34
+ ```
35
+
36
+ #### Scopes
37
+
38
+ ```ruby
39
+ User.unquarantined # Returns all unquarantined records
40
+ User.quarantined # Returns all quarantined records
41
+ ```
@@ -0,0 +1,41 @@
1
+ # Suspension
2
+
3
+ #### Usage
4
+
5
+ ```ruby
6
+ # 1. Add column to table
7
+ class AddRegulationTimestampColumn < ActiveRecord::Migration
8
+ def change
9
+ t.datetime :suspended_at
10
+
11
+ # - or -
12
+
13
+ add_column :your_model, :suspended_at, :datetime
14
+ end
15
+ end
16
+
17
+ # 2. Include module
18
+ class User < ActiveRecord::Base
19
+ include Lite::Regulation::Suspension
20
+ end
21
+ ```
22
+
23
+ #### Methods
24
+
25
+ ```ruby
26
+ user = User.first
27
+ user.unsuspend!
28
+ user.unsuspended? #=> true
29
+
30
+ user.suspend!
31
+ user.suspended? #=> true
32
+
33
+ user.to_suspension #=> Returns the visibility state locale string (ex: Suspended)
34
+ ```
35
+
36
+ #### Scopes
37
+
38
+ ```ruby
39
+ User.unsuspended # Returns all unsuspended records
40
+ User.suspended # Returns all suspended records
41
+ ```
@@ -0,0 +1,41 @@
1
+ # Visibility
2
+
3
+ #### Usage
4
+
5
+ ```ruby
6
+ # 1. Add column to table
7
+ class AddRegulationTimestampColumn < ActiveRecord::Migration
8
+ def change
9
+ t.datetime :invisible_at
10
+
11
+ # - or -
12
+
13
+ add_column :your_model, :invisible_at, :datetime
14
+ end
15
+ end
16
+
17
+ # 2. Include module
18
+ class User < ActiveRecord::Base
19
+ include Lite::Regulation::Visibility
20
+ end
21
+ ```
22
+
23
+ #### Methods
24
+
25
+ ```ruby
26
+ user = User.first
27
+ user.invisible!
28
+ user.invisible? #=> true
29
+
30
+ user.visible!
31
+ user.visible? #=> true
32
+
33
+ user.to_visibility #=> Returns the visibility state locale string (ex: Visible)
34
+ ```
35
+
36
+ #### Scopes
37
+
38
+ ```ruby
39
+ User.invisible # Returns all invisible records
40
+ User.visible # Returns all visible records
41
+ ```
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_record'
4
+ require 'active_support'
5
+
6
+ require 'lite/regulation/version'
7
+ require 'lite/regulation/railtie' if defined?(Rails)
8
+
9
+ %w[base activation containment expiration quarantine suspension visibility].each do |name|
10
+ require "lite/regulation/#{name}"
11
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Regulation
5
+ module Activation
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ scope :active, -> { where(inactivated_at: nil) }
11
+ scope :inactive, -> { where.not(inactivated_at: nil) }
12
+ end
13
+
14
+ def active!
15
+ return true if active?
16
+
17
+ update(inactivated_at: nil)
18
+ end
19
+
20
+ def active?
21
+ inactivated_at.nil?
22
+ end
23
+
24
+ def inactive!
25
+ return true if inactive?
26
+
27
+ update(inactivated_at: Time.current)
28
+ end
29
+
30
+ def inactive?
31
+ !active?
32
+ end
33
+
34
+ def inactivated_at_or_time
35
+ return inactivated_at if inactive?
36
+
37
+ Lite::Regulation::Base.timestamp
38
+ end
39
+
40
+ def to_activation
41
+ I18n.t("lite.regulation.activation.#{:in if inactive?}active")
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Regulation
5
+ module Base
6
+
7
+ def self.timestamp
8
+ Time.respond_to?(:current) ? Time.current : Time.now
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Regulation
5
+ module Containment
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ scope :contained, -> { where.not(contained_at: nil) }
11
+ scope :uncontained, -> { where(contained_at: nil) }
12
+ end
13
+
14
+ def contain!
15
+ return true if contained?
16
+
17
+ update(contained_at: Time.current)
18
+ end
19
+
20
+ def contained?
21
+ !uncontained?
22
+ end
23
+
24
+ def contained_at_or_time
25
+ return contained_at if contained?
26
+
27
+ Lite::Regulation::Base.timestamp
28
+ end
29
+
30
+ def to_containment
31
+ I18n.t("lite.regulation.containment.#{uncontained? ? :uncontained : :contained}")
32
+ end
33
+
34
+ def uncontain!
35
+ return true if uncontained?
36
+
37
+ update(contained_at: nil)
38
+ end
39
+
40
+ def uncontained?
41
+ contained_at.nil?
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Regulation
5
+ module Expiration
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ # rubocop:disable Style/Lambda
10
+ included do
11
+ scope :expired, -> do
12
+ where('expires_at IS NULL OR expires_at < ?', Lite::Regulation::Base.timestamp)
13
+ end
14
+ scope :unexpired, -> do
15
+ where('expires_at IS NOT NULL AND expires_at >= ?', Lite::Regulation::Base.timestamp)
16
+ end
17
+ end
18
+ # rubocop:enable Style/Lambda
19
+
20
+ def expire!
21
+ return true if expires_at.nil?
22
+
23
+ update(expires_at: nil)
24
+ end
25
+
26
+ def expired?
27
+ return true if expires_at.nil?
28
+
29
+ Lite::Regulation::Base.timestamp >= expires_at
30
+ end
31
+
32
+ def extend!(amount = nil)
33
+ update(expires_at: extension_date(amount))
34
+ end
35
+
36
+ def unexpire!
37
+ return true unless expires_at.nil?
38
+
39
+ update(expires_at: extension_date)
40
+ end
41
+
42
+ def unexpired?
43
+ return false if expires_at.nil?
44
+
45
+ Lite::Regulation::Base.timestamp < expires_at
46
+ end
47
+
48
+ def expires_at_or_time(amount = nil)
49
+ return expires_at if unexpired?
50
+
51
+ extension_date(amount)
52
+ end
53
+
54
+ def to_expiration
55
+ I18n.t("lite.regulation.expiration.#{:un if unexpired?}expired")
56
+ end
57
+
58
+ private
59
+
60
+ def extension_date(amount = nil)
61
+ amount = 30 if amount.nil?
62
+ return amount unless amount.is_a?(Integer)
63
+
64
+ Lite::Regulation::Base.timestamp + amount
65
+ end
66
+
67
+ end
68
+ end
69
+ end