Sha256: e7a6223a27549c2274f4e6094725181d0ff4d4de44dee75a89ff3e3fc7a9d6aa

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module Polyamorous
  class Join
    attr_accessor :name
    attr_reader :type, :klass

    def initialize(name, type = Arel::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?

    private

    def convert_to_arel_join_type(type)
      case type
      when 'inner', :inner
        Arel::InnerJoin
      when 'outer', :outer
        Arel::OuterJoin
      when Class
        if [Arel::InnerJoin, Arel::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

6 entries across 6 versions & 1 rubygems

Version Path
polyamorous-1.0.0 lib/polyamorous/join.rb
polyamorous-0.6.4 lib/polyamorous/join.rb
polyamorous-0.6.3 lib/polyamorous/join.rb
polyamorous-0.6.2 lib/polyamorous/join.rb
polyamorous-0.6.0 lib/polyamorous/join.rb
polyamorous-0.5.0 lib/polyamorous/join.rb