From 191391363c6f3372a920abd0f8a22d252916de52 Mon Sep 17 00:00:00 2001 From: Oliver Peate Date: Wed, 22 Oct 2025 15:09:52 +0100 Subject: [PATCH] Adopt StandardRB https://github.com/standardrb/standard --- .github/workflows/test.yml | 2 + .rubocop.yml | 3 -- .standard.yml | 2 + Gemfile | 2 +- Gemfile.lock | 47 +++++++++++++++++ Rakefile | 4 +- example/example.rb | 6 +-- lib/simplify_rb.rb | 10 ++-- lib/simplify_rb/douglas_peucker_simplifier.rb | 8 +-- lib/simplify_rb/point.rb | 8 +-- simplify_rb.gemspec | 31 ++++++----- spec/simplify_rb/point_spec.rb | 52 +++++++++++-------- spec/simplify_rb_spec.rb | 52 +++++++++---------- 13 files changed, 141 insertions(+), 86 deletions(-) delete mode 100644 .rubocop.yml create mode 100644 .standard.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dcde858..910856b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,3 +17,5 @@ jobs: bundler-cache: true - name: RSpec run: bundle exec rspec + - name: Standard + run: bundle exec standardrb --no-fix \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml deleted file mode 100644 index 215fc90..0000000 --- a/.rubocop.yml +++ /dev/null @@ -1,3 +0,0 @@ -AllCops: - Exclude: - - 'spec/fixtures/*' diff --git a/.standard.yml b/.standard.yml new file mode 100644 index 0000000..29a5a0f --- /dev/null +++ b/.standard.yml @@ -0,0 +1,2 @@ +fix: true +parallel: true \ No newline at end of file diff --git a/Gemfile b/Gemfile index 29b4fd5..459605b 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source 'https://rubygems.org' +source "https://rubygems.org" # Specify your gem's dependencies in simplify_rb.gemspec gemspec diff --git a/Gemfile.lock b/Gemfile.lock index dc71000..f5cdc5d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,8 +6,20 @@ PATH GEM remote: https://rubygems.org/ specs: + ast (2.4.3) diff-lcs (1.6.2) + json (2.15.1) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) + parallel (1.27.0) + parser (3.3.9.0) + ast (~> 2.4.1) + racc + prism (1.6.0) + racc (1.8.1) + rainbow (3.1.1) rake (13.3.0) + regexp_parser (2.11.3) rspec (3.13.2) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) @@ -21,6 +33,40 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.13.0) rspec-support (3.13.6) + rubocop (1.80.2) + json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.46.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.47.1) + parser (>= 3.3.7.2) + prism (~> 1.4) + rubocop-performance (1.25.0) + lint_roller (~> 1.1) + rubocop (>= 1.75.0, < 2.0) + rubocop-ast (>= 1.38.0, < 2.0) + ruby-progressbar (1.13.0) + standard (1.51.1) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.80.2) + standard-custom (~> 1.0.0) + standard-performance (~> 1.8) + standard-custom (1.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.50) + standard-performance (1.8.0) + lint_roller (~> 1.1) + rubocop-performance (~> 1.25.0) + unicode-display_width (3.2.0) + unicode-emoji (~> 4.1) + unicode-emoji (4.1.0) PLATFORMS arm64-darwin-24 @@ -30,6 +76,7 @@ DEPENDENCIES rake rspec simplify_rb! + standard BUNDLED WITH 2.7.2 diff --git a/Rakefile b/Rakefile index e555ba8..998e8fe 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ -require 'bundler/gem_tasks' -require 'rspec/core/rake_task' +require "bundler/gem_tasks" +require "rspec/core/rake_task" RSpec::Core::RakeTask.new(:spec) task default: [:spec] diff --git a/example/example.rb b/example/example.rb index 4e94ddf..e75229b 100644 --- a/example/example.rb +++ b/example/example.rb @@ -1,8 +1,8 @@ -require 'simplify_rb' +require "simplify_rb" points = [ - { x: 51.5256, y: -0.0875 }, - { x: 51.7823, y: -0.0912 } + {x: 51.5256, y: -0.0875}, + {x: 51.7823, y: -0.0912} ] tolerance = 1 high_quality = true diff --git a/lib/simplify_rb.rb b/lib/simplify_rb.rb index cb234d4..bc16434 100644 --- a/lib/simplify_rb.rb +++ b/lib/simplify_rb.rb @@ -1,12 +1,12 @@ -require 'simplify_rb/version' -require 'simplify_rb/point' -require 'simplify_rb/radial_distance_simplifier' -require 'simplify_rb/douglas_peucker_simplifier' +require "simplify_rb/version" +require "simplify_rb/point" +require "simplify_rb/radial_distance_simplifier" +require "simplify_rb/douglas_peucker_simplifier" module SimplifyRb class Simplifier def process(raw_points, tolerance = 1, highest_quality = false) - raise ArgumentError.new('raw_points must be enumerable') unless raw_points.is_a? Enumerable + raise ArgumentError.new("raw_points must be enumerable") unless raw_points.is_a? Enumerable return raw_points if raw_points.length <= 1 diff --git a/lib/simplify_rb/douglas_peucker_simplifier.rb b/lib/simplify_rb/douglas_peucker_simplifier.rb index 61f85c9..69170c0 100644 --- a/lib/simplify_rb/douglas_peucker_simplifier.rb +++ b/lib/simplify_rb/douglas_peucker_simplifier.rb @@ -4,7 +4,7 @@ module SimplifyRb class DouglasPeuckerSimplifier def process(points, sq_tolerance) points.first.keep = true - points.last.keep = true + points.last.keep = true simplify_douglas_peucker(points, sq_tolerance) .select(&:keep) @@ -16,7 +16,7 @@ def process(points, sq_tolerance) def simplify_douglas_peucker(points, sq_tolerance) first_i = 0 - last_i = points.length - 1 + last_i = points.length - 1 index = nil stack = [] @@ -55,8 +55,8 @@ def calc_max_sq_dist(first_i, last_i, points) # Square distance from a point to a segment def get_sq_seg_dist(point, point_1, point_2) - x = point_1.x - y = point_1.y + x = point_1.x + y = point_1.y dx = point_2.x - x dy = point_2.y - y diff --git a/lib/simplify_rb/point.rb b/lib/simplify_rb/point.rb index 9bf057a..bf619a3 100644 --- a/lib/simplify_rb/point.rb +++ b/lib/simplify_rb/point.rb @@ -21,16 +21,16 @@ def parse_x_y(raw_point) x = nil y = nil - if raw_point.kind_of? Hash - x = raw_point[:x] || raw_point['x'] - y = raw_point[:y] || raw_point['y'] + if raw_point.is_a? Hash + x = raw_point[:x] || raw_point["x"] + y = raw_point[:y] || raw_point["y"] elsif raw_point.respond_to?(:x) && raw_point.respond_to?(:y) x = raw_point.x y = raw_point.y end if x.nil? || y.nil? - raise ArgumentError.new('Points must have :x and :y values') + raise ArgumentError.new("Points must have :x and :y values") end [x, y] diff --git a/simplify_rb.gemspec b/simplify_rb.gemspec index df5f2e6..401d883 100644 --- a/simplify_rb.gemspec +++ b/simplify_rb.gemspec @@ -1,22 +1,21 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +lib = File.expand_path("../lib", __FILE__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'simplify_rb/version' +require "simplify_rb/version" Gem::Specification.new do |spec| - spec.name = 'simplify_rb' - spec.version = SimplifyRb::VERSION - spec.authors = ['odlp'] - spec.description = 'You can use this gem to reduce the number of points in a complex polyline / polygon, making use of an optimized Douglas-Peucker algorithm. Ruby port of Simplify.js.' - spec.summary = 'Polyline simplification library. Ruby port of Simplify.js.' - spec.homepage = 'https://github.com/odlp/simplify_rb' - spec.license = 'MIT' + spec.name = "simplify_rb" + spec.version = SimplifyRb::VERSION + spec.authors = ["odlp"] + spec.description = "You can use this gem to reduce the number of points in a complex polyline / polygon, making use of an optimized Douglas-Peucker algorithm. Ruby port of Simplify.js." + spec.summary = "Polyline simplification library. Ruby port of Simplify.js." + spec.homepage = "https://github.com/odlp/simplify_rb" + spec.license = "MIT" - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ['lib'] + spec.files = `git ls-files`.split($/) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.require_paths = ["lib"] - spec.add_development_dependency 'rake' - spec.add_development_dependency 'rspec' + spec.add_development_dependency "rake" + spec.add_development_dependency "rspec" + spec.add_development_dependency "standard" end diff --git a/spec/simplify_rb/point_spec.rb b/spec/simplify_rb/point_spec.rb index 6ff2594..724a5d3 100644 --- a/spec/simplify_rb/point_spec.rb +++ b/spec/simplify_rb/point_spec.rb @@ -1,9 +1,9 @@ -require 'simplify_rb/point' +require "simplify_rb/point" RSpec.describe SimplifyRb::Point do - describe 'parsing hashes with string keys' do - it 'determines the :x, :y value' do - raw_point = { "x" => 51.5256, "y" => -0.0875 } + describe "parsing hashes with string keys" do + it "determines the :x, :y value" do + raw_point = {"x" => 51.5256, "y" => -0.0875} point = described_class.new(raw_point) expect(point.x).to eq(51.5256) @@ -11,10 +11,10 @@ end end - describe 'parsing structs' do - it 'determines the :x, :y value' do - CustomPointStruct = Struct.new(:x, :y) - raw_point = CustomPointStruct.new(51.5256, -0.0875) + describe "parsing structs" do + it "determines the :x, :y value" do + custom_point_struct = Struct.new(:x, :y) + raw_point = custom_point_struct.new(51.5256, -0.0875) point = described_class.new(raw_point) @@ -23,9 +23,9 @@ end end - describe 'handling raw points which are objects' do - it 'determines the :x, :y value' do - class MyCustomPoint + describe "handling raw points which are objects" do + it "determines the :x, :y value" do + my_custom_point = define_class("MyCustomPoint") do attr_reader :x, :y def initialize(x, y) @@ -34,7 +34,7 @@ def initialize(x, y) end end - raw_point = MyCustomPoint.new(51.5256, -0.0875) + raw_point = my_custom_point.new(51.5256, -0.0875) point = described_class.new(raw_point) @@ -43,17 +43,17 @@ def initialize(x, y) end end - describe 'missing x/y values' do - it 'raises an error if the points are missing keys' do - invalid_point = { Z: 51.5256, y: -0.0875 } - expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, 'Points must have :x and :y values') + describe "missing x/y values" do + it "raises an error if the points are missing keys" do + invalid_point = {Z: 51.5256, y: -0.0875} + expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, "Points must have :x and :y values") - invalid_point = { x: 51.5256, Z: -0.0875 } - expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, 'Points must have :x and :y values') + invalid_point = {x: 51.5256, Z: -0.0875} + expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, "Points must have :x and :y values") end - it 'raises an error if points don\'t respond to x / y' do - class UnconventialPoint + it "raises an error if points don't respond to x / y" do + unconvention_point = define_class("UnconventionPoint") do attr_reader :a, :b def initialize(a, b) @@ -62,8 +62,16 @@ def initialize(a, b) end end - invalid_point = UnconventialPoint.new(51.5256, -0.0875) - expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, 'Points must have :x and :y values') + invalid_point = unconvention_point.new(51.5256, -0.0875) + expect { described_class.new(invalid_point) }.to raise_error(ArgumentError, "Points must have :x and :y values") end end + + private + + def define_class(name, &block) + const = stub_const(name, Class.new) + const.class_eval(&block) if block_given? + const + end end diff --git a/spec/simplify_rb_spec.rb b/spec/simplify_rb_spec.rb index 3659ded..98c0454 100644 --- a/spec/simplify_rb_spec.rb +++ b/spec/simplify_rb_spec.rb @@ -1,65 +1,65 @@ -require 'simplify_rb' -require 'yaml' +require "simplify_rb" +require "yaml" RSpec.describe SimplifyRb::Simplifier do - describe '#process' do - context 'simplifies points correctly with the given tolerance' do - let(:test_data) { fixture_file('all-points.yml') } - let(:expected_result_fast) { fixture_file('result-fast.yml') } - let(:expected_result_high_quality) { fixture_file('result-high-quality.yml') } + describe "#process" do + context "simplifies points correctly with the given tolerance" do + let(:test_data) { fixture_file("all-points.yml") } + let(:expected_result_fast) { fixture_file("result-fast.yml") } + let(:expected_result_high_quality) { fixture_file("result-high-quality.yml") } - it 'uses the fast strategy by default', focus: true do + it "uses the fast strategy by default", focus: true do result = subject.process(test_data, 5) expect(result).to eq(expected_result_fast) end - it 'uses the high quality strategy when the flag is passed' do + it "uses the high quality strategy when the flag is passed" do result = subject.process(test_data, 5, true) expect(result).to eq(expected_result_high_quality) end end - describe 'extra properties on the data' do - it 'preserves the extra properties' do + describe "extra properties on the data" do + it "preserves the extra properties" do richer_data = [ - { x: 51.5256, y: -0.0875, note: 'Foo bar' }, - { x: 51.7823, y: -0.0912, attr: 123 } + {x: 51.5256, y: -0.0875, note: "Foo bar"}, + {x: 51.7823, y: -0.0912, attr: 123} ] result = subject.process(richer_data, 5, true) expect(result.length).to eq 2 - expect(result.first[:note]).to eq 'Foo bar' + expect(result.first[:note]).to eq "Foo bar" expect(result.last[:attr]).to eq 123 end end - context 'only one point' do - it 'returns a list with one point' do - data = [{ x: 1, y: 2 }] + context "only one point" do + it "returns a list with one point" do + data = [{x: 1, y: 2}] expect(subject.process(data)).to eq(data) end end - context 'no points' do - it 'returns an empty list of points' do + context "no points" do + it "returns an empty list of points" do expect(subject.process([])).to be_empty end end - describe 'unexpected arguments' do - context 'when raw_points is not enumerable' do - it 'raises an ArgumentError' do + describe "unexpected arguments" do + context "when raw_points is not enumerable" do + it "raises an ArgumentError" do data = Object.new - expect { subject.process(data) }.to raise_error(ArgumentError, 'raw_points must be enumerable') + expect { subject.process(data) }.to raise_error(ArgumentError, "raw_points must be enumerable") end end context "when raw_points is enumerable, but doesn't respond to x/y" do - it 'raises an ArgumentError' do - data = [{ foo: :bar }, { foo: :bar }] - expect { subject.process(data) }.to raise_error(ArgumentError, 'Points must have :x and :y values') + it "raises an ArgumentError" do + data = [{foo: :bar}, {foo: :bar}] + expect { subject.process(data) }.to raise_error(ArgumentError, "Points must have :x and :y values") end end end