Skip to content

Commit

Permalink
Feature/Add counters module (#12)
Browse files Browse the repository at this point in the history
* 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
H3nSte1n authored Apr 20, 2022
1 parent 866d17a commit 53190de
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 3 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,36 @@ def perform(model_id)
end
```

## Counters

There is a counter wrapper that you can use to add custom counters to the meta attribute. To do this, you need to complete the following tasks:
- include WorkerTools::Counters to your class
- add :counters to the wrappers method props
- call counters method with your custom counters
You can see an example below. After that, you can access your custom counters via the meta attribute.

```ruby
class MyImporter
include WorkerTools::Counters
wrappers :counters
counters :foo, :bar

def run
example_foo_counter_methods
end

def example_foo_counter_methods
self.foo = 0

10.times { increment_foo }

puts foo # foo == 10
end

# ..
end
```

## Benchmark

There is a benchmark wrapper that you can use to record the benchmark. The only thing you need to do is to include the benchmark module and append the name to the wrapper array. Below you can see an example of the integration.
Expand All @@ -217,7 +247,6 @@ class MyImporter
end
```


## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/i22-digitalagentur/worker_tools.
Expand Down
1 change: 1 addition & 0 deletions lib/worker_tools/basics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def run

def perform(model_id = nil)
@model_id = model_id

with_wrappers(wrapper_methods) do
run
end
Expand Down
41 changes: 41 additions & 0 deletions lib/worker_tools/counters.rb
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
1 change: 1 addition & 0 deletions lib/worker_tools/recorder.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module WorkerTools
module Recorder

def with_wrapper_recorder(&block)
block.yield
# this time we do want to catch Exception to attempt to handle some of the
Expand Down
2 changes: 1 addition & 1 deletion lib/worker_tools/version.rb
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
2 changes: 1 addition & 1 deletion test/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
t.integer 'kind', null: false
t.integer 'state', default: 0, null: false
t.text 'information'
t.json :meta, default: {}
t.json 'meta', default: {}
t.timestamps
end
end
122 changes: 122 additions & 0 deletions test/worker_tools/counters_test.rb
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

0 comments on commit 53190de

Please sign in to comment.