Skip to content

Commit

Permalink
feat(entry): allow to define multiple entries at once
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Jan 4, 2024
1 parent def6bba commit a9bca88
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

require_relative "commands/define_entries"
require_relative "commands/define_entry"
Original file line number Diff line number Diff line change
@@ -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<String, Symbol>]
#
attr_reader :names

##
# @!attribute [r] body
# @return [Proc, nil]
#
attr_reader :body

##
# @param feature_class [Class]
# @param names [Array<String, Symbol>]
# @param body [Proc, nil]
#
def initialize(feature_class:, names:, body:)
@feature_class = feature_class
@names = names
@body = body
end

##
# @return [Array<String, Symbol>]
#
def call
names.map { |name| Commands::DefineEntry.call(feature_class: feature_class, name: name, body: body) }
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ module Concern

class_methods do
##
# @param name [String, Symbol]
# @param names [Array<String, Symbol>]
# @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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a9bca88

Please sign in to comment.