Class: Cannie::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/cannie/rule.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, subject, options = {}) ⇒ Rule

Initializes new rule

Parameters:

  • action (Symbol)

    action which should be permitted on subject

  • subject (String, Symbol)

    subject of the rule

  • options (Hash) (defaults to: {})

    additional options and conditions for the new rule

Options Hash (options):

  • :if (Proc)

    condition which is checked for a particular Permissions object and should be evaluated to true

  • :unless (Proc)

    condition which is checked for a particular Permissions object and should be evaluated to false



14
15
16
# File 'lib/cannie/rule.rb', line 14

def initialize(action, subject, options = {})
  @action, @subject, @_if, @_unless = action, subject, *options.values_at(:if, :unless)
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/cannie/rule.rb', line 3

def action
  @action
end

#subjectObject (readonly)

Returns the value of attribute subject.



3
4
5
# File 'lib/cannie/rule.rb', line 3

def subject
  @subject
end

Instance Method Details

#applies_to?(permissions) ⇒ Boolean

Checks whether rule is applied to permissions passed as an argument

Parameters:

Returns:

  • (Boolean)


22
23
24
# File 'lib/cannie/rule.rb', line 22

def applies_to?(permissions)
  if?(permissions) && unless?(permissions)
end