Class: ActionThrottler::Actions
- Inherits:
-
Object
- Object
- ActionThrottler::Actions
- Defined in:
- lib/action_throttler/actions.rb
Class Method Summary collapse
-
.add(action = nil, &block) ⇒ Object
Adds an action to the stack.
-
.can_be_run?(action, ref = "") ⇒ Boolean
Checks the database to see if an action can be run.
- .can_run(action, ref = "") ⇒ Object
- .cannot_be_run?(action, ref = "") ⇒ Boolean
- .cannot_run(action, ref = "") ⇒ Object
-
.run(action, ref = "") ⇒ Boolean
Runs an action and registers it in the database.
Class Method Details
.add(action = nil, &block) ⇒ Object
Adds an action to the stack
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/action_throttler/actions.rb', line 8 def add(action = nil, &block) @actions ||= {} raise "A name to the ActionThrottler action is required!" if action.nil? config = ActionThrottler::Config.new block.call(config) @actions[action] = config.to_hash end |
.can_be_run?(action, ref = "") ⇒ Boolean
Checks the database to see if an action can be run
24 25 26 27 28 29 30 31 |
# File 'lib/action_throttler/actions.rb', line 24 def can_be_run?(action, ref = "") ::ActionThrottlerLog.all(:conditions => [ "scope = ? AND reference = ? AND created_at >= ?", action.to_s, normalise_ref(ref), @actions[action][:duration].ago ]).size < @actions[action][:limit] ? true : false end |
.can_run(action, ref = "") ⇒ Object
55 56 57 |
# File 'lib/action_throttler/actions.rb', line 55 def can_run(action, ref = "") run(action, ref) end |
.cannot_be_run?(action, ref = "") ⇒ Boolean
34 35 36 |
# File 'lib/action_throttler/actions.rb', line 34 def cannot_be_run?(action, ref = "") not can_be_run?(action, ref) end |
.cannot_run(action, ref = "") ⇒ Object
60 61 62 |
# File 'lib/action_throttler/actions.rb', line 60 def cannot_run(action, ref = "") ! run(action, ref) end |
.run(action, ref = "") ⇒ Boolean
Runs an action and registers it in the database
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/action_throttler/actions.rb', line 43 def run(action, ref = "") if can_be_run?(action, ref) ::ActionThrottlerLog.create({ :scope => action.to_s, :reference => normalise_ref(ref) }) else false end end |