Module: PolicyCheck

Defined in:
lib/policy_check.rb,
lib/policy_check/error.rb,
lib/policy_check/version.rb,
lib/policy_check/error_list.rb,
lib/policy_check/module_builder.rb

Overview

extend this module to add ‘.policy` method

Examples:

inline policy

class Post
  extend PolicyCheck #-> only add `.policy` method

  policy :publishable do #-> only create `#publishable?` and `#publishable_errors` method
    error 'status is not draft', &:not_draft?
    error('body is empty') { @body.empty? }
  end
end

policy class

class Post
  extend PolicyCheck #-> only add `.policy` method

  policy do #-> only create `#valid?`, `#invalid?` and `#error_messages` method
    error 'status is not draft', &:not_draft?
    error('body is empty') { @body.empty? }
  end
end

Defined Under Namespace

Classes: Error, ErrorList, ModuleBuilder

Constant Summary collapse

VERSION =
"0.1.2"

Instance Method Summary collapse

Instance Method Details

#policy(name = nil, &block) ⇒ Object

If a name is specified “#{name}?” and “#{name}_errors” method are added. if name is ‘nil`, add `#error_messages`, `#valid?` and `#invalid?` method

Parameters:

  • name (#to_sym) (defaults to: nil)

    policy name, default is ‘nil`.



31
32
33
34
# File 'lib/policy_check.rb', line 31

def policy(name = nil, &block)
  name = name&.to_sym
  class_exec { include PolicyCheck::ModuleBuilder.new(name, &block) }
end