Class: Allowed::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/allowed/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(limit, options = {}) ⇒ Throttle

Returns a new instance of Throttle.



5
6
7
8
# File 'lib/allowed/throttle.rb', line 5

def initialize(limit, options = {})
  @limit   = limit
  @options = options
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



3
4
5
# File 'lib/allowed/throttle.rb', line 3

def limit
  @limit
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/allowed/throttle.rb', line 3

def options
  @options
end

Instance Method Details

#callbackObject



10
11
12
# File 'lib/allowed/throttle.rb', line 10

def callback
  options.fetch(:callback, -> (record) { })
end

#messageObject



14
15
16
# File 'lib/allowed/throttle.rb', line 14

def message
  options.fetch(:message, "Limit reached.")
end

#valid?(record) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
# File 'lib/allowed/throttle.rb', line 18

def valid?(record)
  return true if skip?(record)

  scope_for(record).count < allowed_count(record)
end