class FatModelAuth::GateKeeper

Public Class Methods

new(params) click to toggle source
# File lib/fat_model_auth/gate_keeper.rb, line 3
def initialize(params)
  @map = {}
  add_rules(params)
end

Public Instance Methods

add_rules(params) click to toggle source
# File lib/fat_model_auth/gate_keeper.rb, line 8
def add_rules(params)
  *methods, options = params
  auth_condition = options[:if] || negate(options[:unless])

  for method in methods
    @map["to_#{method}?".to_sym] = auth_condition
  end
end
check(model, user) click to toggle source
# File lib/fat_model_auth/gate_keeper.rb, line 17
def check(model, user)
  @model = model
  @user = user
  self
end
method_missing(method, *args) click to toggle source
# File lib/fat_model_auth/gate_keeper.rb, line 23
def method_missing(method, *args)
  unless @map.has_key? method
    raise NoMethodError, "undefined method allows(user).#{method} for #{@model.inspect}"
  end
  return false if @user.nil?

  @map[method].call(@model, @user)
end

Private Instance Methods

negate(predicate) click to toggle source
# File lib/fat_model_auth/gate_keeper.rb, line 34
def negate(predicate)
  proc do |*args|
    !predicate.call(*args)
  end
end