Module: Flex::Result::Document

Defined in:
lib/flex/result/document.rb

Overview

adds sugar to documents with the following structure (_source is optional):

{
    "_index" : "twitter",
    "_type" : "tweet",
    "_id" : "1",
    "_source" : {
        "user" : "kimchy",
        "postDate" : "2009-11-15T14:12:12",
        "message" : "trying out Elastic Search"
    }
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

exposes _source and readers: automatically supply object-like reader access also expose meta readers like _id, _source, etc, also callable without the leading ‘_’



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flex/result/document.rb', line 31

def method_missing(meth, *args, &block)
  smeth = meth.to_s
  case
  # field name
  when readers.has_key?(smeth)
    readers[smeth]
  # result item
  when has_key?(smeth)
    self[smeth]
  # result item called without the '_' prefix
  when has_key?("_#{smeth}")
    self["_#{smeth}"]
  else
    super
  end
end

Class Method Details

.should_extend?(obj) ⇒ Boolean

extend if result has a structure like a document

Returns:

  • (Boolean)


20
21
22
# File 'lib/flex/result/document.rb', line 20

def self.should_extend?(obj)
  %w[_index _type _id].all? {|k| obj.has_key?(k)}
end

Instance Method Details

#index_basenameObject

used to get the unprefixed (by live-reindex) index name



49
50
51
# File 'lib/flex/result/document.rb', line 49

def index_basename
  @index_basename ||= self['_index'].sub(/^\d{14}_/, '')
end

#respond_to?(meth, private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/flex/result/document.rb', line 24

def respond_to?(meth, private=false)
  smeth = meth.to_s
  readers.has_key?(smeth) || has_key?(smeth) || has_key?("_#{smeth}") || super
end