Sha256: cf64ae300e728eedaa3c20c59fd8abcab73f777e2cfde964def2d47b9d0a7089

Contents?: true

Size: 1.42 KB

Versions: 40

Compression:

Stored size: 1.42 KB

Contents

module Polyamorous
  class Join
    include TreeNode

    attr_accessor :name
    attr_reader :type, :klass

    def initialize(name, type = InnerJoin, klass = nil)
      @name = name
      @type = convert_to_arel_join_type(type)
      @klass = convert_to_class(klass) if klass
    end

    def klass=(klass)
      @klass = convert_to_class(klass) if klass
    end

    def type=(type)
      @type = convert_to_arel_join_type(type) if type
    end

    def hash
      [@name, @type, @klass].hash
    end

    def eql?(other)
      self.class == other.class &&
      self.name  == other.name &&
      self.type  == other.type &&
      self.klass == other.klass
    end

    alias :== :eql?

    def add_to_tree(hash)
      hash[self] ||= {}
    end

    private

    def convert_to_arel_join_type(type)
      case type
      when 'inner', :inner
        InnerJoin
      when 'outer', :outer
        OuterJoin
      when Class
        if [InnerJoin, OuterJoin].include? type
          type
        else
          raise ArgumentError, "#{type} cannot be converted to an ARel join type"
        end
      else
        raise ArgumentError, "#{type} cannot be converted to an ARel join type"
      end
    end

    def convert_to_class(value)
      case value
      when String, Symbol
        Kernel.const_get(value)
      when Class
        value
      else
        raise ArgumentError, "#{value} cannot be converted to a Class"
      end
    end

  end
end

Version data entries

40 entries across 40 versions & 6 rubygems

Version Path
ransack-2.3.1 polyamorous/lib/polyamorous/join.rb
polyamorous-2.3.0 lib/polyamorous/join.rb
ransack-2.3.0 polyamorous/lib/polyamorous/join.rb
temp_poly-2.1.1a lib/polyamorous/join.rb
ransack-1.8.10 lib/polyamorous/join.rb
ransack-2.1.1 lib/polyamorous/join.rb
ransack-2.1.0 lib/polyamorous/join.rb
ransack-2.0.1 lib/polyamorous/join.rb
ransack-2.0.0 lib/polyamorous/join.rb
ransack-1.8.9 lib/polyamorous/join.rb
polyamorous-1.3.3 lib/polyamorous/join.rb
polyamorous-1.3.2 lib/polyamorous/join.rb
polyamorous-1.3.1 lib/polyamorous/join.rb
polyamorous-1.3.0 lib/polyamorous/join.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/polyamorous-1.2.0/lib/polyamorous/join.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/polyamorous-1.2.0/lib/polyamorous/join.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/polyamorous-1.2.0/lib/polyamorous/join.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/polyamorous-1.2.0/lib/polyamorous/join.rb
polyamorous-1.2.0 lib/polyamorous/join.rb
polyamorous-1.1.0 lib/polyamorous/join.rb