Class: Aegis::Sieve

Inherits:
Object
  • Object
show all
Defined in:
lib/aegis/sieve.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role_name, effect, block = nil) ⇒ Sieve

Returns a new instance of Sieve.



4
5
6
7
8
9
# File 'lib/aegis/sieve.rb', line 4

def initialize(role_name, effect, block = nil)
  role_name = 'everyone' if role_name.blank?
  @role_name = role_name.to_s
  @effect = effect
  @block = block
end

Class Method Details

.allow_to_allObject



29
30
31
# File 'lib/aegis/sieve.rb', line 29

def self.allow_to_all
  new('everyone', true)
end

.deny_to_allObject



33
34
35
# File 'lib/aegis/sieve.rb', line 33

def self.deny_to_all
  new('everyone', false)
end

Instance Method Details

#inspectObject



25
26
27
# File 'lib/aegis/sieve.rb', line 25

def inspect
  "Sieve(#{{:role_name => @role_name, :effect => @effect ? :allow : :deny, :block => @block.present?}.inspect})"
end

#may?(context, *args) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aegis/sieve.rb', line 11

def may?(context, *args)
  matches_role = @role_name == 'everyone' || @role_name == context.role.name
  if matches_role
    if @block
      block_result = context.instance_exec(*args, &@block)
      block_result ? @effect : !@effect
    else
      @effect
    end
  else
    nil
  end
end