Class: Aequitas::Rule::Guard

Inherits:
Object
  • Object
show all
Extended by:
Equalizable
Defined in:
lib/aequitas/rule/guard.rb

Instance Attribute Summary collapse

Attributes included from Equalizable

#equalizer

Instance Method Summary collapse

Methods included from Equalizable

equalize_on

Constructor Details

#initialize(options = {}) ⇒ Guard

Returns a new instance of Guard.



15
16
17
18
# File 'lib/aequitas/rule/guard.rb', line 15

def initialize(options = {})
  @if_test     = options.fetch(:if,     nil)
  @unless_test = options.fetch(:unless, nil)
end

Instance Attribute Details

#if_testObject (readonly)

Returns the value of attribute if_test.



12
13
14
# File 'lib/aequitas/rule/guard.rb', line 12

def if_test
  @if_test
end

#unless_testObject (readonly)

Returns the value of attribute unless_test.



13
14
15
# File 'lib/aequitas/rule/guard.rb', line 13

def unless_test
  @unless_test
end

Instance Method Details

#allow?(resource) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if this RuleGuard allows the given resource by evaluating the if_test and unless_test

Parameters:

  • resource (Object)

    The resource that we check against.

Returns:

  • (Boolean)

    true if allowed, otherwise false.



30
31
32
33
34
35
36
37
38
# File 'lib/aequitas/rule/guard.rb', line 30

def allow?(resource)
  if if_test
    !!evaluate_conditional_clause(resource, if_test)
  elsif unless_test
    !evaluate_conditional_clause(resource, unless_test)
  else
    true
  end
end

#evaluate_conditional_clause(resource, clause) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



41
42
43
44
45
46
47
# File 'lib/aequitas/rule/guard.rb', line 41

def evaluate_conditional_clause(resource, clause)
  if clause.respond_to?(:call)
    clause.call(resource)
  elsif clause.kind_of?(Symbol)
    resource.__send__(clause)
  end
end