Sha256: 21b1e2fde09f7edb3f1681fb295d7f6ecf2196a92a0ea32a9c8bf193156243c2

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

class Tram::Policy
  # Class-level DSL for policy objects
  module DSL
    # @!method validate(name, opts)
    # Registers a validator
    #
    # @param  [#to_sym, nil] name (nil)
    # @option opts [Boolean] :stop_on_failure
    # @return [self]
    #
    def validate(name = nil, **opts, &block)
      local_validators << Validator.new(name, block, opts)
      self
    end

    # Policy constructor/validator (alias for [.new])
    #
    # @param  [Object] *args
    # @return [Tram::Policy]
    #
    def [](*args)
      new(*args)
    end

    # Translation scope for a policy
    #
    # @return [Array<String>]
    #
    def scope
      @scope ||= ["tram-policy", *Inflector.underscore(name)]
    end

    # List of validators defined by a policy per se
    #
    # @return [Array<Proc>]
    #
    def local_validators
      @local_validators ||= []
    end

    # List of all applicable validators from both the policy and its parent
    #
    # @return [Array<Proc>]
    #
    def validators
      parent_validators = self == Tram::Policy ? [] : superclass.validators
      (parent_validators + local_validators).uniq
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tram-policy-0.3.1 lib/tram/policy/dsl.rb
tram-policy-0.3.0 lib/tram/policy/dsl.rb
tram-policy-0.2.5 lib/tram/policy/dsl.rb
tram-policy-0.2.4 lib/tram/policy/dsl.rb