Class: Rack::Queries::Cache
- Inherits:
-
Object
- Object
- Rack::Queries::Cache
- Extended by:
- Forwardable
- Defined in:
- lib/rack/queries/cache.rb
Defined Under Namespace
Classes: CreateQuery
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
Class Method Summary collapse
Instance Method Summary collapse
- #add(*queries) ⇒ Object
- #create(&block) ⇒ 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.
34 35 36 |
# File 'lib/rack/queries/cache.rb', line 34 def initialize @cache = [] end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
32 33 34 |
# File 'lib/rack/queries/cache.rb', line 32 def cache @cache end |
Class Method Details
.instance ⇒ Object
69 70 71 |
# File 'lib/rack/queries/cache.rb', line 69 def instance @instance ||= new end |
Instance Method Details
#add(*queries) ⇒ Object
38 39 40 |
# File 'lib/rack/queries/cache.rb', line 38 def add(*queries) @cache = (cache + queries).sort_by(&:name) end |
#create(&block) ⇒ Object
42 43 44 45 |
# File 'lib/rack/queries/cache.rb', line 42 def create(&block) query = Class.new(CreateQuery, &block) @cache = (cache << query).sort_by(&:name) end |
#opts_for(name, opt) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/rack/queries/cache.rb', line 47 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
55 56 57 58 59 60 61 62 |
# File 'lib/rack/queries/cache.rb', line 55 def queries cache.map do |query| desc = query.respond_to?(:desc) ? query.desc : '' opts = query.public_instance_methods(false) - %i[run] { name: query.name, desc: desc, opts: opts } end end |
#query_for(name) ⇒ Object
64 65 66 |
# File 'lib/rack/queries/cache.rb', line 64 def query_for(name) cache.detect { |query| query.name == name } end |