Class: Rack::Queries::Cache
- Inherits:
-
Object
- Object
- Rack::Queries::Cache
- Extended by:
- Forwardable
- Defined in:
- lib/rack/queries/cache.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Class Method Summary collapse
Instance Method Summary collapse
- #add(*queries) ⇒ Object
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
- #opts_for(name, opt) ⇒ Object
- #queries ⇒ Object
- #query_for(name) ⇒ Object
Constructor Details
#initialize ⇒ Cache
Returns a new instance of Cache.
8 9 10 |
# File 'lib/rack/queries/cache.rb', line 8 def initialize @cache = [] end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
6 7 8 |
# File 'lib/rack/queries/cache.rb', line 6 def cache @cache end |
Class Method Details
.instance ⇒ Object
36 37 38 |
# File 'lib/rack/queries/cache.rb', line 36 def instance @instance ||= new end |
Instance Method Details
#add(*queries) ⇒ Object
12 13 14 |
# File 'lib/rack/queries/cache.rb', line 12 def add(*queries) @cache = (cache + queries).sort_by(&:name) end |
#opts_for(name, opt) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/rack/queries/cache.rb', line 16 def opts_for(name, opt) query = query_for(name) return unless query instance = query.new instance.public_send(opt) if instance.respond_to?(opt) end |
#queries ⇒ Object
24 25 26 27 28 29 |
# File 'lib/rack/queries/cache.rb', line 24 def queries cache.map do |query| opts = query.public_instance_methods(false) - %i[run] { name: query.name, opts: opts } end end |
#query_for(name) ⇒ Object
31 32 33 |
# File 'lib/rack/queries/cache.rb', line 31 def query_for(name) cache.detect { |query| query.name == name } end |