cleanse 0.1.0 → 0.1.2

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: b90083299b31f490bb5e4b6aa9ad215f13de7bec7389024162cb4c8cf14935b1
4
- data.tar.gz: 3d3d1b2b3053f6531b00d79a2855704bda544cef9576141ccf92299600e040b3
3
+ metadata.gz: 48ff8467e9ded2005f428c6fc11379660366a9756fb7cb3aafd9c36edaabc771
4
+ data.tar.gz: 346188d6031d41a26745814546aa9e88b1b4f2935c7cb1209b6c05ad0e2b5bbc
5
5
  SHA512:
6
- metadata.gz: 3591c649946baedd6ac927bd9e3f51d75f94214c3b2cc426b2fa86fbc6b0284425e85e15f7cda46b3fb6bfa7122e3340942246e4c2d1c6f2a6188ecc0bbc622a
7
- data.tar.gz: 93e53d2719c236e5fd23f7924a226852977d53e85ae2a575a932bd36ea1b898423c950240c2708eee0efa25d21de9b8534e5eb5b938be12c75948d88f271fc70
6
+ metadata.gz: 47d7f5f084fce8354a29af2f280ede64b8116c6f8dad63ccf865eb3c7bc929361aa8610dd279d9433ecd79f69b0a062ccb19da35fe3580426aaace547d74fcef
7
+ data.tar.gz: 8f782d65d080664578721bc5e9b1d83097fec0103d020b91625258c328e5190ca84ed0213627df5859d88ad2da7d94bb69da8f1eba5c2f03412b134c798cd4d0
data/CHANGELOG.md CHANGED
@@ -3,3 +3,7 @@
3
3
  ## [0.1.0] - 2025-05-04
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2025-05-04
8
+
9
+ - Auto cleaning rails models
data/README.md CHANGED
@@ -0,0 +1,22 @@
1
+ # Cleanse
2
+
3
+ Cleanse influences you to create modules and include them dynamically in your classes in order to keep them clean.
4
+
5
+ ## Usage
6
+ Create modules with the same name as the class you want to clean up and, finally, include our module with the `include Cleanse::Cleaner` directive in the class.
7
+
8
+ In the case of Rails applications, all the modules defined in `app/models/concerns` will be included automatically in their respective models. For this to work properly, make sure you enable eager load in each environment in the `config/environments` folder:
9
+
10
+ ```config.eager_load = true```
11
+
12
+ ## Purpose
13
+
14
+ Imagine a fat product model with countless lines of code. In order to reduce it to just the class definition line (`class Product; end`), we encourage you to create descriptive modules with the same name as your class. `Cleanse::Cleaner` will include all these modules and your code will be more concise, objective and maintainable.
15
+
16
+ - `app/models/product.rb` -> The cleaned class;
17
+ - `app/models/concerns/product/associations.rb` -> The code containing the product model associations;
18
+ - `app/models/concerns/product/callbacks.rb` -> The active record callbacks for the product model;
19
+ - `app/models/concerns/product/?.rb` -> Whatever you want, it will be included automatically.
20
+
21
+ ## Questions/suggestions/contributions
22
+ Feel free to create issues or pull request in this repository.
@@ -7,8 +7,14 @@ module Cleanse
7
7
  end
8
8
 
9
9
  def self.clean!(cleanable_class)
10
- cleanable_class.constants.each do |constant|
11
- cleanable_class.include(constant) if !cleanable_class.abstract_class? && constant.is_a?(Module)
10
+ return if cleanable_class.abstract_class?
11
+
12
+ includable_modules = cleanable_class.constants.map do |constant|
13
+ cleanable_class.const_get(constant)
14
+ end
15
+
16
+ includable_modules.each do |constant|
17
+ cleanable_class.include(constant) if constant.instance_of?(Module)
12
18
  end
13
19
  end
14
20
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cleanse
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/cleanse.rb CHANGED
@@ -4,17 +4,23 @@ require_relative 'cleanse/version'
4
4
  require_relative 'cleanse/cleaner'
5
5
 
6
6
  module Cleanse
7
- if defined?(ActiveRecord::Base)
8
- parent_classes = children_of(ActiveRecord::Base).flat_map do |parent_class|
9
- children_of(parent_class)
10
- end
7
+ def self.children_of(parent_class)
8
+ parent_class.descendants
9
+ end
11
10
 
12
- parent_classes.each do |parent_class|
13
- parent_class.include(Cleanse::Cleaner)
11
+ if defined?(ActiveSupport)
12
+ ActiveSupport.on_load(:after_initialize) do
13
+ Cleanse.clean_rails_models!
14
14
  end
15
15
  end
16
16
 
17
- def children_of(parent_class)
18
- parent_class.abstract_class? ? [] : parent.subclasses
17
+ def self.clean_rails_models!
18
+ parent_class = 'ApplicationRecord'.safe_constantize || 'ActiveRecord::Base'.safe_constantize || nil
19
+ return unless parent_class
20
+
21
+ cleanable_classes = Cleanse.children_of(parent_class)
22
+ cleanable_classes.each do |cleanable_class|
23
+ cleanable_class.include(Cleanse::Cleaner)
24
+ end
19
25
  end
20
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cleanse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Douglas Vitor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-05-04 00:00:00.000000000 Z
11
+ date: 2025-05-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Cleanse influences you to create modules and include them dynamically
14
14
  in your classes in order to keep them clean.
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.3.7
54
+ rubygems_version: 3.5.11
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: Clean your fat ruby classes.