Sha256: 826f2cee1f1a57a2e9e53b1044d018ce2c564e8dee94cfd9b9a4f539520eecb6

Contents?: true

Size: 1.81 KB

Versions: 30

Compression:

Stored size: 1.81 KB

Contents

# Based on great work by Sam Smoot
# http://substantiality.net/archives/2007/4/16/datamapper-part-xiv-finder-block

module Sequel
  class Dataset
    class BlankSlate #:nodoc:
      instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_eval)/ }
    end

    # An Expression is made of a left side, an operator, and a right side.
    class Expression < BlankSlate
      attr_reader :left, :right
      attr_accessor :op

      def initialize(left)
        @left = left
      end

      def method_missing(sym, *right)
        @op = case sym
        when :==, :===, :in, :in?: :eql
        when :=~, :like, :like?: :like
        when :"<=>", :is_not: :not
        when :<: :lt
        when :<=: :lte
        when :>: :gt
        when :>=: :gte
        else
          @left = "#{left}.#{sym}"
        end
        @right = right.first
        self
      end
      
      def nil?
        @op = :eql
        @right = nil
        self
      end
    end

    # An ExpressionCompiler takes a Proc object and compiles it
    # into an array of expressions using instance_eval magic.
    class ExpressionCompiler < BlankSlate
      def initialize(&block) #:nodoc:
        @block = block
        @expressions = []
      end
      
      # Converts the block into an array of expressions.
      def __to_a__
        instance_eval(&@block)
        @expressions
      rescue => e
        raise SequelError, e.message
      end

      private
        def __expr(sym) #:nodoc:
          expr = Expression.new(sym)
          @expressions << expr
          expr
        end

        def method_missing(sym, *args); __expr(sym); end #:nodoc:
        def test; __expr(:test); end #:nodoc:
        def SUM(sym); __expr(sym.SUM); end #:nodoc:
    end
  end
end

class Proc
  def to_expressions
    Sequel::Dataset::ExpressionCompiler.new(&self).__to_a__
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
sequel-0.1.4 lib/sequel/expressions.rb
sequel-0.1.0 lib/sequel/expressions.rb
sequel-0.1.2 lib/sequel/expressions.rb
sequel-0.1.5 lib/sequel/expressions.rb
sequel-0.1.3 lib/sequel/expressions.rb
sequel-0.1.1 lib/sequel/expressions.rb
sequel-0.1.9.4 lib/sequel/expressions.rb
sequel-0.1.9.2 lib/sequel/expressions.rb
sequel-0.1.6 lib/sequel/expressions.rb
sequel-0.1.7 lib/sequel/expressions.rb
sequel-0.1.8 lib/sequel/expressions.rb
sequel-0.1.9.3 lib/sequel/expressions.rb
sequel-0.1.9.10 lib/sequel/expressions.rb
sequel-0.1.9.5 lib/sequel/expressions.rb
sequel-0.1.9.1 lib/sequel/expressions.rb
sequel-0.1.9.11 lib/sequel/expressions.rb
sequel-0.1.9.12 lib/sequel/expressions.rb
sequel-0.1.9.7 lib/sequel/expressions.rb
sequel-0.2.1 lib/sequel/expressions.rb
sequel-0.2.0.1 lib/sequel/expressions.rb