Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
7 changes: 7 additions & 0 deletions .qlty/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*
!configs
!configs/**
!hooks
!hooks/**
!qlty.toml
!.gitignore
79 changes: 79 additions & 0 deletions .qlty/qlty.toml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
inherit_from: .rubocop_todo.yml

require:
plugins:
- rubocop-rake
- rubocop-rspec

Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_units/math.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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|
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby_units/math_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down