Module: RailsQuery::Adapter::ClassMethods
- Defined in:
- lib/rails_query/adapter.rb
Overview
Class methods for defining base URL, client, queries, and mutations
Instance Method Summary collapse
- #base_url(url = nil) ⇒ Object
- #client(&block) ⇒ Object
- #mutation(name, &block) ⇒ Object
-
#query(name, ttl: nil, &block) ⇒ Object
rubocop:disable Metrics/MethodLength.
- #with(context = {}) ⇒ Object
Instance Method Details
#base_url(url = nil) ⇒ Object
12 13 14 15 16 |
# File 'lib/rails_query/adapter.rb', line 12 def base_url(url = nil) return @base_url if url.nil? @base_url = url end |
#client(&block) ⇒ Object
18 19 20 21 22 |
# File 'lib/rails_query/adapter.rb', line 18 def client(&block) return @client_block if block.nil? @client_block = block end |
#mutation(name, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rails_query/adapter.rb', line 50 def mutation(name, &block) define_method(name) do |*args, **opts| opts_with_context = inject_context(**opts) if block instance_exec(*args, **opts_with_context, &block) else mutation_class = resolve_mutation_class(name) mutation_class.call(*args, **opts_with_context) end end define_singleton_method(name) do |*args, **opts| instance.public_send(name, *args, **opts) end end |
#query(name, ttl: nil, &block) ⇒ Object
rubocop:disable Metrics/MethodLength
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rails_query/adapter.rb', line 31 def query(name, ttl: nil, &block) define_method(name) do |*args, **opts| opts_with_context = inject_context(**opts) if block execute_with_cache(name, ttl, args) do instance_exec(*args, **opts_with_context, &block) end else query_class = resolve_query_class(name) query_class.call(*args, **opts_with_context) end end define_singleton_method(name) do |*args, **opts| instance.public_send(name, *args, **opts) end end |
#with(context = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/rails_query/adapter.rb', line 24 def with(context = {}) reset_instance if context != instance.context instance.instance_variable_set(:@_context, context) instance end |