Module: Cant::Editable

Included in:
Cant, Engine
Defined in:
lib/cant/engine.rb

Instance Method Summary collapse

Instance Method Details

#cant(options = {}, &block) ⇒ Object

add a Rule, as a pair of functions die

block form sets predicate function. block can have argument, (as any proc)

eg :

rooms = [:kitchen] 
cant {|context| rooms.include?(context[:room]) and context[:user]}
cant {current_user.admin?}

returns a rule which die function can be configured

cant do |controller|
  controller.request.path =~ /admin/ unless controller.current_user.admin?
end.die do |controller| 
  raise AccessDenied.new(controller.request)
end

options form looks for the predicate and die functions as values in options, under symbols :predicate and :die

cant :predicate => proc {|controller| not controller.current_user},

:die => proc {|controller| controller.redirect '/users/sign_in'}


35
36
37
38
39
40
41
42
43
# File 'lib/cant/engine.rb', line 35

def cant(options={}, &block)
  rule = if block.nil?
    Rule.new(options[:predicate], options[:die] || self.die)
  else
    Rule.new(block, self.die)
  end
  rules << rule
  rule
end

#die(&block) ⇒ Object

set default die function for this device

example :

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


51
52
53
54
# File 'lib/cant/engine.rb', line 51

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

#fold(&block) ⇒ Object

define fold function fold function has arity 2..n

  • rules : the rules to traverse

  • receiver : a Questionable asking for cant?

  • *args : the arguments for each rule to pass to predicate functions

returns a rule if strategy evaluates it cant do nil either

eg :

fold {true} #=> always cant
fold {|rules, _receiver, context| rules.reverse.find {|rule| rule.predicate?(context)}}


68
69
70
71
# File 'lib/cant/engine.rb', line 68

def fold(&block)
  @fold = block unless block.nil?
  @fold || Cant.fold
end

#rulesObject

list of Rules returns the list of rules for this device



5
6
7
8
9
10
11
# File 'lib/cant/engine.rb', line 5

def rules
  unless @rules
    @rules = []
    @rules.concat(Cant.rules) unless self == Cant
  end
  @rules
end