diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 7069c86..33f9af6 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -26,6 +26,10 @@ jobs: bundler-cache: true - name: Tests for Ruby ${{ matrix.ruby }} run: bundle exec rake + - uses: qltysh/qlty-action/coverage@v2 + with: + token: ${{ secrets.QLTY_COVERAGE_TOKEN }} + files: coverage/.resultset.json # CodeClimate has become qlth.sh, this integration is currently broken. # coverage: # needs: test @@ -43,6 +47,7 @@ jobs: # CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} # with: # coverageCommand: bundle exec rake + yard: runs-on: ubuntu-latest env: diff --git a/.qlty/.gitignore b/.qlty/.gitignore new file mode 100644 index 0000000..3036618 --- /dev/null +++ b/.qlty/.gitignore @@ -0,0 +1,7 @@ +* +!configs +!configs/** +!hooks +!hooks/** +!qlty.toml +!.gitignore diff --git a/.qlty/qlty.toml b/.qlty/qlty.toml new file mode 100644 index 0000000..2f50e17 --- /dev/null +++ b/.qlty/qlty.toml @@ -0,0 +1,79 @@ +# This file was automatically generated by `qlty init`. +# You can modify it to suit your needs. +# We recommend you to commit this file to your repository. +# +# This configuration is used by both Qlty CLI and Qlty Cloud. +# +# Qlty CLI -- Code quality toolkit for developers +# Qlty Cloud -- Fully automated Code Health Platform +# +# Try Qlty Cloud: https://qlty.sh +# +# For a guide to configuration, visit https://qlty.sh/d/config +# Or for a full reference, visit https://qlty.sh/d/qlty-toml +config_version = "0" + +exclude_patterns = [ + "*_min.*", + "*-min.*", + "*.min.*", + "**/.yarn/**", + "**/*.d.ts", + "**/assets/**", + "**/bower_components/**", + "**/build/**", + "**/cache/**", + "**/config/**", + "**/db/**", + "**/deps/**", + "**/dist/**", + "**/extern/**", + "**/external/**", + "**/generated/**", + "**/Godeps/**", + "**/gradlew/**", + "**/mvnw/**", + "**/node_modules/**", + "**/protos/**", + "**/seed/**", + "**/target/**", + "**/templates/**", + "**/testdata/**", + "**/vendor/**", +] + +test_patterns = [ + "**/test/**", + "**/spec/**", + "**/*.test.*", + "**/*.spec.*", + "**/*_test.*", + "**/*_spec.*", + "**/test_*.*", + "**/spec_*.*", +] + +[smells] +mode = "comment" + +[smells.boolean_logic] +threshold = 4 + +[smells.file_complexity] +threshold = 55 + +[smells.return_statements] +threshold = 4 + +[smells.nested_control_flow] +threshold = 4 + +[smells.function_parameters] +threshold = 4 + +[smells.function_complexity] +threshold = 5 + +[[source]] +name = "default" +default = true diff --git a/.rubocop.yml b/.rubocop.yml index ddc5b73..1a8689e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,6 +1,6 @@ inherit_from: .rubocop_todo.yml -require: +plugins: - rubocop-rake - rubocop-rspec diff --git a/lib/ruby_units/math.rb b/lib/ruby_units/math.rb index 303d582..6801c90 100644 --- a/lib/ruby_units/math.rb +++ b/lib/ruby_units/math.rb @@ -112,9 +112,9 @@ def atan(number) # @return [RubyUnits::Unit] if parameters are units # @raise [ArgumentError] if parameters are not numbers or compatible units def atan2(x, y) - raise ArgumentError, "Incompatible RubyUnits::Units" if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && !x.compatible?(y) + raise ArgumentError, "Incompatible RubyUnits::Units" if x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit) && !x.compatible?(y) - if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && x.compatible?(y) + if x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit) && x.compatible?(y) [super(x.base_scalar, y.base_scalar), "radian"].to_unit else super diff --git a/lib/ruby_units/unit.rb b/lib/ruby_units/unit.rb index 53d558b..ed730db 100644 --- a/lib/ruby_units/unit.rb +++ b/lib/ruby_units/unit.rb @@ -180,7 +180,7 @@ def self.inherited(subclass) # setup internal arrays and hashes # @return [Boolean] - def self.setup + def self.setup # rubocop:disable Naming/PredicateMethod clear_cache self.prefix_values = {} self.prefix_map = {} @@ -275,7 +275,7 @@ def self.cached end # @return [Boolean] - def self.clear_cache + def self.clear_cache # rubocop:disable Naming/PredicateMethod cached.clear base_unit_cache.clear new(1) @@ -556,7 +556,7 @@ def initialize(*options) _opt_scalar, opt_units = self.class.parse_into_numbers_and_units(options[0]) if !(self.class.cached.keys.include?(opt_units) || (opt_units =~ %r{\D/[\d+.]+}) || - (opt_units =~ %r{(#{self.class.temp_regex})|(#{STONE_LB_UNIT_REGEX})|(#{LBS_OZ_UNIT_REGEX})|(#{FEET_INCH_UNITS_REGEX})|%|(#{TIME_REGEX})|i\s?(.+)?|±|\+/-})) && (opt_units && !opt_units.empty?) + (opt_units =~ %r{(#{self.class.temp_regex})|(#{STONE_LB_UNIT_REGEX})|(#{LBS_OZ_UNIT_REGEX})|(#{FEET_INCH_UNITS_REGEX})|%|(#{TIME_REGEX})|i\s?(.+)?|±|\+/-})) && opt_units && !opt_units.empty? self.class.cached.set(opt_units, scalar == 1 ? self : opt_units.to_unit) end end @@ -682,17 +682,17 @@ def to_s(target_units = nil, precision: 0.0001, format: RubyUnits.configuration. feet, inches = convert_to("in").scalar.abs.divmod(12) improper, frac = inches.divmod(1) frac = frac.zero? ? "" : "-#{frac.rationalize(precision)}" - out = "#{negative? ? '-' : nil}#{feet}'#{improper}#{frac}\"" + out = "#{'-' if negative?}#{feet}'#{improper}#{frac}\"" when :lbs pounds, ounces = convert_to("oz").scalar.abs.divmod(16) improper, frac = ounces.divmod(1) frac = frac.zero? ? "" : "-#{frac.rationalize(precision)}" - out = "#{negative? ? '-' : nil}#{pounds}#{separator}lbs #{improper}#{frac}#{separator}oz" + out = "#{'-' if negative?}#{pounds}#{separator}lbs #{improper}#{frac}#{separator}oz" when :stone stone, pounds = convert_to("lbs").scalar.abs.divmod(14) improper, frac = pounds.divmod(1) frac = frac.zero? ? "" : "-#{frac.rationalize(precision)}" - out = "#{negative? ? '-' : nil}#{stone}#{separator}stone #{improper}#{frac}#{separator}lbs" + out = "#{'-' if negative?}#{stone}#{separator}stone #{improper}#{frac}#{separator}lbs" when String out = case target_units.strip when /\A\s*\Z/ # whitespace only @@ -1309,7 +1309,7 @@ def units(with_prefix: true, format: nil) .uniq .map { [_1, output_denominator.count(_1)] } .map { |element, power| (element.to_s.strip + (power > 1 ? "^#{power}" : "")) } - "#{on.join('*')}#{od.empty? ? '' : "/#{od.join('*')}"}".strip + "#{on.join('*')}#{"/#{od.join('*')}" unless od.empty?}".strip end end @@ -1768,7 +1768,7 @@ def parse(passed_unit_string = "0") # eliminate all known terms from this string. This is a quick check to see if the passed unit # contains terms that are not defined. - used = "#{top} #{bottom}".to_s.gsub(self.class.unit_match_regex, "").gsub(%r{[\d*, "'_^/$]}, "") + used = "#{top} #{bottom}".gsub(self.class.unit_match_regex, "").gsub(%r{[\d*, "'_^/$]}, "") raise(ArgumentError, "'#{passed_unit_string}' Unit not recognized") unless used.empty? @numerator = @numerator.map do |item| diff --git a/spec/ruby_units/math_spec.rb b/spec/ruby_units/math_spec.rb index a9fe8c2..09fa295 100644 --- a/spec/ruby_units/math_spec.rb +++ b/spec/ruby_units/math_spec.rb @@ -25,7 +25,7 @@ end context "with 'PI/4 radians' unit" do - subject(:angle) { RubyUnits::Unit.new((Math::PI / 4), "radians") } + subject(:angle) { RubyUnits::Unit.new(Math::PI / 4, "radians") } specify { expect(Math.sin(angle)).to be_within(0.01).of(0.70710678) } specify { expect(Math.cos(angle)).to be_within(0.01).of(0.70710678) }