Class: Percolate::Percolator
- Inherits:
-
Object
- Object
- Percolate::Percolator
- Defined in:
- lib/percolate/percolator.rb
Overview
The class that percolates information from entities through facets to the user.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
Instance Method Summary collapse
-
#find(context, facet_name, *args) ⇒ Object
Finds an entity or entities.
-
#find_facet(context, facet_name) ⇒ Object
Finds a facet.
-
#initialize(adapter) ⇒ Percolator
constructor
The constructor.
-
#load(&block) ⇒ Object
Configures and loads the underlying adapter’s entities.
Constructor Details
#initialize(adapter) ⇒ Percolator
The constructor.
49 50 51 52 |
# File 'lib/percolate/percolator.rb', line 49 def initialize(adapter) @adapter = adapter @facet_cache = {} end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
44 45 46 |
# File 'lib/percolate/percolator.rb', line 44 def adapter @adapter end |
#entities ⇒ Object (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.
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.
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.
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 |