Module: Lycra::Document::Proxy::InstanceMethods

Defined in:
lib/lycra/document/proxy.rb

Instance Method Summary collapse

Instance Method Details

#_indexedObject



306
307
308
# File 'lib/lycra/document/proxy.rb', line 306

def _indexed
  @indexed ||= self.class.search({query: {terms: {_id: [subject.id]}}}).results.first
end

#_indexed?Boolean

Returns:

  • (Boolean)


318
319
320
# File 'lib/lycra/document/proxy.rb', line 318

def _indexed?
  !!@indexed
end

#as_indexed_json(options = {}) ⇒ Object



273
274
275
# File 'lib/lycra/document/proxy.rb', line 273

def as_indexed_json(options={})
  resolve!.as_json(options)
end

#as_json(options = {}) ⇒ Object



328
329
330
331
332
333
334
335
336
337
# File 'lib/lycra/document/proxy.rb', line 328

def as_json(options={})
  resolve! unless resolved?

  { index: self.class.index_name,
    document: self.class.document_type,
    subject: self.class.subject_type.name,
    resolved: resolved.map { |k,a| [k, a.as_json] }.to_h,
    indexed: indexed? && indexed.map { |k,a| [k, a.as_json] }.to_h }
    .as_json(options)
end

#index!(options = {}) ⇒ Object



277
278
279
280
281
282
# File 'lib/lycra/document/proxy.rb', line 277

def index!(options={})
  raise Lycra::AbstractClassError, "Cannot index using an abstract class" if abstract?

  @indexed = nil
  __lycra__.index_document(options)
end

#indexedObject



310
311
312
# File 'lib/lycra/document/proxy.rb', line 310

def indexed
  _indexed&._source&.to_h
end

#indexed?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/lycra/document/proxy.rb', line 314

def indexed?
  !!indexed
end

#inspectObject



339
340
341
342
# File 'lib/lycra/document/proxy.rb', line 339

def inspect
  attr_str = "#{attributes.map { |key,attr| "#{key}: #{(resolved? && resolved[key].try(:to_json)) || (_indexed? && indexed[key.to_s].try(:to_json)) || (attr.nested? ? "[#{attr.type.type}]" : attr.type.type)}"}.join(', ')}>"
  "#<#{self.class.name} index: #{self.class.index_name}, document: #{self.class.document_type}, subject: #{self.class.subject_type}, #{attr_str}"
end

#reloadObject



322
323
324
325
326
# File 'lib/lycra/document/proxy.rb', line 322

def reload
  super if defined?(super)
  @indexed = nil
  self
end

#update!(options = {}) ⇒ Object



284
285
286
287
288
289
# File 'lib/lycra/document/proxy.rb', line 284

def update!(options={})
  raise Lycra::AbstractClassError, "Cannot update using an abstract class" if abstract?

  @indexed = nil
  __lycra__.update_document(options)
end

#update_attributes!(*attrs, **options) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/lycra/document/proxy.rb', line 291

def update_attributes!(*attrs, **options)
  raise Lycra::AbstractClassError, "Cannot update using an abstract class" if abstract?

  if attrs.empty?
    document_attrs = resolve!
  else
    document_attrs = resolve!(only: attrs)
  end

  @indexed = nil
  __lycra__.update_document_attributes(document_attrs, options)
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
  index!(options)
end