-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
703 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Utils | ||
module Array | ||
## | ||
# @example | ||
# Preconditions (pseudo-real world): | ||
# | ||
# - Both `match?` and `match` do exactly the same thing, only their return values are different. | ||
# - `match?` returns `true` when the string matches the regex, otherwise - `false`. | ||
# - `match` returns regex match data when the string matches the regex, otherwise - `nil`. | ||
# - Regex matching is a very resource-heavy process. | ||
# | ||
# - `test` is defined like this: | ||
# def test | ||
# string = strings.find { |string| string.match?(regexp) } | ||
# | ||
# return unless string | ||
# | ||
# string.match(regexp) | ||
# end | ||
# | ||
# - To simplify the example, imagine that `strings` return an array with only one string and that string matches regex. | ||
# def strings | ||
# ["foo bar"] | ||
# end | ||
# | ||
# def regexp | ||
# /\w+/ | ||
# end | ||
# - This way regex matching (a very resource-heavy process) is executed twice. | ||
# - First time during the `match?` method call. | ||
# - The second time during the `match` method call. | ||
# | ||
# Task: | ||
# - How to implement `test` in a way that regexp matching (very resource-heavy process) is executed only once for the preconditions above? | ||
# | ||
# Solution: | ||
# - Use `ConvenientService::Utils::Array.find_yield` | ||
# - `test` should be rewritten like this: | ||
# def test | ||
# ConvenientService::Utils::Array.find_yield(strings) { |string| string.match(regexp) } | ||
# end | ||
# | ||
# Inspiration: | ||
# - https://github.com/rubyworks/facets/blob/3.1.0/lib/core/facets/enumerable/find_yield.rb#L28 | ||
# - https://stackoverflow.com/a/38457218/12201472 | ||
# | ||
# Note: `String#match?` and `String#match` docs. | ||
# - https://ruby-doc.org/core-2.7.0/String.html#method-i-match-3F | ||
# - https://ruby-doc.org/core-2.7.0/String.html#method-i-match | ||
# | ||
class FindYield < Support::Command | ||
## | ||
# @!attribute [r] array | ||
# @return [Array] | ||
# | ||
attr_reader :array | ||
|
||
## | ||
# @!attribute [r] block | ||
# @return [Proc] | ||
# | ||
attr_reader :block | ||
|
||
## | ||
# @param array [Array] | ||
# @param block [Proc] | ||
# @return [void] | ||
# | ||
def initialize(array, &block) | ||
@array = array | ||
@block = block | ||
end | ||
|
||
## | ||
# @return [Object, nil] Can be any type. | ||
# @see https://github.com/rubyworks/facets/blob/main/lib/core/facets/enumerable/find_yield.rb#L28 | ||
# | ||
def call | ||
array.each do |item| | ||
block_value = block.call(item) | ||
|
||
return block_value if block_value | ||
end | ||
|
||
nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Utils | ||
module Array | ||
class KeepAfter < Support::Command | ||
## | ||
# @!attribute [r] array | ||
# @return [Array] | ||
# | ||
attr_reader :array | ||
|
||
## | ||
# @!attribute [r] object | ||
# @return [Object] Can be any type. | ||
# | ||
attr_reader :object | ||
|
||
## | ||
# @param array [Array] | ||
# @param object [Object] Can be any type. | ||
# @return [void] | ||
# | ||
def initialize(array, object) | ||
@array = array | ||
@object = object | ||
end | ||
|
||
## | ||
# @return [Object, nil] Can be any type. | ||
# @see https://github.com/rubyworks/facets/blob/main/lib/core/facets/array/before.rb#L35 | ||
# | ||
def call | ||
return [] unless array.include?(object) | ||
|
||
Utils::Array.drop_while(array, inclusively: true) { |item| item != object } | ||
end | ||
end | ||
end | ||
end | ||
end |
55 changes: 55 additions & 0 deletions
55
spec/lib/convenient_service/core/entities/method_middlewares/entities/callers/base_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
require "convenient_service" | ||
|
||
RSpec.describe ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base do | ||
include ConvenientService::RSpec::Matchers::DelegateTo | ||
|
||
let(:caller) { caller_class.new(entity: entity) } | ||
let(:caller_class) { described_class } | ||
|
||
let(:entity) { double } | ||
let(:method) { :result } | ||
|
||
example_group "modules" do | ||
include ConvenientService::RSpec::Matchers::IncludeModule | ||
|
||
subject { described_class } | ||
|
||
it { is_expected.to include_module(ConvenientService::Support::AbstractMethod) } | ||
end | ||
|
||
example_group "attributes" do | ||
include ConvenientService::RSpec::Matchers::HaveAttrReader | ||
|
||
subject { caller } | ||
|
||
it { is_expected.to have_attr_reader(:entity) } | ||
end | ||
|
||
example_group "abstract methods" do | ||
include ConvenientService::RSpec::Matchers::HaveAbstractMethod | ||
|
||
subject { caller } | ||
|
||
it { is_expected.to have_abstract_method(:commit_config!) } | ||
it { is_expected.to have_abstract_method(:ancestors) } | ||
it { is_expected.to have_abstract_method(:methods_middlewares_callers) } | ||
end | ||
|
||
# example_group "instance methods" do | ||
# describe "#ancestors_greater_than_methods_middlewares_callers" do | ||
# ## | ||
# # NOTE: `#ancestors_greater_than_methods_middlewares_callers` is tested in `ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base` descendants. | ||
# # | ||
# end | ||
# | ||
# describe "#resolve_super_method" do | ||
# ## | ||
# # NOTE: `#resolve_super_method` is tested in `ConvenientService::Core::Entities::MethodMiddlewares::Entities::Callers::Base` descendants. | ||
# # | ||
# end | ||
# end | ||
end |
Oops, something went wrong.