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 validates 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

follow_policy, follow_validator, validate, validators

Methods included from Validations

#invalid, #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

Yields:

  • the block in the scope of created class



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

def self.new(*attributes, &block)
  Struct.new(*attributes) do
    include Attestor::Policy
    instance_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:

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

    Combines the policy with the others

    The combination is valid if all policies are valid

  • #and(policy) ⇒ Attestor::Policy::Negator

    Creates a negator object, awaiting fot #not method call

    Examples:

    policy.and.not(one, two)
    
    # this is equal to combination with negation of other policies:
    policy.and(one.not, two.not)
    


85
86
87
# File 'lib/attestor/policy.rb', line 85

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

#invalid?Boolean

Checks whether the policy is invalid



53
54
55
# File 'lib/attestor/policy.rb', line 53

def invalid?
  !valid?
end

#notAttestor::Policy::Not

Negates the current policy



60
61
62
# File 'lib/attestor/policy.rb', line 60

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:

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

    Combines the policy with the others

    The combination is valid unless all the policies are invalid

  • #or(policy) ⇒ Attestor::Policy::Negator

    Creates a negator object, awaiting fot #not method call

    Examples:

    policy.or.not(one, two)
    
    # this is equal to combination with negation of other policies:
    policy.or(one.not, two.not)
    


110
111
112
# File 'lib/attestor/policy.rb', line 110

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

#valid?Boolean

Checks whether the policy is valid



42
43
44
45
46
47
48
# File 'lib/attestor/policy.rb', line 42

def valid?
  validate
rescue InvalidError
  false
else
  true
end

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

Builds the XOR composition of current policy with other policies

Overloads:

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

    Combines the policy with the others

    The combination is valid if both valid and invalid policies are present

  • #xor(policy) ⇒ Attestor::Policy::Negator

    Creates a negator object, awaiting fot #not method call

    Examples:

    policy.xor.not(one, two)
    
    # this is equal to combination with negation of other policies:
    policy.xor(one.not, two.not)
    


135
136
137
# File 'lib/attestor/policy.rb', line 135

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