order_cop 0.1.2 → 0.2.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: 9844113f7fde924bc0804ffd7a8a3e81b034e1009e2b8e399538e791756f1ba6
4
- data.tar.gz: 71231522bb6415cb282945f68efcf58acc66d8f5400a70cbc039a076c49877ce
3
+ metadata.gz: f04b2f958bd26504fa02f2a8c7933b34f6b2f32510c7bdd039741b968b4cf828
4
+ data.tar.gz: 2a5ce657241968b7fa4b3710efdd3171bab6b6f79371686985cf63afd440cfe4
5
5
  SHA512:
6
- metadata.gz: 874cf90c0aefe66aa65a936bf2f48ee2fb2b383c41d5b137af448a5fafbabd5c138940254481ae27a36ea59c004692ca5d75cdc6e61104a6cebd81dfa676226f
7
- data.tar.gz: ca59878e2544b486200256d224b5fc99ec0433ee134ea2617f46db658880f3860c42e316391b77afd332b7077fe073d51cd95db2758aaccaf2e5ba813c944cf7
6
+ metadata.gz: 33bc928727bb612bc378e8627a1f768692db2186311ae13099751ce386867b5d91af8fc2c7d022c60a9670397e9f13809ffd3774dfd481e9eddeeeaffb5392bd
7
+ data.tar.gz: 29661cd6a332cf92870f5d7fe5b2283483a915f3ef6654159c6f56abaa492332adaefa5730ec6721463a6cb828b889cabf963989829f5047a6d98421901af95b
data/Gemfile CHANGED
@@ -12,4 +12,5 @@ gem "rspec", "~> 3.0"
12
12
  gem "standard", "~> 1.3"
13
13
 
14
14
  gem "rails", "~> 7.0.0"
15
+
15
16
  gem "sqlite3"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- order_cop (0.1.2)
4
+ order_cop (0.2.0)
5
5
  binding_of_caller (~> 1.0)
6
6
  rails (~> 7.0)
7
7
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OrderCop
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/order_cop.rb CHANGED
@@ -11,6 +11,42 @@ require "zeitwerk"
11
11
  loader = Zeitwerk::Loader.for_gem
12
12
  loader.setup
13
13
  module OrderCop
14
+ class OrderCopConfig
15
+ def initialize
16
+ @enabled = true
17
+ @raise = true
18
+ @debug = false
19
+ @rails_logger = false
20
+ @whitelist_methods = %i[sum any? none? inspect method_missing not load_target reindex attachment attachments attributes_table with_current_arbre_element]
21
+ end
22
+ attr_accessor :enabled, :raise, :debug, :rails_logger, :whitelist_methods
23
+ end
24
+
25
+ def self.config(**options, &block)
26
+ @config ||= OrderCopConfig.new
27
+ options.each do |key, value|
28
+ @config.send("#{key}=", value)
29
+ end
30
+ yield @config if block
31
+ @config
32
+ end
33
+
34
+ def self.disable(&block)
35
+ old_enabled = @config.enabled
36
+ @config.enabled = false
37
+ yield
38
+ ensure
39
+ @config.enabled = old_enabled
40
+ end
41
+
42
+ def self.disabled?
43
+ !config.enabled
44
+ end
45
+
46
+ def self.enabled?
47
+ config.enabled
48
+ end
49
+
14
50
  module OrderCopMixin
15
51
  # patch all methods which iterate over the collection and return several records
16
52
  # we don't patch `first` and `last`, take or others because they return a single record)
@@ -59,9 +95,9 @@ module OrderCop
59
95
  lbinding = binding.of_caller(i)
60
96
  lmethod = lbinding.eval("__method__")
61
97
  next if lmethod.nil?
62
- puts "lmethod: #{lmethod}" if OrderCop.debug?
63
- if OrderCop::WHITELIST.include?(lmethod)
64
- puts "#{lmethod} is whitelisted, ignoring" if OrderCop.debug?
98
+ puts "lmethod: #{lmethod}" if OrderCop.config.debug
99
+ if OrderCop.config.whitelist_methods.include?(lmethod)
100
+ puts "#{lmethod} is whitelisted, ignoring" if OrderCop.config.debug
65
101
  return true
66
102
  end
67
103
  end
@@ -70,8 +106,8 @@ module OrderCop
70
106
 
71
107
  def detect_missing_order(method)
72
108
  return if OrderCop.disabled?
73
- puts "missing order, detect if allowed" if OrderCop.debug?
74
- puts "stack size: #{binding.frame_count}" if OrderCop.debug?
109
+ puts "missing order, detect if allowed" if OrderCop.config.debug
110
+ puts "stack size: #{binding.frame_count}" if OrderCop.config.debug
75
111
 
76
112
  return if stack_is_whitelisted?
77
113
 
@@ -90,42 +126,22 @@ module OrderCop
90
126
  msg = "Missing Order for :#{method}"
91
127
  end
92
128
 
93
- if OrderCop.rails_logger?
94
- Rails.logger.error order_cop_red("Missing order for #{method}")
129
+ red = ->(msg) { ActiveSupport::LogSubscriber.new.send(:color, msg, :red) }
130
+
131
+ if OrderCop.config.rails_logger
132
+ Rails.logger.error red.call(msg)
95
133
  caller.each do |line|
96
- Rails.logger.error order_cop_red(" #{line}") if line.include?(Rails.root.to_s)
134
+ Rails.logger.error red.call(" #{line}") if line.include?(Rails.root.to_s)
97
135
  end
98
136
  end
99
- if OrderCop.raise?
137
+ if OrderCop.config.raise
100
138
  raise OrderCop::Error.new(msg)
101
139
  end
102
140
  end
103
-
104
- def order_cop_red(msg)
105
- ActiveSupport::LogSubscriber.new.send(:color, msg, :red)
106
- end
107
141
  end
108
142
 
109
143
  class Error < StandardError; end
110
144
 
111
- WHITELIST = %i[sum any? none? inspect method_missing not load_target reindex attachment attachments attributes_table with_current_arbre_element].freeze
112
-
113
- def self.disable(&block)
114
- old_enabled = @enabled
115
- @enabled = false
116
- yield
117
- ensure
118
- @enabled = old_enabled
119
- end
120
-
121
- def self.disabled?
122
- @enabled == false
123
- end
124
-
125
- def self.enabled?
126
- @enabled != false
127
- end
128
-
129
145
  def self.patch_active_record(app)
130
146
  ActiveRecord::Base.descendants.each do |model|
131
147
  model.const_get(:ActiveRecord_Associations_CollectionProxy).class_eval do
@@ -140,31 +156,6 @@ module OrderCop
140
156
  end
141
157
  end
142
158
 
143
- def self.rails_logger?
144
- config.rails_logger
145
- end
146
-
147
- def self.raise?
148
- config.raise
149
- end
150
-
151
- def self.debug?
152
- config.debug
153
- end
154
-
155
- def self.config(**options)
156
- @config ||= OpenStruct.new(rails_logger: false, raise: true, enabled: true, debug: false)
157
- options.each do |k, v|
158
- @config[k] = v
159
- end
160
- @enabled = @config.enabled
161
- @config
162
- end
163
-
164
- def self.setup(**options)
165
- config(**options)
166
- end
167
-
168
159
  def self.apply(app = Rails.application)
169
160
  patch_active_record(app) if enabled?
170
161
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: order_cop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gauthier Monserand
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-16 00:00:00.000000000 Z
11
+ date: 2023-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails