From d051849d982fd82e4656c17fce564a7a70f76edf Mon Sep 17 00:00:00 2001 From: Nikita Shilnikov Date: Wed, 16 Nov 2022 19:36:13 +0300 Subject: [PATCH] Fix rubocop offences --- Gemfile | 2 +- bin/console | 6 +++--- lib/dry/matcher/case.rb | 14 +++++--------- lib/dry/matcher/either_matcher.rb | 8 ++++---- lib/dry/matcher/evaluator.rb | 3 ++- lib/dry/matcher/match.rb | 8 ++++++-- lib/dry/matcher/maybe_matcher.rb | 2 +- lib/dry/matcher/result_matcher.rb | 2 +- lib/dry/matcher/version.rb | 2 +- spec/integration/matcher_spec.rb | 8 ++++---- spec/integration/maybe_matcher_spec.rb | 16 ++++++++-------- spec/integration/result_matcher_spec.rb | 12 ++++++------ spec/spec_helper.rb | 2 +- 13 files changed, 43 insertions(+), 42 deletions(-) diff --git a/Gemfile b/Gemfile index 0a96303..76f499b 100644 --- a/Gemfile +++ b/Gemfile @@ -12,7 +12,7 @@ group :test do end group :tools do - gem "yard" gem "byebug", platform: :mri gem "pry" + gem "yard" end diff --git a/bin/console b/bin/console index 50f1967..bf729b1 100755 --- a/bin/console +++ b/bin/console @@ -1,13 +1,13 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'bundler/setup' -require 'dry-matcher' +require "bundler/setup" +require "dry-matcher" # You can add fixtures and/or initialization code here to make experimenting # with your gem easier. You can also use a different console, if you like. # (If you use this, don't forget to add pry to your Gemfile!) -require 'pry' +require "pry" binding.pry diff --git a/lib/dry/matcher/case.rb b/lib/dry/matcher/case.rb index e36c2ea..93fc939 100644 --- a/lib/dry/matcher/case.rb +++ b/lib/dry/matcher/case.rb @@ -10,15 +10,11 @@ class Case # @param match [#call] callable used to test given pattern against value # @param resolve [#call] callable used to resolve value into a result def initialize(match: Undefined, resolve: DEFAULT_RESOLVE, &block) - if block - @match = block - else - @match = proc do |value, patterns| - if match.(value, *patterns) - resolve.(value) - else - Undefined - end + @match = block || proc do |value, patterns| + if match.(value, *patterns) + resolve.(value) + else + Undefined end end end diff --git a/lib/dry/matcher/either_matcher.rb b/lib/dry/matcher/either_matcher.rb index a6fb1c2..8dabe02 100644 --- a/lib/dry/matcher/either_matcher.rb +++ b/lib/dry/matcher/either_matcher.rb @@ -1,14 +1,14 @@ # frozen_string_literal: true -require 'dry/core/deprecations' -require 'dry/matcher/result_matcher' +require "dry/core/deprecations" +require "dry/matcher/result_matcher" module Dry class Matcher - extend Dry::Core::Deprecations[:'dry-matcher'] + extend Dry::Core::Deprecations[:"dry-matcher"] EitherMatcher = ResultMatcher - deprecate_constant(:EitherMatcher, message: 'Dry::Matcher::ResultMatcher') + deprecate_constant(:EitherMatcher, message: "Dry::Matcher::ResultMatcher") end end diff --git a/lib/dry/matcher/evaluator.rb b/lib/dry/matcher/evaluator.rb index d9d5be9..385a168 100644 --- a/lib/dry/matcher/evaluator.rb +++ b/lib/dry/matcher/evaluator.rb @@ -61,7 +61,8 @@ def method_missing(name, *args, &block) def ensure_exhaustive_match if @unhandled_cases.any? - ::Kernel.raise NonExhaustiveMatchError, "cases +#{@unhandled_cases.join(', ')}+ not handled" + ::Kernel.raise NonExhaustiveMatchError, + "cases +#{@unhandled_cases.join(", ")}+ not handled" end end end diff --git a/lib/dry/matcher/match.rb b/lib/dry/matcher/match.rb index 124d48f..27dec7b 100644 --- a/lib/dry/matcher/match.rb +++ b/lib/dry/matcher/match.rb @@ -1,15 +1,18 @@ # frozen_string_literal: true -require 'dry/matcher' +require "dry/matcher" +# rubocop:disable Style/CaseEquality module Dry class Matcher - PatternMatch = ::Proc.new do |value, patterns| + PatternMatch = proc do |value, patterns| if patterns.empty? value + # rubocop:disable Lint/DuplicateBranch elsif value.is_a?(::Array) && patterns.any? { |p| p === value[0] } value elsif patterns.any? { |p| p === value } + # rubocop:enable Lint/DuplicateBranch value else Undefined @@ -17,3 +20,4 @@ class Matcher end end end +# rubocop:enable Style/CaseEquality diff --git a/lib/dry/matcher/maybe_matcher.rb b/lib/dry/matcher/maybe_matcher.rb index 83fcafe..df2e39e 100644 --- a/lib/dry/matcher/maybe_matcher.rb +++ b/lib/dry/matcher/maybe_matcher.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'dry/matcher' +require "dry/matcher" require "dry/matcher/match" module Dry diff --git a/lib/dry/matcher/result_matcher.rb b/lib/dry/matcher/result_matcher.rb index e524c64..b1bb811 100644 --- a/lib/dry/matcher/result_matcher.rb +++ b/lib/dry/matcher/result_matcher.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require 'dry/matcher' +require "dry/matcher" require "dry/matcher/match" module Dry diff --git a/lib/dry/matcher/version.rb b/lib/dry/matcher/version.rb index 104890a..a68a1e7 100644 --- a/lib/dry/matcher/version.rb +++ b/lib/dry/matcher/version.rb @@ -2,6 +2,6 @@ module Dry class Matcher - VERSION = '0.10.0' + VERSION = "0.10.0" end end diff --git a/spec/integration/matcher_spec.rb b/spec/integration/matcher_spec.rb index 348c7d1..0dd99cc 100644 --- a/spec/integration/matcher_spec.rb +++ b/spec/integration/matcher_spec.rb @@ -7,14 +7,14 @@ let(:success_case) do Dry::Matcher::Case.new( match: -> result { result.success? }, - resolve: -> result { result.value! }, + resolve: -> result { result.value! } ) end let(:failure_case) do Dry::Matcher::Case.new( match: -> result { result.failure? }, - resolve: -> result { result.failure }, + resolve: -> result { result.failure } ) end @@ -61,7 +61,7 @@ def call_match(input) let(:success_case) do Dry::Matcher::Case.new( match: -> result { result.first == :ok }, - resolve: -> result { result.last }, + resolve: -> result { result.last } ) end @@ -70,7 +70,7 @@ def call_match(input) match: -> result, failure_type { result.length == 3 && result[0] == :failure && result[1] == failure_type }, - resolve: -> result { result.last }, + resolve: -> result { result.last } ) end diff --git a/spec/integration/maybe_matcher_spec.rb b/spec/integration/maybe_matcher_spec.rb index d727e5f..19a85f9 100644 --- a/spec/integration/maybe_matcher_spec.rb +++ b/spec/integration/maybe_matcher_spec.rb @@ -10,7 +10,7 @@ before { Object.send(:remove_const, :Operation) if defined? Operation } - def self.set_up_expectations(matches) + def self.prepare_expectations(matches) matches.each do |value, matched| context "Matching #{value}" do let(:result) { value } @@ -33,9 +33,9 @@ def self.set_up_expectations(matches) end end - set_up_expectations( + prepare_expectations( Some("a success") => "Matched some: a success", - None() => "Matched none", + None() => "Matched none" ) end @@ -49,11 +49,11 @@ def self.set_up_expectations(matches) end end - set_up_expectations( + prepare_expectations( Some(:a) => "Matched specific success: :a", Some(:b) => "Matched specific success: :b", Some("a success") => "Matched general success: a success", - None() => "Matched none", + None() => "Matched none" ) end @@ -63,13 +63,13 @@ def self.set_up_expectations(matches) on.some(/done/) { |s| "Matched string by pattern: #{s.inspect}" } on.some(String) { |s| "Matched string success: #{s.inspect}" } on.some(Integer) { |n| "Matched integer success: #{n}" } - on.some(Date, Time) { |t| "Matched date success: #{t.strftime('%Y-%m-%d')}" } + on.some(Date, Time) { |t| "Matched date success: #{t.strftime("%Y-%m-%d")}" } on.some { |v| "Matched general success: #{v}" } on.none { "Matched none" } end end - set_up_expectations( + prepare_expectations( Some("nicely done") => 'Matched string by pattern: "nicely done"', Some("yay") => 'Matched string success: "yay"', Some(3) => "Matched integer success: 3", @@ -92,7 +92,7 @@ def self.set_up_expectations(matches) end end - set_up_expectations( + prepare_expectations( Some([:created, 5]) => "Matched :created by code: 5", Some([:updated, 6, 7]) => "Matched :updated by code: 6, 7", Some([:deleted, 8, 9]) => "Matched :deleted by code: 8", diff --git a/spec/integration/result_matcher_spec.rb b/spec/integration/result_matcher_spec.rb index 186ba65..9699c43 100644 --- a/spec/integration/result_matcher_spec.rb +++ b/spec/integration/result_matcher_spec.rb @@ -10,7 +10,7 @@ before { Object.send(:remove_const, :Operation) if defined? Operation } - def self.set_up_expectations(matches) + def self.prepare_expectations(matches) matches.each do |value, matched| context "Matching #{value}" do let(:result) { value } @@ -33,7 +33,7 @@ def self.set_up_expectations(matches) end end - set_up_expectations( + prepare_expectations( Success("a success") => "Matched success: a success", Failure("a failure") => "Matched failure: a failure", Try(StandardError) { "a success" } => "Matched success: a success", @@ -53,7 +53,7 @@ def self.set_up_expectations(matches) end end - set_up_expectations( + prepare_expectations( Success(:a) => "Matched specific success: :a", Success(:b) => "Matched specific success: :b", Success("a success") => "Matched general success: a success", @@ -69,14 +69,14 @@ def self.set_up_expectations(matches) on.success(/done/) { |s| "Matched string by pattern: #{s.inspect}" } on.success(String) { |s| "Matched string success: #{s.inspect}" } on.success(Integer) { |n| "Matched integer success: #{n}" } - on.success(Date, Time) { |t| "Matched date success: #{t.strftime('%Y-%m-%d')}" } + on.success(Date, Time) { |t| "Matched date success: #{t.strftime("%Y-%m-%d")}" } on.success { |v| "Matched general success: #{v}" } on.failure(Integer) { |n| "Matched integer failure: #{n}" } on.failure { |v| "Matched general failure: #{v}" } end end - set_up_expectations( + prepare_expectations( Success("nicely done") => 'Matched string by pattern: "nicely done"', Success("yay") => 'Matched string success: "yay"', Success(3) => "Matched integer success: 3", @@ -101,7 +101,7 @@ def self.set_up_expectations(matches) end end - set_up_expectations( + prepare_expectations( Success([:created, 5]) => "Matched :created by code: 5", Success([:updated, 6, 7]) => "Matched :updated by code: 6, 7", Success([:deleted, 8, 9]) => "Matched :deleted by code: 8", diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6f062f7..e59e83b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,7 +8,7 @@ rescue LoadError; end require "dry-matcher" -Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].each { |f| require f } +Dir[File.join(File.dirname(__FILE__), "support/**/*.rb")].sort.each { |f| require f } RSpec.configure do |config| config.disable_monkey_patching!