Skip to content

Commit

Permalink
✨ Allow to configure if raises with no limits
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeelaudibert committed Jul 21, 2023
1 parent bd8af3a commit 4fc0c3b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/clock/limiter/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def redis=(redis)
@redis = redis
end

# @return [::Redis]
def redis
raise ConfigurationError, 'Redis not configured' if @redis.nil?

Expand All @@ -27,11 +28,28 @@ def time_provider=(time_provider)
@time_provider = time_provider
end

# @return [Proc]
def time_provider
raise ConfigurationError, 'Time provider not configured' if @time_provider.nil?

@time_provider
end

# @param fail_with_empty_limits [Boolean]
def fail_with_empty_limits=(fail_with_empty_limits)
unless [true, false].include?(fail_with_empty_limits)
raise ConfigurationError, '`fail_with_empty_limits` must be a boolean'
end

@fail_with_empty_limits = fail_with_empty_limits
end

# @return [Boolean] - true by default
def fail_with_empty_limits?
return @fail_with_empty_limits unless @fail_with_empty_limits.nil?

true
end
end
end
end
9 changes: 7 additions & 2 deletions lib/clock/limiter/limiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def on_clock_limit_failure(&block)
# @raise [NoLimitsError] If no limits have been set
# @raise [Clock::Limiter::Period::InvalidError] If an invalid period has been set
def with_clock_limiter(limit_key = self.class.name)
raise NoLimitsError if self.class.clock_limits.nil? || self.class.clock_limits.empty?
raise NoLimitsError if fail_with_empty_limits?

self.class.clock_limits.each do |limit|
self.class.clock_limits&.each do |limit|
next if within_limit?(limit, limit_key)

return self.class.on_clock_limit_failure_block&.call(limit, limit_key)
Expand All @@ -76,6 +76,11 @@ def with_clock_limiter(limit_key = self.class.name)

private

def fail_with_empty_limits?
Clock::Limiter.configuration.fail_with_empty_limits? &&
(self.class.clock_limits.nil? || self.class.clock_limits.empty?)
end

# Increments the value of the key and returns true if the limit has not
# been reached. If the limit has been reached, false will be returned.
#
Expand Down
35 changes: 35 additions & 0 deletions test/clock/limiter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,47 @@ def call
assert_equal ['key_b'], failed_groups
end

def test_fails_with_empty_limit
configure_gem

my_class = Class.new do
include Clock::Limiter

def call
with_clock_limiter {}
end
end

assert_raises(Clock::Limiter::NoLimitsError) { my_class.new.call }
end

def test_doesnt_fail_with_empty_limit_if_properly_configured
configure_gem

Clock::Limiter.configure do |config|
config.fail_with_empty_limits = false
end

my_class = Class.new do
include Clock::Limiter

def call
with_clock_limiter {}
end
end

my_class.new.call

assert true
end

private

def configure_gem
Clock::Limiter.configure do |config|
config.redis = Redis.new
config.time_provider = -> { Time.now }
config.fail_with_empty_limits = true
end
end
end
Expand Down

0 comments on commit 4fc0c3b

Please sign in to comment.