Module: Policy::Follower::ClassMethods

Included in:
Policy::Follower
Defined in:
lib/policy/follower.rb

Overview

Methods to be added to the class the module is included to

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#followed_policiesPolicy::Follower::FollowedPolicies (readonly)

The collection of policies to be followed by instances of the class



23
24
25
# File 'lib/policy/follower.rb', line 23

def followed_policies
  @followed_policies ||= FollowedPolicies.new
end

Instance Method Details

#follow_policy(policy, *attributes, as: nil) ⇒ undefined

Adds a policy to the list of #followed_policies

Parameters:

  • policy (Class)

    the policy object klass

  • attributes (Array<#to_sym>)

    the list of attributes of the instance the policy should be applied to

  • [#to_sym] (Hash)

    a customizable set of options

Returns:

  • (undefined)


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

def follow_policy(policy, *attributes, as: nil)
  object = FollowedPolicy.new(__policies__, policy, as, *attributes)
  followed_policies.add object
end

#use_policies(namespace) { ... } ⇒ undefined

Changes the namespace for applied policies

Examples:

For Policies::Finances::TransferConsistency

use_policies Policies::Finances do
  apply_policy :TransferConstistency, :debet, :credit
end

Parameters:

  • namespace (Module)

Yields:

  • the block in the current scope

Returns:

  • (undefined)


56
57
58
59
60
61
# File 'lib/policy/follower.rb', line 56

def use_policies(namespace, &block)
  @__policies__ = namespace
  instance_eval(&block)
ensure
  @__policies__ = nil
end