Sha256: 157d8b2bf866ef3d9250884686091cd49a03f2e9cc5992d8cb2f884a9fb974a4

Contents?: true

Size: 795 Bytes

Versions: 1

Compression:

Stored size: 795 Bytes

Contents

# -*- encoding: utf-8 -*-

require 'aequitas/rule'

module Aequitas
  class Rule
    class Block < Rule

      attr_reader :block

      def initialize(attribute_name, options, &block)
        unless block_given?
          raise ArgumentError, 'cannot initialize a Block validator without a block'
        end

        super

        @block = block
        @violation_type = options.fetch(:violation_type, :unsatisfied_condition)
      end

      def validate(resource)
        result, error_message = resource.instance_eval(&self.block)

        if result
          nil
        else
          Violation.new(resource, error_message, self)
        end
      end

      def violation_type(resource)
        @violation_type
      end

    end # class Block
  end # class Rule
end # module Aequitas

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
aequitas-0.0.2 lib/aequitas/rule/block.rb