Module: Policy::Base

Included in:
Node
Defined in:
lib/policy/base.rb,
lib/policy/base/or.rb,
lib/policy/base/and.rb,
lib/policy/base/not.rb,
lib/policy/base/xor.rb,
lib/policy/base/node.rb,
lib/policy/base/negator.rb

Overview

Policy object class interface

Includes ActiveModel::Validation and a list of methods to compose policies.

Defined Under Namespace

Classes: And, Negator, Node, Not, Or, Xor

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



109
110
111
# File 'lib/policy/base.rb', line 109

def self.included(klass)
  klass.instance_eval { include ActiveModel::Validations }
end

Instance Method Details

#and(*others) ⇒ Policy::Base #and#not

Composes the policy with others by AND method

Overloads:

  • #and(*others) ⇒ Policy::Base

    Returns a composition of the policy with other policies

    The composition is valid when all policies are valid.

    Examples:

    composition = one_policy.and(another)
    one_policy.valid?  # => true
    another.valid?     # => false
    composition.valid? # => false

    Parameters:

    Returns:

  • #and#not

    Returns a negator object expecting negation of another policy

    Examples:

    composition = one_policy.and.not(another)
    one_policy.valid?  # => true
    another.valid?     # => false
    composition.valid? # => true

    Returns:



39
40
41
# File 'lib/policy/base.rb', line 39

def and(*others)
  compose And, others
end

#or(*others) ⇒ Policy::Base #or#not

Composes the policy with others by OR method

Overloads:

  • #or(*others) ⇒ Policy::Base

    Returns a composition of the policy with other policies

    The composition is valid when any policy is valid.

    Examples:

    composition = one_policy.or(another)
    one_policy.valid?  # => true
    another.valid?     # => false
    composition.valid? # => true

    Parameters:

    Returns:

  • #or#not

    Returns a negator object expecting negation of another policy

    Examples:

    composition = one_policy.or.not(another)
    one_policy.valid?  # => false
    another.valid?     # => false
    composition.valid? # => true

    Returns:



71
72
73
# File 'lib/policy/base.rb', line 71

def or(*others)
  compose Or, others
end

#xor(another) ⇒ Policy::Base #xor#not

Composes the policy with others by XOR method

Overloads:

  • #xor(another) ⇒ Policy::Base

    Returns a composition of the policy with other policies

    The composition is valid when both valid and invalid policies are present

    Examples:

    composition = one_policy.xor(another)
    one_policy.valid?  # => true
    another.valid?     # => true
    composition.valid? # => false

    Parameters:

    Returns:

  • #xor#not

    Returns a negator object expecting negation of another policy

    Examples:

    composition = one_policy.xor.not(another)
    one_policy.valid?  # => false
    another.valid?     # => false
    composition.valid? # => true

    Returns:



104
105
106
# File 'lib/policy/base.rb', line 104

def xor(*others)
  compose Xor, others
end