Sha256: 9150345f97f0b4589acfbf9e0311cb036cb37ae1d232e98cb5de881b17f481e1

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

require File.dirname(__FILE__) + '/blank_slate'

class Proc
  
  # Yeah, all this is kinda the suck.
  def to_hash
    Conditions.new(&self).__to_hash__
  end
  
  class Conditions < BlankSlate
    
    def initialize(&block)
      @block = block
      @conditions = []
    end
    
    def method_missing(sym, *args)
      attribute = Attribute.new(sym)
      @conditions << attribute
      attribute
    end
    
    def __to_hash__
      instance_eval(&@block)      
      @conditions.inject({}) do |h,attribute|
        h[attribute.__operator__] = *attribute.__args__; h
      end
    end
    
    class Attribute < BlankSlate
      
      def initialize(message)
        @message = message
        @args = nil
        @operator = nil
      end
    
      def method_missing(sym, *args)
        op = case sym
          when :==, :===, :in then :eql
          when :=~ then :like
          when :"<=>" then :not
          when :< then :lt
          when :<= then :lte
          when :> then :gt
          when :>= then :gte
          else sym
        end
        
        @operator = Symbol::Operator.new(@message, op)
        @args = args
      end
      
      def __operator__
        @operator
      end
      
      def __args__
        @args
      end
    
      def __message__
        @message
      end
      
    end
        
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
datamapper-0.1.0 lib/data_mapper/support/proc.rb
datamapper-0.1.1 lib/data_mapper/support/proc.rb