module Bizness::Policy

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”]

Attributes

violations[W]

Public Class Methods

included(base) click to toggle source
# File lib/bizness/policy.rb, line 46
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

obeyed?() click to toggle source
# 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?() click to toggle source
# File lib/bizness/policy.rb, line 62
def violated?
  !obeyed?
end
violations() click to toggle source
# File lib/bizness/policy.rb, line 50
def violations
  @violations || []
end