Module: Tram::Policy::DSL

Included in:
Tram::Policy
Defined in:
lib/tram/policy/dsl.rb

Overview

Class-level DSL for policy objects

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Tram::Policy

Policy constructor/validator (alias for [.new])

Parameters:

  • *args (Object)

Returns:



21
22
23
# File 'lib/tram/policy/dsl.rb', line 21

def [](*args)
  new(*args)
end

#local_validatorsArray<Proc>

List of validators defined by a policy per se

Returns:

  • (Array<Proc>)


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

def local_validators
  @local_validators ||= []
end

#root_scope(*value) ⇒ self

Sets the root scope of the policy and its subclasses

Parameters:

  • value (String, Array<String>)

Returns:

  • (self)


30
31
32
# File 'lib/tram/policy/dsl.rb', line 30

def root_scope(*value)
  tap { @root_scope = value.flatten.map(&:to_s).reject(&:empty?) }
end

#scopeArray<String>

Translation scope for a policy

Returns:

  • (Array<String>)


38
39
40
# File 'lib/tram/policy/dsl.rb', line 38

def scope
  @scope ||= Array(@root_scope) + [Inflector.underscore(name)]
end

#validate(name, opts) ⇒ self

Registers a validator

Parameters:

  • name (#to_sym, nil)

    (nil)

  • opts (Hash)

    a customizable set of options

Options Hash (opts):

  • :stop_on_failure (Boolean)

Returns:

  • (self)


11
12
13
14
# File 'lib/tram/policy/dsl.rb', line 11

def validate(name = nil, **opts, &block)
  local_validators << Validator.new(name, block, opts)
  self
end

#validatorsArray<Proc>

List of all applicable validators from both the policy and its parent

Returns:

  • (Array<Proc>)


54
55
56
57
# File 'lib/tram/policy/dsl.rb', line 54

def validators
  parent_validators = self == Tram::Policy ? [] : superclass.validators
  (parent_validators + local_validators).uniq
end