Sha256: d02fa2d19b77dd136b16f02bb902fd8f62b47af614ddddc2b0ad18acdec31ac7

Contents?: true

Size: 787 Bytes

Versions: 8

Compression:

Stored size: 787 Bytes

Contents

class Tram::Policy
  # @private
  class Validator
    attr_reader :name, :block, :stop_on_failure

    def ==(other)
      other.is_a?(self.class) && name && other.name == name
    end

    def check(object)
      name ? object.__send__(name) : object.instance_exec(&block)
    end

    private

    def initialize(name, block, stop_on_failure: false)
      @name  = name&.to_sym
      @block = to_lambda(block)
      raise "Provide either method name or a block" unless !name ^ !block
      @stop_on_failure = stop_on_failure
    end

    def to_lambda(block)
      return unless block

      unbound = Module.new.module_exec do
        instance_method define_method(:_, &block)
      end

      ->(&b) { unbound.bind(self == block ? block.receiver : self).call(&b) }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
tram-policy-2.2.0 lib/tram/policy/validator.rb
tram-policy-2.1.0 lib/tram/policy/validator.rb
tram-policy-2.0.1 lib/tram/policy/validator.rb
tram-policy-2.0.0 lib/tram/policy/validator.rb
tram-policy-1.0.1 lib/tram/policy/validator.rb
tram-policy-1.0.0 lib/tram/policy/validator.rb
tram-policy-0.4.0 lib/tram/policy/validator.rb
tram-policy-0.3.1 lib/tram/policy/validator.rb