Class: ElasticRecord::Relation

Inherits:
Object
  • Object
show all
Includes:
Batches, Delegation, FinderMethods, Merging, SearchMethods
Defined in:
lib/elastic_record/relation.rb,
lib/elastic_record/relation/value_methods.rb

Constant Summary collapse

MULTI_VALUE_METHODS =
[:extending, :facet, :filter, :order, :select]
SINGLE_VALUE_METHODS =
[:query, :limit, :offset]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Batches

#find_each

Methods included from Delegation

#include?, #to_ary

Methods included from FinderMethods

#all, #find, #first, #last

Methods included from Merging

#merge, #merge!

Methods included from SearchMethods

#as_elastic, #extending, #extending!, #facet, #facet!, #filter, #filter!, #limit, #limit!, #offset, #offset!, #order, #order!, #query, #query!, #select, #select!

Constructor Details

#initialize(klass, arelastic) ⇒ Relation

Returns a new instance of Relation.



14
15
16
17
18
# File 'lib/elastic_record/relation.rb', line 14

def initialize(klass, arelastic)
  @klass = klass
  @arelastic = arelastic
  @values = {}
end

Dynamic Method Handling

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

Instance Attribute Details

#arelasticObject (readonly)

Returns the value of attribute arelastic.



12
13
14
# File 'lib/elastic_record/relation.rb', line 12

def arelastic
  @arelastic
end

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'lib/elastic_record/relation.rb', line 12

def klass
  @klass
end

#valuesObject (readonly)

Returns the value of attribute values.



12
13
14
# File 'lib/elastic_record/relation.rb', line 12

def values
  @values
end

Instance Method Details

#==(other) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/elastic_record/relation.rb', line 52

def ==(other)
  case other
  when Relation
    other.as_elastic == as_elastic
  when Array
    to_a == other
  end
end

#countObject



20
21
22
# File 'lib/elastic_record/relation.rb', line 20

def count
  to_hits.total_entries
end

#facetsObject



24
25
26
# File 'lib/elastic_record/relation.rb', line 24

def facets
  to_hits.facets
end

#initialize_copy(other) ⇒ Object



32
33
34
35
# File 'lib/elastic_record/relation.rb', line 32

def initialize_copy(other)
  @values = @values.dup
  reset
end

#inspectObject



61
62
63
# File 'lib/elastic_record/relation.rb', line 61

def inspect
  to_a.inspect
end

#resetObject



28
29
30
# File 'lib/elastic_record/relation.rb', line 28

def reset
  @hits = @records = nil
end

#scopingObject



65
66
67
68
69
70
# File 'lib/elastic_record/relation.rb', line 65

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

#to_aObject



37
38
39
40
41
42
# File 'lib/elastic_record/relation.rb', line 37

def to_a
  @records ||= begin
    scope = select_values.any? ? klass.select(select_values) : klass
    scope.find(to_ids)
  end
end

#to_hitsObject



48
49
50
# File 'lib/elastic_record/relation.rb', line 48

def to_hits
  @hits ||= klass.elastic_connection.search(as_elastic)#, ids_only: true)
end

#to_idsObject



44
45
46
# File 'lib/elastic_record/relation.rb', line 44

def to_ids
  to_hits.to_a.map(&:id)
end