Skip to content

Commit

Permalink
feat(service): introduce ConvenientService::Service::Plugins::CanHave…
Browse files Browse the repository at this point in the history
…Steps.step?
  • Loading branch information
marian13 committed Jun 3, 2024
1 parent 0637171 commit 340d327
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/convenient_service/service/plugins/can_have_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,44 @@
require_relative "can_have_steps/commands"
require_relative "can_have_steps/concern"
require_relative "can_have_steps/entities"

module ConvenientService
module Service
module Plugins
module CanHaveSteps
class << self
##
# Checks whether an object is a step instance.
#
# @api public
#
# @param step [Object] Can be any type.
# @return [Boolean]
#
# @example Simple usage.
# class Service
# include ConvenientService::Standard::Config
#
# step :result
#
# def result
# success
# end
# end
#
# step = Service.new.steps.first
#
# ConvenientService::Plugins::Service::CanHaveSteps.step?(step)
# # => true
#
# ConvenientService::Plugins::Service::CanHaveSteps.step?(42)
# # => false
#
def step?(step)
Commands::IsStep[step: step]
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative "commands/create_step_class"
require_relative "commands/is_step"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module CanHaveSteps
module Commands
class IsStep < Support::Command
##
# @!attribute [r] step
# @return [Object] Can be any type.
#
attr_reader :step

##
# @param step [Object] Can be any type.
# @return [void]
#
def initialize(step:)
@step = step
end

##
# @return [Boolean]
#
def call
step.class.include?(Entities::Step::Concern)
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Service::Plugins::CanHaveSteps::Commands::IsStep, type: :standard do
example_group "class methods" do
describe ".call" do
let(:command_result) { described_class.call(step: step) }

context "when `step` class does NOT include `ConvenientService::Service::Plugins::CanHaveSteps::Entities::Step::Concern`" do
let(:step) { 42 }

it "returns `false`" do
expect(command_result).to eq(false)
end
end

context "when `step` class includes `ConvenientService::Service::Plugins::CanHaveSteps::Entities::Step::Concern`" do
let(:service) do
Class.new do
include ConvenientService::Service::Configs::Essential

step :result

def result
success
end
end
end

let(:step) { service.new.steps.first }

it "returns `true`" do
expect(command_result).to eq(true)
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups
36 changes: 36 additions & 0 deletions spec/lib/convenient_service/service/plugins/can_have_steps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Service::Plugins::CanHaveSteps, type: :standard do
include ConvenientService::RSpec::Matchers::DelegateTo

example_group "class methods" do
describe ".step?" do
let(:service) do
Class.new do
include ConvenientService::Service::Configs::Essential

step :result

def result
success
end
end
end

let(:step) { service.new.steps.first }

specify do
expect { described_class.step?(step) }
.to delegate_to(ConvenientService::Service::Plugins::CanHaveSteps::Commands::IsStep, :call)
.with_arguments(step: step)
.and_return_its_value
end
end
end
end
# rubocop:enable RSpec/NestedGroups

0 comments on commit 340d327

Please sign in to comment.