abc_size 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 293927810a67c15335a07661d8cb27585ee9856b0e95aeb9721e5e38e40c2d2c
4
+ data.tar.gz: 9a213a32372bf35afdf159222f192bd03fe46d79c5a8fc9480e1c0d31cd099ad
5
+ SHA512:
6
+ metadata.gz: 1cc720a41c800a7a6e7b7f2fbccef9e1b90e325fec341c65084b3eb313e709bfd09914bc99dd66dcd481d7cdd0e29129033ccf2bed0f9104f0ba0a55e070e41f
7
+ data.tar.gz: 50d451798ee2b1f86acb242fb99cf347b3aaec1f75578e9603bd9b8ac6532406cad67534496bf669e25c5dbb1b2bec81bfe77833aa01606ca9fbea219401d144
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.6
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: single_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: single_quotes
16
+
17
+ Layout/LineLength:
18
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ## [0.1.2] - 2021-12-04
2
+
3
+ - Updates README.md
4
+
5
+ ## [0.1.1] - 2021-12-04
6
+
7
+ - Updates gems and gemspec
8
+
9
+ ## [0.1.0] - 2021-11-27
10
+
11
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in abc_size.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'rubocop', '~> 1.21'
13
+ gem 'rubocop-rake', require: false
14
+ gem 'rubocop-rspec', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,64 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ abc_size (0.1.0)
5
+ rainbow (>= 3.0.0)
6
+ rubocop (>= 1.23.0)
7
+ rubocop-ast (>= 1.14.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ diff-lcs (1.4.4)
14
+ parallel (1.21.0)
15
+ parser (3.0.3.1)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.0.0)
18
+ rake (13.0.6)
19
+ regexp_parser (2.1.1)
20
+ rexml (3.2.5)
21
+ rspec (3.10.0)
22
+ rspec-core (~> 3.10.0)
23
+ rspec-expectations (~> 3.10.0)
24
+ rspec-mocks (~> 3.10.0)
25
+ rspec-core (3.10.1)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-expectations (3.10.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-mocks (3.10.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-support (3.10.3)
34
+ rubocop (1.23.0)
35
+ parallel (~> 1.10)
36
+ parser (>= 3.0.0.0)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.8, < 3.0)
39
+ rexml
40
+ rubocop-ast (>= 1.12.0, < 2.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 1.4.0, < 3.0)
43
+ rubocop-ast (1.14.0)
44
+ parser (>= 3.0.1.1)
45
+ rubocop-rake (0.6.0)
46
+ rubocop (~> 1.0)
47
+ rubocop-rspec (2.6.0)
48
+ rubocop (~> 1.19)
49
+ ruby-progressbar (1.11.0)
50
+ unicode-display_width (2.1.0)
51
+
52
+ PLATFORMS
53
+ x86_64-linux
54
+
55
+ DEPENDENCIES
56
+ abc_size!
57
+ rake (~> 13.0)
58
+ rspec (~> 3.0)
59
+ rubocop (~> 1.21)
60
+ rubocop-rake
61
+ rubocop-rspec
62
+
63
+ BUNDLED WITH
64
+ 2.2.32
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Emil Furtak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # abc_size
2
+
3
+ Calculate ABC size, divided into methods defined in a single file.
4
+
5
+ ## Installation
6
+
7
+ From RubyGems
8
+
9
+ ```sh
10
+ gem install abc_size
11
+ ```
12
+
13
+ From GitHub
14
+
15
+ ```sh
16
+ git clone https://github.com/efurtak/abc_size
17
+ ```
18
+
19
+ ```sh
20
+ cd abc_size
21
+ ```
22
+
23
+ ```sh
24
+ gem build abc_size.gemspec
25
+ ```
26
+
27
+ ```sh
28
+ gem install abc_size-*.gem
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```sh
34
+ abc [file] [options]
35
+ ```
36
+
37
+ ```
38
+ Options:
39
+ -d, --discount Discount repeated attributes
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ Bug reports are welcome on GitHub at https://github.com/efurtak/abc_size.
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'abc_size'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
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
data/exe/abc ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH.unshift("#{__dir__}/../lib")
5
+
6
+ require 'abc_size'
7
+
8
+ path = ARGV[0]
9
+ discount = ['-d', '--discount'].include?(ARGV[1])
10
+
11
+ if path
12
+ AbcSize::Calculator.new.call(path: path, discount: discount)
13
+ else
14
+ puts "Usage: abc [file] [options]\n"\
15
+ "\n"\
16
+ "Options:\n"\
17
+ ' -d, --discount Discount repeated attributes'
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AbcSize
4
+ VERSION = '0.1.2'
5
+ end
data/lib/abc_size.rb ADDED
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'abc_size/version'
4
+
5
+ require 'rainbow'
6
+ require 'rubocop'
7
+ require 'rubocop-ast'
8
+
9
+ module AbcSize
10
+ class Error < StandardError; end
11
+
12
+ # class responsible for returning ABC size from ABC size calculator
13
+ class Calculator
14
+ SATISFACTORY_ABC_SIZE = 17
15
+
16
+ attr_reader :results
17
+
18
+ def initialize
19
+ @results = []
20
+ end
21
+
22
+ def call(source_code: nil, path: nil, discount: false)
23
+ source = source_code || source_code_from_file(path)
24
+ ruby_version = RuboCop::TargetRuby.supported_versions.last
25
+
26
+ nodes = RuboCop::AST::ProcessedSource.new(source, ruby_version).ast
27
+
28
+ nodes.each_node { |node| results << calculate_result(node, discount) if node.is_a?(RuboCop::AST::DefNode) }
29
+
30
+ print_results
31
+
32
+ print_interpretation
33
+
34
+ # return results for testing purposes
35
+ results
36
+ end
37
+
38
+ private
39
+
40
+ def source_code_from_file(path)
41
+ data = File.open(path, 'r').read
42
+ raise Error, 'File is empty!' if data.empty?
43
+
44
+ data
45
+ rescue TypeError, Errno::ENOENT, Errno::EISDIR, Error => e
46
+ puts "#{e.message}\n"\
47
+ 'Please provide valid path to valid file.'
48
+ exit
49
+ end
50
+
51
+ def calculate_result(node, discount)
52
+ abc_size, abc = RuboCop::Cop::Metrics::Utils::AbcSizeCalculator.calculate(
53
+ node,
54
+ discount_repeated_attributes: discount
55
+ )
56
+
57
+ [abc_size, abc, node.method_name]
58
+ end
59
+
60
+ def print_results
61
+ results.each do |result|
62
+ abc_size, abc, method_name = result
63
+
64
+ satisfactory = abc_size <= SATISFACTORY_ABC_SIZE
65
+
66
+ puts "ABC size: #{color(format('%.2f', abc_size), satisfactory)}, "\
67
+ "ABC: #{color(abc, satisfactory)} "\
68
+ "for method: #{color(method_name, satisfactory)}"
69
+ end
70
+ end
71
+
72
+ def color(input, satisfactory)
73
+ color = satisfactory ? :yellow : :red
74
+ Rainbow(input).color(color)
75
+ end
76
+
77
+ def print_interpretation
78
+ puts "\n"\
79
+ "ABC size: <= 17 satisfactory, 18..30 unsatisfactory, > 30 dangerous\n"\
80
+ 'ABC: <assignments, branches (method calls), conditions>'
81
+ end
82
+ end
83
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: abc_size
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Emil Furtak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rainbow
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.23.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.23.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-ast
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.14.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.14.0
55
+ description: Calculate ABC size, divided into methods defined in a single file.
56
+ email:
57
+ - emil.furtak@gmail.com
58
+ executables:
59
+ - abc
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".rspec"
64
+ - ".rubocop.yml"
65
+ - CHANGELOG.md
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - exe/abc
74
+ - lib/abc_size.rb
75
+ - lib/abc_size/version.rb
76
+ homepage: https://github.com/efurtak/abc_size
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://github.com/efurtak/abc_size
81
+ source_code_uri: https://github.com/efurtak/abc_size
82
+ changelog_uri: https://github.com/efurtak/abc_size/blob/master/CHANGELOG.md
83
+ rubygems_mfa_required: 'true'
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.6.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.2.32
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Calculate ABC size.
103
+ test_files: []