Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand functionality: limit group for adapter #41

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/yabeda/dsl/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,23 @@ def temporary_tags
#
# @param adapter_names [Array<Symbol>] Names of adapters to use
def adapter(*adapter_names, group: @group)
raise ConfigurationError, "Adapter limitation can't be defined outside of group" unless group
raise ConfigurationError, "Adapter limitation can't be defined without adapter_names" if adapter_names.empty?

@adapter_names = adapter_names
if group
include_group(group)
else
return yield if block_given?

raise ConfigurationError, "Should be block passed with call .include_group for example"
end
end

def include_group(group)
raise ConfigurationError, "Adapter limitation can't be defined without of group name" unless group

Yabeda.groups[group] ||= Yabeda::Group.new(group)
Yabeda.groups[group].adapter(*adapter_names)
Yabeda.groups[group].adapter(*@adapter_names)
end

private
Expand Down
98 changes: 88 additions & 10 deletions spec/yabeda/counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@

let(:tags) { { foo: "bar" } }
let(:metric_value) { 10 }
let(:counter) { Yabeda.test_counter }
let(:built_tags) { { built_foo: "built_bar" } }
let(:adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }
let(:counter) { Yabeda.test_counter }

before do
Yabeda.register_adapter(:test_adapter, adapter)
Yabeda.configure do
counter :test_counter
end
Yabeda.configure! unless Yabeda.already_configured?
allow(Yabeda::Tags).to receive(:build).with(tags, anything).and_return(built_tags)
allow(Yabeda::Tags).to receive(:build).with({}, anything).and_return({})
Yabeda.register_adapter(:test_adapter, adapter)
end

it { expect(increment_counter).to eq(metric_value) }
Expand All @@ -25,30 +22,111 @@
expect { counter.increment }.to change { counter.values[{}] }.by(1)
end

it "execute perform_counter_increment! method of adapter" do
it "execute perform_counter_increment!" do
increment_counter
expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value)
expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
end

context "with adapter option" do
let(:counter) { Yabeda.counter_with_adapter }
let(:another_adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }
let(:counter) { Yabeda.counter_with_adapter }

before do
Yabeda.register_adapter(:another_adapter, another_adapter)

Yabeda.configure do
counter :counter_with_adapter, adapter: :test_adapter
end
Yabeda.configure! unless Yabeda.already_configured?
end

it "execute perform_counter_increment! method of adapter with name :test_adapter" do
it "execute perform_counter_increment! with name :test_adapter" do
increment_counter

aggregate_failures do
expect(adapter).to have_received(:perform_counter_increment!).with(counter, built_tags, metric_value)
expect(adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
expect(another_adapter).not_to have_received(:perform_counter_increment!)
end
end
end

context "with call .adapter method" do
let(:tags) { { type: "champignon" } }
let(:counter) { Yabeda.mushrooms.champignon_counter }
let(:basket_adapter) { instance_double(Yabeda::BaseAdapter, perform_counter_increment!: true, register!: true) }

before do
Yabeda.register_adapter(:basket_adapter, basket_adapter)
end

it "raises an error using include_group construction without adapter_names args" do
expect do
Yabeda.configure do
group :mushrooms do
counter :champignon_counter
end

adapter { include_group :mushrooms }
end
Yabeda.configure! unless Yabeda.already_configured?
end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names")
end

it "raises an error using adapter construction into the group block without adapter_names args" do
expect do
Yabeda.configure do
group :mushrooms do
adapter
counter :champignon_counter
end
end
Yabeda.configure! unless Yabeda.already_configured?
end.to raise_error(Yabeda::ConfigurationError, "Adapter limitation can't be defined without adapter_names")
end

context "when call .adapter method in outside of group" do
before do
Yabeda.configure do
group :mushrooms do
counter :champignon_counter
end

adapter :basket_adapter do
include_group :mushrooms
end
end
Yabeda.configure! unless Yabeda.already_configured?
end

it "execute perform_counter_increment! with name :basket_adapter" do
increment_counter

aggregate_failures do
expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
expect(adapter).not_to have_received(:perform_counter_increment!)
end
end
end

context "when call adapter method in inside of group" do
before do
Yabeda.configure do
group :mushrooms do
adapter :basket_adapter
counter :champignon_counter
end
end
Yabeda.configure! unless Yabeda.already_configured?
end

it "execute perform_counter_increment! with name :basket_adapter" do
increment_counter

aggregate_failures do
expect(basket_adapter).to have_received(:perform_counter_increment!).with(counter, tags, metric_value)
expect(adapter).not_to have_received(:perform_counter_increment!)
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/yabeda/dsl/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
expect do
Yabeda.configure { adapter :test }
Yabeda.configure! unless Yabeda.already_configured?
end.to raise_error(Yabeda::ConfigurationError, /can't be defined outside of group/)
end.to raise_error(Yabeda::ConfigurationError, "Should be block passed with call .include_group for example")
end
end

Expand Down
7 changes: 1 addition & 6 deletions spec/yabeda/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

RSpec.describe Yabeda::Group do
let(:name) { nil }

let(:group) { described_class.new(name) }

before do
Yabeda.groups[name] = group
end

after { Yabeda.reset! }
before { Yabeda.groups[name] = group }

describe "default tags" do
context "when on the top level group" do
Expand Down
2 changes: 2 additions & 0 deletions spec/yabeda_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

RSpec.describe Yabeda do
before { described_class.reset! }

it "has a version number" do
expect(Yabeda::VERSION).not_to be_nil
end
Expand Down