snip_snip 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +6 -18
- data/lib/snip_snip.rb +2 -2
- data/lib/snip_snip/filter.rb +9 -13
- data/lib/snip_snip/railtie.rb +2 -2
- data/lib/snip_snip/registry.rb +21 -7
- data/lib/snip_snip/reporter.rb +22 -12
- data/lib/snip_snip/version.rb +2 -2
- metadata +15 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 062261f05f636bb28b9196d5c084d17cfa4b31c6
|
4
|
+
data.tar.gz: 10bca413ac95b6bcf7fb6b6b7fddf9f01ed04e88
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fdaf95d1674c764032cad5706d195c8842b19badf348fa16d81ca1edeed25beaed99fa881eefc56692c3be566e8cca84b12f53a4c6bbbc1cf61af68717c595d
|
7
|
+
data.tar.gz: 7911d7040597f852ca55a1c6b74e48f68b19a58c0d8fd0ee4c0987145300183f32cbb0d87190b6492726cb21809db7a4e6d599634740900b4a2723bc650394b3
|
data/Rakefile
CHANGED
@@ -1,23 +1,11 @@
|
|
1
|
-
|
2
|
-
begin
|
3
|
-
require 'bundler/setup'
|
4
|
-
rescue LoadError
|
5
|
-
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
-
end
|
7
|
-
|
8
|
-
require 'rdoc/task'
|
9
|
-
|
10
|
-
RDoc::Task.new(:rdoc) do |rdoc|
|
11
|
-
rdoc.rdoc_dir = 'rdoc'
|
12
|
-
rdoc.title = 'SnipSnip'
|
13
|
-
rdoc.options << '--line-numbers'
|
14
|
-
rdoc.rdoc_files.include('README.md')
|
15
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
16
|
-
end
|
17
|
-
|
1
|
+
require 'bundler/setup'
|
18
2
|
require 'bundler/gem_tasks'
|
19
|
-
|
20
3
|
require 'rake/testtask'
|
4
|
+
require 'yard'
|
5
|
+
|
6
|
+
YARD::Rake::YardocTask.new(:yard) do |t|
|
7
|
+
t.stats_options = ['--list-undoc']
|
8
|
+
end
|
21
9
|
|
22
10
|
Rake::TestTask.new(:test) do |t|
|
23
11
|
t.libs << 'lib'
|
data/lib/snip_snip.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
require 'forwardable'
|
3
1
|
require 'logger'
|
4
2
|
|
5
3
|
require 'snip_snip/filter'
|
@@ -7,7 +5,9 @@ require 'snip_snip/railtie'
|
|
7
5
|
require 'snip_snip/registry'
|
8
6
|
require 'snip_snip/reporter'
|
9
7
|
|
8
|
+
# Top-level wrapper module.
|
10
9
|
module SnipSnip
|
10
|
+
# The logger object used to report unused columns.
|
11
11
|
def self.logger
|
12
12
|
@logger ||= Logger.new(Rails.root.join('log', 'snip-snip.log'))
|
13
13
|
end
|
data/lib/snip_snip/filter.rb
CHANGED
@@ -1,26 +1,22 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
1
|
module SnipSnip
|
2
|
+
# Classes to filter out of the tracking when looking at which columns were
|
3
|
+
# selected.
|
3
4
|
class Filter
|
4
|
-
|
5
|
-
attr_accessor :filtered
|
5
|
+
attr_reader :filtered
|
6
6
|
|
7
7
|
def initialize(filtered = [])
|
8
|
-
filtered
|
9
|
-
|
8
|
+
@filtered = filtered
|
9
|
+
|
10
|
+
return unless ActiveRecord.const_defined?(:InternalMetadata)
|
11
|
+
@filtered << ActiveRecord::InternalMetadata
|
10
12
|
end
|
11
13
|
|
12
14
|
def filtered?(record)
|
13
15
|
filtered.any? { |filter| record.is_a?(filter) }
|
14
16
|
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
instance.filtered?(record)
|
19
|
-
end
|
20
|
-
|
21
|
-
def instance
|
22
|
-
@instance ||= new
|
23
|
-
end
|
18
|
+
def self.filtered?(record)
|
19
|
+
(@instance ||= new).filtered?(record)
|
24
20
|
end
|
25
21
|
end
|
26
22
|
end
|
data/lib/snip_snip/railtie.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
1
|
module SnipSnip
|
2
|
+
# Hooks into Rails to track when columns are selected but not necessary.
|
3
3
|
class Railtie < Rails::Railtie
|
4
|
-
|
5
4
|
initializer 'snip_snip.load_extensions' do
|
6
5
|
ActiveSupport.on_load(:action_controller) do
|
6
|
+
before_action { Registry.clear }
|
7
7
|
after_action { Reporter.report(self) }
|
8
8
|
end
|
9
9
|
|
data/lib/snip_snip/registry.rb
CHANGED
@@ -1,33 +1,47 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
1
|
module SnipSnip
|
2
|
+
# A list of records that were found during the course of the last request
|
3
|
+
# on which we are tracking columns.
|
3
4
|
class Registry
|
4
|
-
|
5
|
-
attr_accessor :records
|
5
|
+
attr_reader :records
|
6
6
|
|
7
7
|
def initialize
|
8
8
|
clear
|
9
9
|
end
|
10
10
|
|
11
|
+
# Wipe the registry clear of found objects so that we don't report on them
|
12
|
+
# twice.
|
11
13
|
def clear
|
12
|
-
|
14
|
+
@records = []
|
13
15
|
end
|
14
16
|
|
17
|
+
# Loop through each record that was found during the lifespan of this
|
18
|
+
# registry
|
15
19
|
def each_record(&block)
|
16
20
|
return to_enum(:each_record) unless block_given?
|
17
21
|
records.each(&block)
|
18
22
|
end
|
19
23
|
|
24
|
+
# Add a record to this registry
|
20
25
|
def register(record)
|
21
26
|
records << record
|
22
27
|
end
|
23
28
|
|
24
29
|
class << self
|
25
|
-
|
26
|
-
def_delegators :instance, :clear, :each_record, :register
|
27
|
-
|
30
|
+
# The singleton instance of this registry.
|
28
31
|
def instance
|
29
32
|
@instance ||= new
|
30
33
|
end
|
34
|
+
|
35
|
+
# Delegate all missing methods to the singleton instance.
|
36
|
+
def method_missing(method, *args, &block)
|
37
|
+
return super unless respond_to_missing?(method)
|
38
|
+
instance.public_send(method, *args, &block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Respond to all of the missing methods on the singleton instance.
|
42
|
+
def respond_to_missing?(method)
|
43
|
+
instance.respond_to?(method)
|
44
|
+
end
|
31
45
|
end
|
32
46
|
end
|
33
47
|
end
|
data/lib/snip_snip/reporter.rb
CHANGED
@@ -1,22 +1,29 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
1
|
module SnipSnip
|
2
|
+
# Handles outputting unnecessarily selected columns to the logs.
|
3
3
|
class Reporter
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# Represents a result that has unused columns.
|
5
|
+
Result =
|
6
|
+
Struct.new(:class_name, :primary_key, :unused) do
|
7
|
+
# A string representing the unused columns for the given record.
|
8
|
+
def report
|
9
|
+
@report ||= "#{class_name} #{primary_key}: #{unused.sort.join(', ')}"
|
10
|
+
end
|
7
11
|
end
|
8
|
-
end
|
9
12
|
|
10
|
-
|
13
|
+
attr_reader :results
|
11
14
|
|
12
15
|
def initialize
|
13
|
-
|
16
|
+
@results = find_results
|
14
17
|
end
|
15
18
|
|
19
|
+
# Report on the unused columns that were selected during the course of the
|
20
|
+
# processing the action on the given controller.
|
16
21
|
def report(controller)
|
17
22
|
return if results.empty?
|
18
23
|
|
19
|
-
|
24
|
+
action_display = "#{controller.controller_name}##{controller.action_name}"
|
25
|
+
SnipSnip.logger.info(action_display)
|
26
|
+
|
20
27
|
results.sort_by(&:report).each do |result|
|
21
28
|
SnipSnip.logger.info(" #{result.report}")
|
22
29
|
end
|
@@ -24,6 +31,7 @@ module SnipSnip
|
|
24
31
|
Registry.clear
|
25
32
|
end
|
26
33
|
|
34
|
+
# Report on the specified controller.
|
27
35
|
def self.report(controller)
|
28
36
|
new.report(controller)
|
29
37
|
end
|
@@ -34,11 +42,13 @@ module SnipSnip
|
|
34
42
|
Registry.each_record.each_with_object([]) do |record, records|
|
35
43
|
accessed = record.accessed_fields
|
36
44
|
unused = (record.attributes.keys - accessed)
|
37
|
-
|
38
|
-
|
39
|
-
primary_key = record.class.primary_key
|
40
|
-
records << Result.new(record.class.name, record.send(primary_key), unused)
|
45
|
+
records << result_from(record, unused) if unused.any?
|
41
46
|
end
|
42
47
|
end
|
48
|
+
|
49
|
+
def result_from(record, unused)
|
50
|
+
primary_key = record.class.primary_key
|
51
|
+
Result.new(record.class.name, record.public_send(primary_key), unused)
|
52
|
+
end
|
43
53
|
end
|
44
54
|
end
|
data/lib/snip_snip/version.rb
CHANGED
metadata
CHANGED
@@ -1,37 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snip_snip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Deisz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 5.0.0.beta4
|
20
|
-
- - "<"
|
17
|
+
- - "~>"
|
21
18
|
- !ruby/object:Gem::Version
|
22
19
|
version: '5.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 5.0.0.beta
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- - "
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: 5.0.0.beta4
|
30
|
-
- - "<"
|
27
|
+
- - "~>"
|
31
28
|
- !ruby/object:Gem::Version
|
32
29
|
version: '5.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 5.0.0.beta
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: rubocop
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
|
-
name:
|
48
|
+
name: simplecov
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - ">="
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: '0'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
62
|
+
name: sqlite3
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
64
64
|
requirements:
|
65
65
|
- - ">="
|
@@ -73,7 +73,7 @@ dependencies:
|
|
73
73
|
- !ruby/object:Gem::Version
|
74
74
|
version: '0'
|
75
75
|
- !ruby/object:Gem::Dependency
|
76
|
-
name:
|
76
|
+
name: yard
|
77
77
|
requirement: !ruby/object:Gem::Requirement
|
78
78
|
requirements:
|
79
79
|
- - ">="
|
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.6.13
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: SnipSnip cuts the deadweight
|