diff --git a/lib/convenient_service/feature/plugins/can_have_entries/commands.rb b/lib/convenient_service/feature/plugins/can_have_entries/commands.rb index 721d5677a73..be3cd051a5f 100644 --- a/lib/convenient_service/feature/plugins/can_have_entries/commands.rb +++ b/lib/convenient_service/feature/plugins/can_have_entries/commands.rb @@ -1,3 +1,4 @@ # frozen_string_literal: true +require_relative "commands/define_entries" require_relative "commands/define_entry" diff --git a/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries.rb b/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries.rb new file mode 100644 index 00000000000..b9f1adcabfa --- /dev/null +++ b/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module ConvenientService + module Feature + module Plugins + module CanHaveEntries + module Commands + class DefineEntries < Support::Command + ## + # @!attribute [r] feature_class + # @return [Class] + # + attr_reader :feature_class + + ## + # @!attribute [r] names + # @return [Array] + # + attr_reader :names + + ## + # @!attribute [r] body + # @return [Proc, nil] + # + attr_reader :body + + ## + # @param feature_class [Class] + # @param names [Array] + # @param body [Proc, nil] + # + def initialize(feature_class:, names:, body:) + @feature_class = feature_class + @names = names + @body = body + end + + ## + # @return [Array] + # + def call + names.map { |name| Commands::DefineEntry.call(feature_class: feature_class, name: name, body: body) } + end + end + end + end + end + end +end diff --git a/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb b/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb index 3c0398b6d08..988935ff4b1 100644 --- a/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb +++ b/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry.rb @@ -20,14 +20,14 @@ class DefineEntry < Support::Command ## # @!attribute [r] body - # @return [Proc] + # @return [Proc, nil] # attr_reader :body ## # @param feature_class [Class] # @param name [String, Symbol] - # @param body [Proc] + # @param body [Proc, nil] # def initialize(feature_class:, name:, body:) @feature_class = feature_class diff --git a/lib/convenient_service/feature/plugins/can_have_entries/concern.rb b/lib/convenient_service/feature/plugins/can_have_entries/concern.rb index bc79d890583..bea061dcd12 100644 --- a/lib/convenient_service/feature/plugins/can_have_entries/concern.rb +++ b/lib/convenient_service/feature/plugins/can_have_entries/concern.rb @@ -9,12 +9,12 @@ module Concern class_methods do ## - # @param name [String, Symbol] + # @param names [Array] # @param body [Proc, nil] # @return [String, Symbol] # - def entry(name, &body) - Commands::DefineEntry.call(feature_class: self, name: name, body: body) + def entry(*names, &body) + Commands::DefineEntries.call(feature_class: self, names: names, body: body) end end end diff --git a/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries_spec.rb b/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries_spec.rb new file mode 100644 index 00000000000..ad17b2364bd --- /dev/null +++ b/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entries_spec.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +require "spec_helper" + +require "convenient_service" + +# rubocop:disable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers +RSpec.describe ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntries do + example_group "class methods" do + describe ".call" do + include ConvenientService::RSpec::Matchers::DelegateTo + + subject(:command_result) { described_class.call(feature_class: feature_class, names: names, body: body) } + + let(:feature_class) do + Class.new do + include ConvenientService::Feature::Configs::Standard + end + end + + let(:feature_instance) { feature_class.new } + + let(:body) { proc { |*args, **kwargs, &block| [__method__, args, kwargs, block] } } + + context "when NO names are passed" do + let(:names) { [] } + + it "returns empty array" do + expect(command_result).to eq([]) + end + + specify do + expect { command_result }.not_to delegate_to(ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntry, :call) + end + end + + context "when one name is passed" do + let(:names) { [:foo] } + + it "returns that one name wrapped by array" do + expect(command_result).to eq([:foo]) + end + + specify do + expect { command_result } + .to delegate_to(ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntry, :call) + .with_arguments(feature_class: feature_class, name: names.first, body: body) + end + end + + context "when multiple names are passed" do + let(:names) { [:foo, :bar] } + + it "returns those multiple names" do + expect(command_result).to eq([:foo, :bar]) + end + + specify do + expect { command_result } + .to delegate_to(ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntry, :call) + .with_arguments(feature_class: feature_class, name: names.first, body: body) + end + + specify do + expect { command_result } + .to delegate_to(ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntry, :call) + .with_arguments(feature_class: feature_class, name: names.last, body: body) + end + end + end + end +end +# rubocop:enable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers diff --git a/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry_spec.rb b/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry_spec.rb index 7eb00b20b59..c22481a4d53 100644 --- a/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry_spec.rb +++ b/spec/lib/convenient_service/feature/plugins/can_have_entries/commands/define_entry_spec.rb @@ -12,8 +12,6 @@ example_group "class methods" do describe ".call" do - include ConvenientService::RSpec::Matchers::DelegateTo - subject(:command_result) { described_class.call(feature_class: feature_class, name: name, body: body) } let(:feature_class) do diff --git a/spec/lib/convenient_service/feature/plugins/can_have_entries/concern_spec.rb b/spec/lib/convenient_service/feature/plugins/can_have_entries/concern_spec.rb index e2f3fdb9666..25f8da4729b 100644 --- a/spec/lib/convenient_service/feature/plugins/can_have_entries/concern_spec.rb +++ b/spec/lib/convenient_service/feature/plugins/can_have_entries/concern_spec.rb @@ -39,13 +39,13 @@ example_group "class methods" do describe ".entry" do - let(:name) { :foo } + let(:names) { [:foo, :bar] } let(:body) { proc { :foo } } specify do - expect { feature_class.entry(name, &body) } - .to delegate_to(ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntry, :call) - .with_arguments(feature_class: feature_class, name: name, body: body) + expect { feature_class.entry(*names, &body) } + .to delegate_to(ConvenientService::Feature::Plugins::CanHaveEntries::Commands::DefineEntries, :call) + .with_arguments(feature_class: feature_class, names: names, body: body) .and_return_its_value end end