class Percolate::Percolator

The class that percolates information from entities through facets to the user.

Attributes

adapter[R]
entities[R]

Public Class Methods

new(adapter) click to toggle source

The constructor.

@param adapter [Object] the adapter to a data source.

# File lib/percolate/percolator.rb, line 48
def initialize(adapter)
  @adapter = adapter
  @facet_cache = {}
end

Public Instance Methods

find(context, facet_name, *args) click to toggle source

Finds an entity or entities.

@param context [String] the lookup context. @param facet_name [Symbol] the facet name. @param args [Array] the argument splat passed to the facet.

@return [Object] the retrieved entity or entities.

# File lib/percolate/percolator.rb, line 70
def find(context, facet_name, *args)
  facet = find_facet(context, facet_name)

  if !facet
    return nil
  end

  case result = facet.find(*args)
  when Array
    result.map { |item| @entities[item] }
  when String
    @entities[result]
  when NilClass
    nil
  else
    raise "Bad facet return type #{result.class.name.dump}"
  end
end
find_facet(context, facet_name) click to toggle source

Finds a facet.

@param context [String] the lookup context. @param facet_name [Symbol] the facet name.

@return [Object] the facet.

# File lib/percolate/percolator.rb, line 95
def find_facet(context, facet_name)
  cache_key = [context, facet_name]

  if @facet_cache.include?(cache_key)
    facet = @facet_cache[cache_key]
  else
    begin
      require "percolate/facet/#{facet_name}_facet"
    rescue LoadError
      # Do nothing. Give the benefit of the doubt if the file doesn't exist.
    end

    if facet = @adapter.load_facet(context, facet_name)
      @facet_cache[cache_key] = facet
    end
  end

  facet
end
load(&block) click to toggle source

Configures and loads the underlying adapter's entities.

@param block [Proc] the configuration block.

# File lib/percolate/percolator.rb, line 56
def load(&block)
  @adapter.instance_eval(&block) if !block.nil?
  @entities = @adapter.load_entities

  nil
end