Sha256: f7ad39ff1d3453e1e1cf2ad939d21dcb9650f207dd327e3d54e0403f4904b84d

Contents?: true

Size: 934 Bytes

Versions: 4

Compression:

Stored size: 934 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)
      @proxy.with_shard do
        result = if block_given?
                    base_object.send(method, *args) do
                      yield
                    end
                  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

4 entries across 4 versions & 1 rubygems

Version Path
tako-0.4.0 lib/tako/query_chain.rb
tako-0.3.2 lib/tako/query_chain.rb
tako-0.3.1 lib/tako/query_chain.rb
tako-0.3.0 lib/tako/query_chain.rb