Class: Percolate::Percolator

Inherits:
Object
  • Object
show all
Defined in:
lib/percolate/percolator.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ Percolator

The constructor.

Parameters:

  • adapter (Object)

    the adapter to a data source.



49
50
51
52
# File 'lib/percolate/percolator.rb', line 49

def initialize(adapter)
  @adapter = adapter
  @facet_cache = {}
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



44
45
46
# File 'lib/percolate/percolator.rb', line 44

def adapter
  @adapter
end

#entitiesObject (readonly)

Returns the value of attribute entities.



44
45
46
# File 'lib/percolate/percolator.rb', line 44

def entities
  @entities
end

Instance Method Details

#find(context, facet_name, *args) ⇒ Object

Finds an entity or entities.

Parameters:

  • context (String)

    the lookup context.

  • facet_name (Symbol)

    the facet name.

  • args (Array)

    the argument splat passed to the facet.

Returns:

  • (Object)

    the retrieved entity or entities.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/percolate/percolator.rb', line 71

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) ⇒ Object

Finds a facet.

Parameters:

  • context (String)

    the lookup context.

  • facet_name (Symbol)

    the facet name.

Returns:

  • (Object)

    the facet.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/percolate/percolator.rb', line 96

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) ⇒ Object

Configures and loads the underlying adapter’s entities.

Parameters:

  • block (Proc)

    the configuration block.



57
58
59
60
61
62
# File 'lib/percolate/percolator.rb', line 57

def load(&block)
  @adapter.instance_eval(&block) if !block.nil?
  @entities = @adapter.load_entities

  nil
end