-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): track method_missing commit triggers
- Loading branch information
Showing
7 changed files
with
265 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "commands/track_method_missing_commit_trigger" |
69 changes: 69 additions & 0 deletions
69
lib/convenient_service/core/entities/config/commands/track_method_missing_commit_trigger.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,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module ConvenientService | ||
module Core | ||
module Entities | ||
class Config | ||
module Commands | ||
class TrackMethodMissingCommitTrigger < Support::Command | ||
## | ||
# @!attribute [r] config | ||
# @return [ConvenientService::Core::Entities::Config] | ||
# | ||
attr_reader :config | ||
|
||
## | ||
# @!attribute [r] trigger | ||
# @return [ConvenientService::Support::UniqueValue] | ||
# | ||
attr_reader :trigger | ||
|
||
## | ||
# @param config [ConvenientService::Core::Entities::Config] | ||
# @param trigger [ConvenientService::Support::UniqueValue] | ||
# @return [void] | ||
# | ||
def initialize(config:, trigger:) | ||
@config = config | ||
@trigger = trigger | ||
end | ||
|
||
## | ||
# @return [void] | ||
# | ||
def call | ||
return unless method_missing_trigger_valid? | ||
return if method_missing_commits_counter_incremented? | ||
|
||
raise Errors::TooManyCommitsFromMethodMissing.new(config: config) | ||
end | ||
|
||
## | ||
# @return [Boolean] | ||
# | ||
def method_missing_trigger_valid? | ||
method_missing_triggers.any?(trigger) | ||
end | ||
|
||
## | ||
# @return [Boolean] | ||
# | ||
def method_missing_commits_counter_incremented? | ||
Utils::Object.memoize_including_falsy_values(self, :@method_missing_commits_counter_incremented) { config.method_missing_commits_counter.bincrement } | ||
end | ||
|
||
## | ||
# @return [Array<ConvenientService::Support::UniqueValue>] | ||
# | ||
def method_missing_triggers | ||
@method_missing_triggers ||= [ | ||
Constants::Triggers::INSTANCE_METHOD_MISSING, | ||
Constants::Triggers::CLASS_METHOD_MISSING | ||
] | ||
end | ||
end | ||
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
147 changes: 147 additions & 0 deletions
147
...venient_service/core/entities/config/commands/track_method_missing_commit_trigger_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,147 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
require "convenient_service" | ||
|
||
# rubocop:disable RSpec/NestedGroups | ||
RSpec.describe ConvenientService::Core::Entities::Config::Commands::TrackMethodMissingCommitTrigger do | ||
example_group "class methods" do | ||
subject(:command_result) { described_class.call(config: config, trigger: trigger) } | ||
|
||
let(:config) { ConvenientService::Core::Entities::Config.new(klass: service_class) } | ||
|
||
let(:service_class) do | ||
Class.new do | ||
include ConvenientService::Core | ||
end | ||
end | ||
|
||
describe ".call" do | ||
context "when `method_missing` trigger is NOT valid" do | ||
let(:trigger) { ConvenientService::Core::Constants::Triggers::USER } | ||
|
||
it "does NOT raise `ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing`" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
|
||
it "does NOT increment `method_missing` commit counter" do | ||
expect { command_result }.not_to change { config.method_missing_commits_counter.current_value } | ||
end | ||
end | ||
|
||
context "when `method_missing` trigger is valid" do | ||
context "when `method_missing` trigger is `ConvenientService::Core::Constants::Triggers::INSTANCE_METHOD_MISSING`" do | ||
let(:trigger) { ConvenientService::Core::Constants::Triggers::INSTANCE_METHOD_MISSING } | ||
|
||
before do | ||
config.method_missing_commits_counter.current_value = config.method_missing_commits_counter.max_value | ||
end | ||
|
||
context "when `method_missing` counter is NOT incremented" do | ||
let(:error_message) do | ||
<<~TEXT | ||
`#{config.klass}` config is committed too many times from `method_missing`. | ||
In order to resolve this issue try to commit it manually before usage of any config-dependent method. | ||
Example 1 (outside class): | ||
# Commitment: | ||
#{config.klass}.commit_config! | ||
# Few lines later - usage: | ||
#{config.klass}.result # or whatever method. | ||
Example 2 (inside class): | ||
class #{config.klass} | ||
# ... | ||
commit_config! | ||
step :result # or any other method that becomes available after config commitment. | ||
end | ||
TEXT | ||
end | ||
|
||
it "raises `ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing`" do | ||
expect { command_result } | ||
.to raise_error(ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing) | ||
.with_message(error_message) | ||
end | ||
end | ||
|
||
context "when `method_missing` counter is incremented" do | ||
before do | ||
config.method_missing_commits_counter.reset | ||
end | ||
|
||
it "does NOT raise `ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing`" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
|
||
it "increments `method_missing` commit counter" do | ||
expect { command_result }.to change(config.method_missing_commits_counter, :current_value).from(config.method_missing_commits_counter.current_value).to(config.method_missing_commits_counter.current_value + 1) | ||
end | ||
end | ||
end | ||
|
||
context "when `method_missing` trigger is `ConvenientService::Core::Constants::Triggers::CLASS_METHOD_MISSING`" do | ||
let(:trigger) { ConvenientService::Core::Constants::Triggers::CLASS_METHOD_MISSING } | ||
|
||
before do | ||
config.method_missing_commits_counter.current_value = config.method_missing_commits_counter.max_value | ||
end | ||
|
||
context "when `method_missing` counter is NOT incremented" do | ||
let(:error_message) do | ||
<<~TEXT | ||
`#{config.klass}` config is committed too many times from `method_missing`. | ||
In order to resolve this issue try to commit it manually before usage of any config-dependent method. | ||
Example 1 (outside class): | ||
# Commitment: | ||
#{config.klass}.commit_config! | ||
# Few lines later - usage: | ||
#{config.klass}.result # or whatever method. | ||
Example 2 (inside class): | ||
class #{config.klass} | ||
# ... | ||
commit_config! | ||
step :result # or any other method that becomes available after config commitment. | ||
end | ||
TEXT | ||
end | ||
|
||
it "raises `ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing`" do | ||
expect { command_result } | ||
.to raise_error(ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing) | ||
.with_message(error_message) | ||
end | ||
end | ||
|
||
context "when `method_missing` counter is incremented" do | ||
before do | ||
config.method_missing_commits_counter.reset | ||
end | ||
|
||
it "does NOT raise `ConvenientService::Core::Entities::Config::Errors::TooManyCommitsFromMethodMissing`" do | ||
expect { command_result }.not_to raise_error | ||
end | ||
|
||
it "increments `method_missing` commit counter" do | ||
expect { command_result }.to change(config.method_missing_commits_counter, :current_value).from(config.method_missing_commits_counter.current_value).to(config.method_missing_commits_counter.current_value + 1) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable RSpec/NestedGroups |
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