Skip to content

Commit

Permalink
Fix rubocop offences
Browse files Browse the repository at this point in the history
  • Loading branch information
flash-gordon committed Nov 16, 2022
1 parent f49634e commit d051849
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ group :test do
end

group :tools do
gem "yard"
gem "byebug", platform: :mri
gem "pry"
gem "yard"
end
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -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
14 changes: 5 additions & 9 deletions lib/dry/matcher/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/dry/matcher/either_matcher.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion lib/dry/matcher/evaluator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/dry/matcher/match.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# 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
end
end
end
end
# rubocop:enable Style/CaseEquality
2 changes: 1 addition & 1 deletion lib/dry/matcher/maybe_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'dry/matcher'
require "dry/matcher"
require "dry/matcher/match"

module Dry
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/matcher/result_matcher.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'dry/matcher'
require "dry/matcher"
require "dry/matcher/match"

module Dry
Expand Down
2 changes: 1 addition & 1 deletion lib/dry/matcher/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Dry
class Matcher
VERSION = '0.10.0'
VERSION = "0.10.0"
end
end
8 changes: 4 additions & 4 deletions spec/integration/matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down
16 changes: 8 additions & 8 deletions spec/integration/maybe_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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",
Expand All @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions spec/integration/result_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down

0 comments on commit d051849

Please sign in to comment.