Module: Attestor::Policy

Extended by:
Factory, Validations::ClassMethods
Includes:
Validations
Included in:
Node
Defined in:
lib/attestor/policy.rb,
lib/attestor/policy/or.rb,
lib/attestor/policy/and.rb,
lib/attestor/policy/not.rb,
lib/attestor/policy/xor.rb,
lib/attestor/policy/node.rb,
lib/attestor/policy/factory.rb,
lib/attestor/policy/negator.rb

Overview

API for policies that validate another objects

Defined Under Namespace

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations::ClassMethods

validate, validates, validations, validators

Methods included from Validations

#invalid, #validate, #validate!

Class Method Details

.included(klass) ⇒ Object



12
13
14
15
16
17
# File 'lib/attestor/policy.rb', line 12

def self.included(klass)
  klass.instance_eval do
    include Validations
    extend Factory
  end
end

.new(*attributes, &block) { ... } ⇒ Class

Builds a policy class with given attributes

Examples:

MyPolicy = Attestor::Policy.new(:foo, :bar) do
  attr_reader :baz
end

Parameters:

  • attributes (Array<#to_sym>)
  • block (Proc)

Yields:

  • the block in the scope of created class

Returns:

  • (Class)

    the policy class, based on Struct



32
33
34
35
36
37
# File 'lib/attestor/policy.rb', line 32

def self.new(*attributes, &block)
  Struct.new(*attributes) do
    include Policy
    class_eval(&block) if block_given?
  end
end

Instance Method Details

#and(policy, *others) ⇒ Attestor::Policy::And #and(policy) ⇒ Attestor::Policy::Negator

Builds the AND composition of current policy with other policies

Overloads:



67
68
69
# File 'lib/attestor/policy.rb', line 67

def and(*others)
  self.class.and(self, *others)
end

#notAttestor::Policy::Not

Negates the current policy



42
43
44
# File 'lib/attestor/policy.rb', line 42

def not
  self.class.not(self)
end

#or(policy, *others) ⇒ Attestor::Policy::And #or(policy) ⇒ Attestor::Policy::Negator

Builds the OR composition of current policy with other policies

Overloads:



92
93
94
# File 'lib/attestor/policy.rb', line 92

def or(*others)
  self.class.or(self, *others)
end

#xor(policy, *others) ⇒ Attestor::Policy::And #xor(policy) ⇒ Attestor::Policy::Negator

Builds the XOR composition of current policy with other policies

Overloads:



117
118
119
# File 'lib/attestor/policy.rb', line 117

def xor(*others)
  self.class.xor(self, *others)
end