Sha256: f028fc2f5bdca5f0ee60dfc1c57026a1290e27da2127f7d5195740458b84dc94

Contents?: true

Size: 895 Bytes

Versions: 3

Compression:

Stored size: 895 Bytes

Contents

module Tako
  class QueryChain
    attr_reader :proxy
    attr_reader :base_object

    def initialize(proxy, base_object)
      @proxy = proxy
      @base_object = base_object
    end

    def method_missing(method, *args, &block)
      @proxy.with_shard do
        result = if block_given?
                    base_object.send(method, *args, &block)
                  else
                    base_object.send(method, *args)
                  end

        if chain_available?(result)
          @base_object = result
          return self
        end

        result
      end
    end

    def shard(shard_name)
      new(
        Tako::Repository.create_proxy(shard_name),
        self
      )
    end

    private

    def chain_available?(obj)
      [
        ::ActiveRecord::Relation,
        ::ActiveRecord::QueryMethods::WhereChain
      ].any? { |anc| obj.is_a?(anc) }
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tako-0.6.0 lib/tako/query_chain.rb
tako-0.5.0 lib/tako/query_chain.rb
tako-0.4.1 lib/tako/query_chain.rb