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.



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

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



24
25
26
27
28
29
30
31
# File 'lib/fat_model_auth/gate_keeper.rb', line 24

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



9
10
11
12
13
14
15
16
# File 'lib/fat_model_auth/gate_keeper.rb', line 9

def add_rules(params)
  auth_condition = params.pop[:if]
  methods = params
  
  for method in methods
    @map["to_#{method}?".to_sym] = auth_condition
  end
end

#check(model, user) ⇒ Object



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

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