Sha256: 0b49d0a86009bb75c5455bb547f8308aeb089f850c68899240feeaaedd98f7c1

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/slice'
require 'elasticsearch/persistence/relation/merger'

module Elasticsearch
  module Persistence

      module SpawnMethods
        def spawn
          clone
        end

        def merge(other)
          if other.is_a?(Array)
            to_a & other
          elsif other
            spawn.merge!(other)
          else
            self
          end
        end

        def merge!(other) # :nodoc:
          if !other.is_a?(Relation) && other.respond_to?(:to_proc)
            instance_exec(&other)
          else
            klass = other.is_a?(Hash) ? Relation::HashMerger : Relation::Merger
            klass.new(self, other).merge
          end
        end

        # Removes from the query the condition(s) specified in +skips+.
        #
        #   Post.order('id asc').except(:order)                  # discards the order condition
        #   Post.where('id > 10').order('id asc').except(:where) # discards the where condition but keeps the order
        def except(*skips)
          relation_with values.except(*skips)
        end

        # Removes any condition from the query other than the one(s) specified in +onlies+.
        #
        #   Post.order('id asc').only(:where)         # discards the order condition
        #   Post.order('id asc').only(:where, :order) # uses the specified order
        def only(*onlies)
          if onlies.any? { |o| o == :where }
            onlies << :bind
          end
          relation_with values.slice(*onlies)
        end

        private

        def relation_with(values) # :nodoc:
          result = Relation.create(klass, values)
          result.extend(*extending_values) if extending_values.any?
          result
        end
      end

    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
elasticsearch-persistence-queryable-0.1.9 lib/elasticsearch/persistence/relation/spawn_methods.rb
elasticsearch-persistence-queryable-0.1.8 lib/elasticsearch/persistence/relation/spawn_methods.rb