Class: Flex::InstanceProxy::ModelIndexer
Instance Attribute Summary collapse
Attributes inherited from ModelSyncer
#class_flex, #instance
Instance Method Summary
collapse
Methods inherited from ModelSyncer
#initialize, #refresh_index, #sync, #uid
Instance Attribute Details
#index ⇒ Object
57
58
59
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 57
def index
@index ||= instance.respond_to?(:flex_index) ? instance.flex_index : class_flex.index
end
|
#parent ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 84
def parent
@parent ||= case
when instance.respond_to?(:flex_parent) then instance.flex_parent
when is_child? then parent_instance.id.to_s
else nil
end
end
|
#routing ⇒ Object
75
76
77
78
79
80
81
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 75
def routing
@routing ||= case
when instance.respond_to?(:flex_routing) then instance.flex_routing
when is_child? then parent_instance.flex.routing
when is_parent? then create_routing
end
end
|
#type ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 62
def type
@type ||= case
when instance.respond_to?(:flex_type) then instance.flex_type
when is_child? then class_flex.parent_child_map[parent_instance.flex.type]
else class_flex.type
end
end
|
Instance Method Details
#each_parent ⇒ Object
helper that iterates through the parent record chain record.flex.each_parent{|p| p.do_something }
49
50
51
52
53
54
55
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 49
def each_parent
pi = parent_instance
while pi do
yield pi
pi = pi.flex.parent_instance
end
end
|
#full_get(*vars) ⇒ Object
like get, but it returns all the fields after a refresh
36
37
38
39
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 36
def full_get(*vars)
return unless instance.flex_indexable?
Flex.search_by_id(metainfo, {:refresh => true, :params => {:fields => '*,_source'}}, *vars)
end
|
#get(*vars) ⇒ Object
gets the document from ES
30
31
32
33
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 30
def get(*vars)
return unless instance.flex_indexable?
Flex.get(metainfo, *vars)
end
|
#id ⇒ Object
71
72
73
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 71
def id
@id ||= instance.respond_to?(:flex_id) ? instance.flex_id : instance.id.to_s
end
|
93
94
95
96
97
98
99
100
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 93
def metainfo
meta = Vars.new( :index => index, :type => type, :id => id )
params = {}
params[:routing] = routing if routing
params[:parent] = parent if parent
meta.merge!(:params => params) unless params.empty?
meta
end
|
#parent_instance ⇒ Object
41
42
43
44
45
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 41
def parent_instance
return unless is_child?
@parent_instance ||= instance.send(class_flex.parent_association) ||
raise(MissingParentError, "missing parent instance for document #{instance.inspect}.")
end
|
#remove(*vars) ⇒ Object
removes the document from the index (called from after_destroy)
24
25
26
27
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 24
def remove(*vars)
return unless instance.flex_indexable?
Flex.remove(metainfo, *vars)
end
|
#store(*vars) ⇒ Object
indexes the document usually called from after_save, you can eventually call it explicitly for example from another callback or whenever the DB doesn’t get updated by the model you can also pass the :data=>flex_source explicitly (useful for example to override the flex_source in the model)
15
16
17
18
19
20
21
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 15
def store(*vars)
if instance.flex_indexable?
Flex.store(metainfo, {:data => instance.flex_source}, *vars)
else
Flex.remove(metainfo, *vars) if Flex.get(metainfo, *vars, :raise => false)
end
end
|
#sync_self ⇒ Object
102
103
104
|
# File 'lib/flex/instance_proxy/model_indexer.rb', line 102
def sync_self
instance.destroyed? ? remove : store
end
|