Class: Rack::Queries::Cache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/rack/queries/cache.rb

Defined Under Namespace

Classes: CreateQuery

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



34
35
36
# File 'lib/rack/queries/cache.rb', line 34

def initialize
  @cache = []
end

Instance Attribute Details

#cacheObject (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

.instanceObject



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

#queriesObject



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