active_record_callbacks_debug 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: db618b8aa216496725ac548faac94ab5e9f04afa66a3234e4c6902a6fb99b431
4
+ data.tar.gz: 179ef36266140fd2fa79692b53a860a134d0c62b5da8f0366d80d9122ca93d3b
5
+ SHA512:
6
+ metadata.gz: 307f42bb1548d2b7b2033a8b062a4415e71571384ec46cf27b24126a6ffa01e7fae4290011a079efed990d15942018cb3822c535b551928eb42267867b1cad40
7
+ data.tar.gz: 7a631ba0267ff06117ee91132354c8583ba26fe625e911db0a9d46cd69df7532381cf91b0dd5935db8d1ee335d3490a964f3150468b857e183987baf7f13312f
@@ -0,0 +1,93 @@
1
+ require 'active_support/all'
2
+ require 'benchmark'
3
+ require 'active_record_callbacks_debug/configuration.rb'
4
+
5
+ module ActiveRecordCallbacksDebug
6
+ class << self
7
+ attr_accessor :configuration
8
+
9
+ def logger
10
+ configuration.logger || Logger.new
11
+ end
12
+
13
+ def log(params, bench=nil)
14
+ type = define_type params
15
+ klass = klass params[:target]
16
+ msg = "#{type.upcase}: #{klass} => "
17
+
18
+ case type
19
+ when :proc
20
+ msg += "#{params[:over_method]} => proc: #{params[:over_block]}"
21
+ when :val
22
+ msg += "#{params[:over_target].class} > attr: #{params[:over_target].attributes}"
23
+ when :meth
24
+ msg += params[:over_method].to_s
25
+ end
26
+
27
+ msg = "#{bench_format(bench)}#{msg}"
28
+ logger.debug msg
29
+ msg
30
+ end
31
+
32
+ def define_type(params)
33
+ return :proc if params[:over_block].present?
34
+ return :val if params[:over_method] == 'validate'
35
+ return :meth if params[:over_method].present?
36
+ end
37
+
38
+ def klass(klass_source)
39
+ klass_source.class.to_s
40
+ end
41
+
42
+ def bench_format(bench)
43
+ return "#{format('%.6f', bench.total)} seg | " if bench.present?
44
+
45
+ ''
46
+ end
47
+
48
+ def configuration
49
+ @configuration ||= ActiveRecordCallbacksDebug::Configuration.new
50
+ end
51
+
52
+ def reset
53
+ @configuration = ActiveRecordCallbacksDebug::Configuration.new
54
+ end
55
+
56
+ def configure
57
+ yield(configuration)
58
+ end
59
+ end
60
+
61
+ module CallTemplateExtension
62
+ def expand(target, value, block)
63
+ if ::ActiveRecordCallbacksDebug.configuration.active
64
+ result = nil
65
+ params = {
66
+ target: target,
67
+ value: value,
68
+ block: block,
69
+ over_target: @override_target,
70
+ over_method: @method_name,
71
+ over_block: @override_block
72
+ }
73
+ bench = Benchmark.measure { result = super(target, value, block) }
74
+ ::ActiveRecordCallbacksDebug.log(params, bench)
75
+ result
76
+ else
77
+ super(target, value, block)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ module ActiveSupport
84
+ module Callbacks
85
+ if ::ActiveSupport.gem_version >= ::Gem::Version.new('5.1.0')
86
+ class CallTemplate
87
+ prepend ::ActiveRecordCallbacksDebug::CallTemplateExtension
88
+ end
89
+ else
90
+ Exception.new 'Not work for rails 5.1 <'
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,10 @@
1
+ module ActiveRecordCallbacksDebug
2
+ class Configuration
3
+ attr_accessor :active, :logger
4
+
5
+ def initialize
6
+ @active = false
7
+ @logger = Logger.new($stdout)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordCallbacksDebug
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_record_callbacks_debug
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Juan Manuel Jurado
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.1'
48
+ - - "<"
49
+ - !ruby/object:Gem::Version
50
+ version: '6.0'
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '5.1'
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: '6.0'
61
+ description:
62
+ email:
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - lib/active_record_callbacks_debug.rb
68
+ - lib/active_record_callbacks_debug/configuration.rb
69
+ - lib/active_record_callbacks_debug/version.rb
70
+ homepage:
71
+ licenses:
72
+ - MIT
73
+ metadata: {}
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 2.7.6.2
91
+ signing_key:
92
+ specification_version: 4
93
+ summary: Debug callbacks or hook in Active Record
94
+ test_files: []