Module: Bizness::Policy

Defined in:
lib/bizness/policy.rb

Overview

Adds convenience methods for defining a policy object and recording its violations. To take use this module, do the following:

  1. Write one or more private predicate methods that define your policies requirements

  2. Define violation messages in your corresponding I18n locale YAML files

– string_format_policy.rb

class Policies::StringFormatPolicy

include Bizness::Policy

def initialize(string)
  @string = string
end

private

attr_reader :string

def all_caps?
 string.upcase == string
end

end

– en.yml

en:

policies:
  string_format_policy:
    violations:
      all_caps: "Must be an uppercase string"

Example usage:

policy = StringFormatPolicy.new(“abcd”) policy.obeyed?

> false

policy.violated?

> true

policy.violations

> [“Must be an uppercase string”]

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#violationsObject



50
51
52
# File 'lib/bizness/policy.rb', line 50

def violations
  @violations || []
end

Class Method Details

.included(base) ⇒ Object



46
47
48
# File 'lib/bizness/policy.rb', line 46

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#obeyed?Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/bizness/policy.rb', line 54

def obeyed?
  self.violations = []
  self.class.__requirements__.each do |m|
    self.violations << self.class.violation_message(m) unless send(m)
  end
  violations.empty?
end

#violated?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/bizness/policy.rb', line 62

def violated?
  !obeyed?
end