Sha256: dfc93109e2b78c4263e332df2ce6784e09ddf237a62904d1642a6b6b8ca35d09

Contents?: true

Size: 1.45 KB

Versions: 7

Compression:

Stored size: 1.45 KB

Contents

module Gummi
  module RepositoryLayer
    module Repository
      class Result

        attr_reader :took

        def initialize(search_result, converter)
          @success       = search_result.success?
          @search_result = search_result
          @took          = @search_result.took      if @search_result
          @documents     = @search_result.documents if @search_result
          @converter     = converter
        end

        def facets
          return unless search_result.facets
          @facets ||= begin
            result = {}
            search_result.facets.each do |(name, content)|
              result[name] = Hash[content.terms.map(&:values)]
            end
            Hashie::Mash.new result
          end
        end

        def records
          @records ||= begin
            # Passing through the Leaflet Collection, but converting the records.
            documents.map! { |document| converter.db_instance_to_entity(document) }
            documents
          end
        end

        def success?
          @success
        end

        # Leaflet::Collection has many methods we want to make use of.
        # E.g. kaminari and will_paginate support.
        # Also, we want to have Enumerable and #decorate at our fingertips.
        #
        def method_missing(method_name, *args)
          records.send method_name, *args
        end

        private

        attr_reader :search_result, :converter, :documents

      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gummi-0.3.9 lib/gummi/repository_layer/repository/result.rb
gummi-0.3.8 lib/gummi/repository_layer/repository/result.rb
gummi-0.3.7 lib/gummi/repository_layer/repository/result.rb
gummi-0.3.6 lib/gummi/repository_layer/repository/result.rb
gummi-0.3.5 lib/gummi/repository_layer/repository/result.rb
gummi-0.3.4 lib/gummi/repository_layer/repository/result.rb
gummi-0.3.3 lib/gummi/repository_layer/repository/result.rb