Class: FatModelAuth::GateKeeper

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

Defined Under Namespace

Classes: Checker

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ GateKeeper

Returns a new instance of GateKeeper.



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

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

Instance Method Details

#add_rules(params) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/fat_model_auth/gate_keeper.rb', line 10

def add_rules(params)
  *methods, options = params
  unless options.is_a?(Hash) && (options[:if] || options[:unless])
    raise ArgumentError, 'allows requires an :if or :unless condition'
  end

  auth_condition = options[:if] || negate(options[:unless])

  methods.each do |method|
    key = :"to_#{method}?"
    raise ArgumentError, "rule for :#{method} is already defined" if @map.key?(key)

    @map[key] = auth_condition
  end
end

#check(model, user) ⇒ Object



26
27
28
# File 'lib/fat_model_auth/gate_keeper.rb', line 26

def check(model, user)
  Checker.new(@map, model, user)
end