Class: RailsQuery::Query
- Inherits:
-
Object
- Object
- RailsQuery::Query
- Defined in:
- lib/rails_query/query.rb
Overview
Base class for queries
Class Method Summary collapse
Instance Method Summary collapse
- #build_key(*args, **opts) ⇒ Object
- #call(*args, **opts) ⇒ Object
- #kwargs? ⇒ Boolean
- #resolve ⇒ Object
- #ttl ⇒ Object
Class Method Details
.call(*args, **opts) ⇒ Object
7 8 9 |
# File 'lib/rails_query/query.rb', line 7 def call(*args, **opts) new.call(*args, **opts) end |
.key(&block) ⇒ Object
16 17 18 19 |
# File 'lib/rails_query/query.rb', line 16 def key(&block) @key_block = block if block @key_block end |
.ttl(value = nil) ⇒ Object
11 12 13 14 |
# File 'lib/rails_query/query.rb', line 11 def ttl(value = nil) @ttl = value if value @ttl end |
Instance Method Details
#build_key(*args, **opts) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/rails_query/query.rb', line 34 def build_key(*args, **opts) opts = opts.select { |k, _| self.class.key.parameters.map(&:last).include?(k) } return instance_exec(*args, **opts, &self.class.key) if self.class.key [self.class.name, args] end |
#call(*args, **opts) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/rails_query/query.rb', line 22 def call(*args, **opts) key = build_key(*args, **opts) provider_class = opts[:provider] || self.class.name RailsQuery.client.fetch(key, ttl: ttl, provider: provider_class) do kwargs? ? resolve(*args, **opts) : resolve(*args) end end |
#kwargs? ⇒ Boolean
41 42 43 |
# File 'lib/rails_query/query.rb', line 41 def kwargs? method(:resolve).parameters.any? { |type, _| %i[keyrest opt].include?(type) } end |
#resolve ⇒ Object
45 46 47 |
# File 'lib/rails_query/query.rb', line 45 def resolve(*) raise NotImplementedError end |
#ttl ⇒ Object
30 31 32 |
# File 'lib/rails_query/query.rb', line 30 def ttl self.class.ttl || RailsQuery.configuration.default_ttl end |