Sha256: cec768a5c34ada8908e691c015ec7f63bc43cb305cf45ed19bcd68bb4070e70d

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8

module Veritas
  class Optimizer

    # Allow operations to be optimizable
    module Optimizable
      include Immutable

      # Hook called when module is included
      #
      # @param [Module] descendant
      #   the module or class including Optimizable
      #
      # @return [self]
      #
      # @api private
      def self.included(descendant)
        descendant.extend ClassMethods
        self
      end

      # Optimize the operation
      #
      # @example
      #   optimized = operation.optimize
      #
      # @param [Optimizer] optimizer
      #   optional optimizer
      #
      # @return [Optimizable]
      #   the optimized operation
      #
      # @api public
      #
      # @todo simplify by setting a default Noop optimizer for all relations
      def optimize(optimizer = self.class.optimizer || Optimizer::Noop)
        optimized = optimizer.call(self)
        equal?(optimized)                ? self               :
        optimized.respond_to?(:optimize) ? optimized.optimize :
                                           optimized
      end

      memoize :optimize

      module ClassMethods

        # The Optimizer for the operation
        #
        # @return [Optimizable]
        #
        # @api private
        attr_accessor :optimizer

      end # module ClassMethods
    end # module Optimizable
  end # class Optimizer
end # module Veritas

Veritas::Aggregate.class_eval { include Veritas::Optimizer::Optimizable }
Veritas::Function.class_eval  { include Veritas::Optimizer::Optimizable }
Veritas::Relation.class_eval  { include Veritas::Optimizer::Optimizable }

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
veritas-optimizer-0.0.7 lib/veritas/optimizer/optimizable.rb
veritas-optimizer-0.0.6 lib/veritas/optimizer/optimizable.rb
veritas-optimizer-0.0.5 lib/veritas/optimizer/optimizable.rb
veritas-optimizer-0.0.4 lib/veritas/optimizer/optimizable.rb