Class: Rack::Queries::Cache

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

Defined Under Namespace

Classes: CreateQuery, Option, SelectOption

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



61
62
63
# File 'lib/rack/queries/cache.rb', line 61

def initialize
  @cache = []
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



59
60
61
# File 'lib/rack/queries/cache.rb', line 59

def cache
  @cache
end

Class Method Details

.instanceObject



104
105
106
# File 'lib/rack/queries/cache.rb', line 104

def instance
  @instance ||= new
end

Instance Method Details

#add(*queries) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rack/queries/cache.rb', line 65

def add(*queries)
  queries.each do |query|
    opts = {}

    (query.public_instance_methods(false) - %i[run]).each do |name|
      opts[name] = SelectOption.new(name)
    end

    query.define_singleton_method(:opts) { opts }
  end

  @cache = (cache + queries).sort_by(&:name)
end

#create(&block) ⇒ Object



79
80
81
82
# File 'lib/rack/queries/cache.rb', line 79

def create(&block)
  query = Class.new(CreateQuery, &block)
  @cache = (cache << query).sort_by(&:name)
end

#opts_for(name, opt) ⇒ Object



84
85
86
87
# File 'lib/rack/queries/cache.rb', line 84

def opts_for(name, opt)
  query = query_for(name)
  query.new.public_send(opt) if query
end

#queriesObject



89
90
91
92
93
94
95
96
97
# File 'lib/rack/queries/cache.rb', line 89

def queries
  cache.map do |query|
    desc = query.respond_to?(:desc) ? query.desc : ''
    opts =
      query.respond_to?(:opts) ? query.opts.values.map(&:as_json) : []

    { name: query.name, desc: desc, opts: opts }
  end
end

#query_for(name) ⇒ Object



99
100
101
# File 'lib/rack/queries/cache.rb', line 99

def query_for(name)
  cache.detect { |query| query.name == name }
end