Sha256: 5d142523159bdb232d49f58a61b973a6480dd4809eff47d5187e2f17f8f489bd

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

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

require 'aequitas/support/value_object'

module Aequitas
  class Rule
    class Guard
      extend ValueObject

      equalize_on :if_test, :unless_test

      attr_reader :if_test
      attr_reader :unless_test

      def initialize(options = {})
        @if_test     = options.fetch(:if,     nil)
        @unless_test = options.fetch(:unless, nil)
      end

      # Determines if this RuleGuard allows the given resource
      # by evaluating the if_test and unless_test
      #
      # @param [Object] resource
      #   The resource that we check against.
      #
      # @return [Boolean]
      #   true if allowed, otherwise false.
      #
      # @api private
      def allow?(resource)
        if if_test
          !!evaluate_conditional_clause(resource, if_test)
        elsif unless_test
          !evaluate_conditional_clause(resource, unless_test)
        else
          true
        end
      end

      # @api private
      def evaluate_conditional_clause(resource, clause)
        if clause.respond_to?(:call)
          clause.call(resource)
        elsif clause.kind_of?(Symbol)
          resource.__send__(clause)
        end
      end

    end # class Guard
  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/guard.rb