Class: Cant::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/cant/engine.rb

Overview

a Rule is a pair of functions :

  • predicate(*args), that return true if predicate is met (hint of predicate?)

  • die(*args), that cant raise if convenient

this class could have been:

  • spared

  • an Array, with an optional syntactic sugar

  • a property list in erlang

Instance Method Summary collapse

Constructor Details

#initialize(predicate = nil, die = Cant.die) ⇒ Rule

a new rule with a predicate and response function



130
131
132
133
# File 'lib/cant/engine.rb', line 130

def initialize(predicate=nil, die=Cant.die)
  @predicate=predicate
  @die = die
end

Instance Method Details

#die(&block) ⇒ Object

set die function using block

example :

die do |request|
  raise AccessDenied, "Cant process #{request}"
end


156
157
158
159
# File 'lib/cant/engine.rb', line 156

def die(&block)
  @die = block unless block.nil?
  @die
end

#die!(*args) ⇒ Object

call die function with args

*args - variable list of arguments



163
164
165
# File 'lib/cant/engine.rb', line 163

def die!(*args)
  die.call(*args)
end

#predicate(&block) ⇒ Object

set or return predicate function using block

return true means rule can die

example :

predicate do |request|
  not current_user.admin? if request.path =~ /^\/admin/ 
end


142
143
144
145
# File 'lib/cant/engine.rb', line 142

def predicate(&block)
  @predicate = block unless block.nil?
  @predicate
end

#predicate?(*args) ⇒ Boolean

evaluates predicate function with args

Returns:

  • (Boolean)


147
148
149
# File 'lib/cant/engine.rb', line 147

def predicate?(*args)
  predicate.call(*args)
end