Skip to content

Commit

Permalink
Fix counter increment for Ruby 2.x
Browse files Browse the repository at this point in the history
Adding default values to arguments in 7df0d8e broke these methods in Ruby 2.x due to the differences in keyword arguments handling in Ruby 2.x that were fixed in Ruby 3.x

See https://www.ruby-lang.org/en/news/2019/12/12/separation-of-positional-and-keyword-arguments-in-ruby-3-0/ for more details

Fixes #40
  • Loading branch information
Envek committed Oct 11, 2024
1 parent b9bb7c4 commit 5b890ed
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Fixed

- Compatibility with Ruby 2.x, broken in 0.13.0 due to differences of keywords handling in Ruby 2.x for case when method has arguments with default values. [@Envek]

## 0.13.0 - 2024-10-02

### Added
Expand Down
2 changes: 2 additions & 0 deletions lib/yabeda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
module Yabeda
include DSL

EMPTY_TAGS = {}.freeze

class << self
extend Forwardable

Expand Down
27 changes: 26 additions & 1 deletion lib/yabeda/counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
module Yabeda
# Growing-only counter
class Counter < Metric
def increment(tags = {}, by: 1)
# @overload increment(tags = {}, by: 1)
# Increment the counter for given set of tags by the given increment value
# @param tags Hash{Symbol=>#to_s} tags to identify the counter
# @param by [Integer] strictly positive increment value
def increment(*args)
tags, by = self.class.parse_args(*args)
all_tags = ::Yabeda::Tags.build(tags, group)
values[all_tags] += by
adapters.each_value do |adapter|
Expand All @@ -15,5 +20,25 @@ def increment(tags = {}, by: 1)
def values
@values ||= Concurrent::Hash.new(0)
end

# @api private
# rubocop:disable Metrics/MethodLength
def self.parse_args(*args)
case args.size
when 0 # increment()
[EMPTY_TAGS, 1]
when 1 # increment(by: 5) or increment(tags)
if args[0].key?(:by)
[EMPTY_TAGS, args.fetch(:by)]
else
[args[0], 1]
end
when 2 # increment(tags, by: 5)
[args[0], args[1].fetch(:by, 1)]
else
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 0..2)"
end
end
# rubocop:enable Metrics/MethodLength
end
end
14 changes: 12 additions & 2 deletions lib/yabeda/gauge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ def set(tags, value)
value
end

def increment(tags = {}, by: 1)
# @overload increment(tags = {}, by: 1)
# Convenience method to increment current gauge value for given set of tags by the given increment value
# @param tags Hash{Symbol=>#to_s} tags to identify the gauge
# @param by [Integer] increment value
def increment(*args)
tags, by = Counter.parse_args(*args)
set(tags, get(tags).to_i + by)
end

def decrement(tags = {}, by: 1)
# @overload decrement(tags = {}, by: 1)
# Convenience method to decrement current gauge value for given set of tags by the given decrement value
# @param tags Hash{Symbol=>#to_s} tags to identify the gauge
# @param by [Integer] decrement value
def decrement(*args)
tags, by = Counter.parse_args(*args)
set(tags, get(tags).to_i - by)
end
end
Expand Down

0 comments on commit 5b890ed

Please sign in to comment.