-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add custom counters * feat: Add minitests for new counter feature * feat: Update minitests from new counter feature * feat: Add Counters section to README * feat: Add counter method tests and enrich README with an example of a counter
- Loading branch information
Showing
7 changed files
with
197 additions
and
3 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 |
---|---|---|
|
@@ -33,6 +33,7 @@ def run | |
|
||
def perform(model_id = nil) | ||
@model_id = model_id | ||
|
||
with_wrappers(wrapper_methods) do | ||
run | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module WorkerTools | ||
module Counters | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def self.counters(*args) | ||
@counters ||= args.flatten | ||
add_counter_methods | ||
end | ||
|
||
def self.read_counters | ||
@counters || [] | ||
end | ||
|
||
def self.add_counter_methods | ||
@counters.each do |name| | ||
define_method name do | ||
model.meta[name] | ||
end | ||
define_method "#{name}=" do |value| | ||
model.meta[name] = value | ||
end | ||
define_method "increment_#{name}" do | ||
model.meta[name] += 1 | ||
end | ||
end | ||
end | ||
|
||
def with_wrapper_counters(&block) | ||
reset_counters | ||
block.call | ||
end | ||
|
||
def reset_counters | ||
self.class.read_counters.each do |name| | ||
model.meta[name] = 0 | ||
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module WorkerTools | ||
VERSION = '0.2.3'.freeze | ||
VERSION = '0.2.2'.freeze | ||
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
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,122 @@ | ||
require 'test_helper' | ||
|
||
describe WorkerTools::Counters do | ||
class Counter | ||
include WorkerTools::Basics | ||
include WorkerTools::Counters | ||
|
||
wrappers :basics, :counters | ||
counters :foo, :bar | ||
|
||
def model_class | ||
Import | ||
end | ||
|
||
def model_kind | ||
'foo' | ||
end | ||
|
||
def run; end | ||
end | ||
|
||
describe '#counters' do | ||
before :each do | ||
import = create_import | ||
@importer = Counter.new | ||
@importer.perform(import) | ||
end | ||
|
||
it 'returns an array of counters' do | ||
expect(@importer.class.read_counters).must_equal [:foo, :bar] | ||
end | ||
|
||
it 'creates for each counter a getter method' do | ||
@importer.class.read_counters.each do |counter| | ||
expect(@importer.respond_to?(counter)).must_equal true | ||
end | ||
end | ||
|
||
it 'creates for each counter a setter method' do | ||
@importer.class.read_counters.each do |counter| | ||
expect(@importer.respond_to?("#{counter}=")).must_equal true | ||
end | ||
end | ||
|
||
it 'creates for each counter an incrementer method' do | ||
@importer.class.read_counters.each do |counter| | ||
expect(@importer.respond_to?("increment_#{counter}")).must_equal true | ||
end | ||
end | ||
|
||
describe '#counters_increment' do | ||
it 'increments the counter by 1' do | ||
@importer.class.read_counters.each do |counter| | ||
@importer.send("#{counter}=", 0) | ||
@importer.send("increment_#{counter}") | ||
expect(@importer.send(counter)).must_equal 1 | ||
end | ||
end | ||
end | ||
|
||
describe '#counter=' do | ||
it 'overwrites the current counter value' do | ||
@importer.class.read_counters.each do |counter| | ||
@importer.send("#{counter}=", 5) | ||
expect(@importer.send(counter)).must_equal 5 | ||
end | ||
|
||
@importer.class.read_counters.each do |counter| | ||
@importer.send("#{counter}=", 2) | ||
expect(@importer.send(counter)).must_equal 2 | ||
end | ||
end | ||
end | ||
|
||
describe '#counter' do | ||
it 'returns value of counter' do | ||
@importer.class.read_counters.each do |counter| | ||
@importer.send("#{counter}=", 2) | ||
expect(@importer.send(counter)).must_equal 2 | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '#with_wrapper_counters' do | ||
before :each do | ||
@import = create_import | ||
@importer = Counter.new | ||
end | ||
|
||
it 'should call reset_counters function' do | ||
@importer.expects(:reset_counters).returns(true) | ||
@importer.perform(@import) | ||
end | ||
|
||
it 'raise error if model.meta not exist' do | ||
err = assert_raises(StandardError) { @importer.with_wrapper_counters } | ||
assert_includes err.message, 'Model not available' | ||
end | ||
end | ||
|
||
describe 'reset_counters' do | ||
before :each do | ||
import = create_import | ||
@importer = Counter.new | ||
@importer.perform(import) | ||
end | ||
|
||
it 'resets the counters' do | ||
@importer.class.read_counters.each do |counter| | ||
@importer.send("#{counter}=", 1) | ||
expect(@importer.send(counter)).must_equal 1 | ||
end | ||
|
||
@importer.reset_counters | ||
|
||
@importer.class.read_counters.each do |counter| | ||
expect(@importer.send(counter)).must_equal 0 | ||
end | ||
end | ||
end | ||
end |