Class: Limited::Action
- Inherits:
-
Object
- Object
- Limited::Action
- Defined in:
- lib/limited/action.rb
Overview
This class represents an action which can be executed a limited amount of times
Instance Attribute Summary collapse
-
#actors ⇒ Object
readonly
users which have already executed this action they are differenciated by the identifier object.
-
#identifier ⇒ Object
readonly
an object used to distinguish users.
-
#limit ⇒ Object
readonly
the amount of times the action can be executed.
-
#name ⇒ Object
readonly
a symbol for the name of the action.
Instance Method Summary collapse
-
#actor(attributes) ⇒ Object
get a user which can execute this action by the attributes.
-
#executed(actor_attributes = nil) ⇒ Object
should be called everytime the action gets executed so the internal counter is always up-to-date.
-
#initialize(name, limit, interval = nil, identifier = []) ⇒ Action
constructor
:call-seq: Limited::Action.new(name, limit) -> Limited::Action.
-
#interval ⇒ Object
returns the interval for which the limit counts.
-
#limit_reached(actor_attributes = nil) ⇒ Object
wheter the limit of executions has been exceeded.
-
#num_executed(actor_attributes = nil) ⇒ Object
returns the amount of times the action already has been executed.
-
#num_left(actor_attributes = nil) ⇒ Object
returns how many times the action can be executed until the given limit is reached.
Constructor Details
#initialize(name, limit, interval = nil, identifier = []) ⇒ Action
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/limited/action.rb', line 26 def initialize(name, limit, interval = nil, identifier = []) raise ArgumentError.new("Limited::Action::name needs to be a Symbol") unless name.is_a?(Symbol) raise ArgumentError.new("Limited::Action::limit needs to be an Integer") unless limit.is_a?(Integer) raise ArgumentError.new("Limited::Action::limit needs to be positive") if limit < 0 @name = name @limit = limit @num_executed = 0 @interval = Interval.new(interval.nil? ? :endless : interval) @identifier = identifier.is_a?(Limited::Actor::Identifier) ? identifier : Limited::Actor::Identifier.new(*identifier) @actors = [] check_new_interval end |
Instance Attribute Details
#actors ⇒ Object (readonly)
users which have already executed this action they are differenciated by the identifier object
15 16 17 |
# File 'lib/limited/action.rb', line 15 def actors @actors end |
#identifier ⇒ Object (readonly)
an object used to distinguish users
12 13 14 |
# File 'lib/limited/action.rb', line 12 def identifier @identifier end |
#limit ⇒ Object (readonly)
the amount of times the action can be executed
10 11 12 |
# File 'lib/limited/action.rb', line 10 def limit @limit end |
#name ⇒ Object (readonly)
a symbol for the name of the action
8 9 10 |
# File 'lib/limited/action.rb', line 8 def name @name end |
Instance Method Details
#actor(attributes) ⇒ Object
get a user which can execute this action by the attributes
if the actor doesn’t exist yet it is created
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/limited/action.rb', line 93 def actor(attributes) actor = nil @actors.each do |current_actor| if current_actor.attributes == attributes actor = current_actor end end @actors << actor = Limited::Actor.new(@identifier, attributes) if actor.nil? actor end |
#executed(actor_attributes = nil) ⇒ Object
should be called everytime the action gets executed so the internal counter is always up-to-date
70 71 72 73 74 75 76 77 |
# File 'lib/limited/action.rb', line 70 def executed(actor_attributes = nil) check_new_interval if actor_attributes.is_a?(Hash) actor_executed(actor_attributes) else @num_executed += 1 end end |
#interval ⇒ Object
returns the interval for which the limit counts
43 44 45 46 |
# File 'lib/limited/action.rb', line 43 def interval check_new_interval @interval end |
#limit_reached(actor_attributes = nil) ⇒ Object
wheter the limit of executions has been exceeded
82 83 84 85 86 |
# File 'lib/limited/action.rb', line 82 def limit_reached(actor_attributes = nil) check_new_interval return @limit <= @num_executed unless actor_attributes.is_a?(Hash) actor_limit_reached(actor_attributes) end |
#num_executed(actor_attributes = nil) ⇒ Object
returns the amount of times the action already has been executed
51 52 53 54 55 |
# File 'lib/limited/action.rb', line 51 def num_executed(actor_attributes = nil) check_new_interval return @num_executed unless actor_attributes.is_a?(Hash) actor_num_executed(actor_attributes) end |
#num_left(actor_attributes = nil) ⇒ Object
returns how many times the action can be executed until the given limit is reached
60 61 62 63 64 |
# File 'lib/limited/action.rb', line 60 def num_left(actor_attributes = nil) check_new_interval return @limit - @num_executed unless actor_attributes.is_a?(Hash) actor_num_left(actor_attributes) end |