Sha256: 9342ddaf9933edd3bfd9852d1e38af0124444e670f943f7f3413ff5db413a40c

Contents?: true

Size: 1.13 KB

Versions: 13

Compression:

Stored size: 1.13 KB

Contents

module SearchFlip
  # The SearchFlip::Result class is a simple Hash, but extended with
  # method-like access. Keys assigned via methods are stored as strings.
  #
  # @example method access
  #   result = SearchFlip::Result.new
  #   result["some_key"] = "value"
  #   result.some_key # => "value"

  class Result < Hash
    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

    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

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

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

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

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
search_flip-4.0.0.beta16 lib/search_flip/result.rb
search_flip-4.0.0.beta14 lib/search_flip/result.rb
search_flip-4.0.0.beta13 lib/search_flip/result.rb
search_flip-4.0.0.beta12 lib/search_flip/result.rb
search_flip-4.0.0.beta11 lib/search_flip/result.rb
search_flip-4.0.0.beta10 lib/search_flip/result.rb
search_flip-4.0.0.beta9 lib/search_flip/result.rb
search_flip-4.0.0.beta8 lib/search_flip/result.rb
search_flip-4.0.0.beta7 lib/search_flip/result.rb
search_flip-4.0.0.beta6 lib/search_flip/result.rb
search_flip-4.0.0.beta5 lib/search_flip/result.rb
search_flip-4.0.0.beta4 lib/search_flip/result.rb
search_flip-4.0.0.beta3 lib/search_flip/result.rb