Class: SearchFlip::Result

Inherits:
Hash
  • Object
show all
Defined in:
lib/search_flip/result.rb

Overview

The SearchFlip::Result class is a simple Hash, but extended with method-like access. Keys assigned via methods are stored as strings.

Examples:

method access

result = SearchFlip::Result.new
result["some_key"] = "value"
result.some_key # => "value"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



37
38
39
# File 'lib/search_flip/result.rb', line 37

def method_missing(name, *args, &block)
  self[name.to_s]
end

Class Method Details

.convert(hash) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/search_flip/result.rb', line 11

def self.convert(hash)
  res = self[hash]

  res.each do |key, value|
    if value.is_a?(Hash)
      res[key] = convert(value)
    elsif value.is_a?(Array)
      res[key] = convert_array(value)
    end
  end

  res
end

.convert_array(arr) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/search_flip/result.rb', line 25

def self.convert_array(arr)
  arr.map do |obj|
    if obj.is_a?(Hash)
      convert(obj)
    elsif obj.is_a?(Array)
      convert_array(obj)
    else
      obj
    end
  end
end

.from_hit(hit) ⇒ Object



45
46
47
48
49
# File 'lib/search_flip/result.rb', line 45

def self.from_hit(hit)
  res = convert(hit["_source"] || {})
  res["_hit"] = convert(self[hit].tap { |hash| hash.delete("_source") })
  res
end

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/search_flip/result.rb', line 41

def respond_to_missing?(name, include_private = false)
  key?(name.to_s) || super
end