Class: Elasticsearch::Persistence::Relation

Inherits:
Object
  • Object
show all
Includes:
Delegation, FinderMethods, QueryMethods, SearchOptionMethods, SpawnMethods
Defined in:
lib/elasticsearch/persistence/relation.rb,
lib/elasticsearch/persistence/relation/merger.rb

Defined Under Namespace

Classes: HashMerger, Merger

Constant Summary collapse

MULTI_VALUE_METHODS =
[:order, :where, :or_filter, :filter, :bind, :extending, :unscope, :skip_callbacks]
SINGLE_VALUE_METHODS =
[:limit, :offset, :routing, :size]
INVALID_METHODS_FOR_DELETE_ALL =
[:limit, :offset]
VALUE_METHODS =
MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS

Constants included from Delegation

Delegation::BLACKLISTED_ARRAY_METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FinderMethods

#count, #count!, #first, #first!, #last, #last!

Methods included from SpawnMethods

#except, #merge, #merge!, #only, #spawn

Methods included from QueryMethods

#aggregation, #aggregation!, #bind, #bind!, #build_where, #extending, #extending!, #field, #field!, #filter, #filter!, #has_field?, #highlight, #highlight!, #must_not, #must_not!, #none, #none!, #or_filter, #or_filter!, #order, #order!, #query_string, #query_string!, #should, #should!, #size, #size!, #skip_callbacks, #skip_callbacks!, #source, #source!, #where, #where!

Methods included from SearchOptionMethods

#routing, #routing!, #search_options, #search_options!, #search_type, #search_type!

Methods included from Delegation

#respond_to?

Constructor Details

#initialize(klass, values = {}) ⇒ Relation

Returns a new instance of Relation.



20
21
22
23
24
25
# File 'lib/elasticsearch/persistence/relation.rb', line 20

def initialize(klass, values={})
    @klass  = klass
    @values = values
    @offsets = {}
    @loaded = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Elasticsearch::Persistence::Delegation

Instance Attribute Details

#klassObject (readonly) Also known as: model

Returns the value of attribute klass.



14
15
16
# File 'lib/elasticsearch/persistence/relation.rb', line 14

def klass
  @klass
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



14
15
16
# File 'lib/elasticsearch/persistence/relation.rb', line 14

def loaded
  @loaded
end

Instance Method Details

#as_json(options = nil) ⇒ Object



37
38
39
# File 'lib/elasticsearch/persistence/relation.rb', line 37

def as_json(options = nil)
  to_a.as_json(options)
end

#build(*args) ⇒ Object



27
28
29
# File 'lib/elasticsearch/persistence/relation.rb', line 27

def build(*args)
 @klass.new *args
end

#create(*args, &block) ⇒ Object



45
46
47
# File 'lib/elasticsearch/persistence/relation.rb', line 45

def create(*args, &block)
   scoping { @klass.create!(*args, &block) }
end

#delete(opts = nil) ⇒ Object



63
64
# File 'lib/elasticsearch/persistence/relation.rb', line 63

def delete(opts=nil)
end

#exec_queriesObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/elasticsearch/persistence/relation.rb', line 66

def exec_queries
  # Run safety callback
  klass.circuit_breaker_callbacks.each do |cb|
    current_scope_values = self.send("#{cb[:options][:in]}_values")
    next if skip_callbacks_values.include? cb[:name]
    valid = if cb[:callback].nil?
      current_scope_values.collect(&:keys).flatten.include? cb[:name]
    else
      cb[:callback].call(current_scope_values.collect(&:keys).flatten, current_scope_values)
    end

    raise Elasticsearch::Persistence::Model::QueryOptionMissing, "#{cb[:name]} #{cb[:options][:message]}" unless valid
  end

  @records = @klass.fetch_results(query_builder)

  @loaded = true
  @records
end

#inspectObject



90
91
92
93
94
95
96
97
98
# File 'lib/elasticsearch/persistence/relation.rb', line 90

def inspect
  entries = to_a.results.take([size_value.to_i + 1, 11].compact.min).map!(&:inspect)
  message = {}
  message = {total: to_a.total, max: to_a.total}
  message.merge!(aggregations: results.aggregations.keys) unless results.aggregations.nil?
  message = message.each_pair.collect { |k,v|  "#{k}: #{v}" }
  message.unshift entries.join(', ') unless entries.size.zero?
  "#<#{self.class.name} #{message.join(', ')}>"
end

#loadObject Also known as: fetch



56
57
58
59
60
# File 'lib/elasticsearch/persistence/relation.rb', line 56

def load
  exec_queries unless loaded?

  self
end

#scopingObject



49
50
51
52
53
54
# File 'lib/elasticsearch/persistence/relation.rb', line 49

def scoping
  previous, klass.current_scope = klass.current_scope, self
  yield
ensure
  klass.current_scope = previous
end

#to_aObject Also known as: results



31
32
33
34
# File 'lib/elasticsearch/persistence/relation.rb', line 31

def to_a
  load
  @records
end

#to_elasticObject



41
42
43
# File 'lib/elasticsearch/persistence/relation.rb', line 41

def to_elastic
  query_builder.to_elastic
end

#valuesObject



86
87
88
# File 'lib/elasticsearch/persistence/relation.rb', line 86

def values
  Hash[@values]
end