Module: BallotBox::Callbacks

Included in:
Manager
Defined in:
lib/ballot_box/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#_after_voteObject

Provides access to the callback array for before_vote :api: private



45
46
47
# File 'lib/ballot_box/callbacks.rb', line 45

def _after_vote
  @_after_vote ||= []
end

#_before_voteObject

Provides access to the callback array for before_vote :api: private



29
30
31
# File 'lib/ballot_box/callbacks.rb', line 29

def _before_vote
  @_before_vote ||= []
end

#_run_callbacks(kind, *args) ⇒ Object

Hook to _run_callbacks asserting for conditions.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/ballot_box/callbacks.rb', line 5

def _run_callbacks(kind, *args) #:nodoc:
  options = args.last # Last callback arg MUST be a Hash

  send("_#{kind}").each do |callback, conditions|
    invalid = conditions.find do |key, value|
      value.is_a?(Array) ? !value.include?(options[key]) : (value != options[key])
    end

    callback.call(*args) unless invalid
  end
end

#after_vote(options = {}, method = :push, &block) ⇒ Object

A callback that runs after vote created Example:

BallotBox::Manager.after_vote do |env, opts|
end

Raises:

  • (BlockNotGiven)


38
39
40
41
# File 'lib/ballot_box/callbacks.rb', line 38

def after_vote(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  _after_vote.send(method, [block, options])
end

#before_vote(options = {}, method = :push, &block) ⇒ Object

A callback that runs before create vote Example:

BallotBox::Manager.before_vote do |env, opts|
end

Raises:

  • (BlockNotGiven)


22
23
24
25
# File 'lib/ballot_box/callbacks.rb', line 22

def before_vote(options = {}, method = :push, &block)
  raise BlockNotGiven unless block_given?
  _before_vote.send(method, [block, options])
end