Class: FatModelAuth::GateKeeper

Inherits:
Object
  • Object
show all
Defined in:
lib/fat_model_auth/gate_keeper.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ GateKeeper

Returns a new instance of GateKeeper.



3
4
5
6
# File 'lib/fat_model_auth/gate_keeper.rb', line 3

def initialize(params)
  @map = {}
  add_rules(params)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



23
24
25
26
27
28
29
30
# 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

Instance Method Details

#add_rules(params) ⇒ Object



8
9
10
11
12
13
14
15
# 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) ⇒ Object



17
18
19
20
21
# File 'lib/fat_model_auth/gate_keeper.rb', line 17

def check(model, user)
  @model = model
  @user = user
  self
end