module Axiom::Optimizer::Function::Predicate::Enumerable

Abstract base class representing Enumerable predicate optimizations

Public Class Methods

sort_by_value(object) click to toggle source

Return a value to sort the obejct with

@param [Object] object

@return [Object]

@api private

# File lib/axiom/optimizer/function/predicate/enumerable.rb, line 18
def self.sort_by_value(object)
  case object
  when TrueClass  then 1
  when FalseClass then 0
  else
    object
  end
end

Private Instance Methods

normalized_right_enumerable() click to toggle source

Normalize the right enumerable to only include unique, sorted, valid values

@return [Enumerable]

@api private

# File lib/axiom/optimizer/function/predicate/enumerable.rb, line 74
def normalized_right_enumerable
  enumerable = operation.right.select(&left.method(:include?))
  enumerable.uniq!
  enumerable.sort_by!(&Enumerable.method(:sort_by_value))
end
optimize_right() click to toggle source

Optimize the right operand

@return [Object]

@api private

# File lib/axiom/optimizer/function/predicate/enumerable.rb, line 34
def optimize_right
  right = operation.right

  if right.respond_to?(:to_inclusive)
    optimize_right_range
  elsif right.respond_to?(:select)
    optimize_right_enumerable
  else
    right
  end
end
optimize_right_enumerable() click to toggle source

Optimize the right enumerable operand

@return [Enumerable]

@api private

# File lib/axiom/optimizer/function/predicate/enumerable.rb, line 62
def optimize_right_enumerable
  enumerable = normalized_right_enumerable
  right      = operation.right

  right.eql?(enumerable) ? right : enumerable
end
optimize_right_range() click to toggle source

Optimize the right range operand

@return [Range]

@api private

# File lib/axiom/optimizer/function/predicate/enumerable.rb, line 51
def optimize_right_range
  right = operation.right
  return unless left.respond_to?(:range)
  right.to_inclusive if right.overlaps?(left.range)
end