Class: ROM::Yesql::Relation
- Inherits:
-
Relation
- Object
- Relation
- ROM::Yesql::Relation
- Extended by:
- Dry::Core::ClassAttributes
- Defined in:
- lib/rom/yesql/relation.rb,
lib/rom/yesql/relation/class_interface.rb
Overview
Yesql relation subclass
Class that inherits from this relation will be extended with methods based on its gateway queries hash
It also supports overriding query_proc
Defined Under Namespace
Modules: ClassInterface
Constant Summary collapse
- Materialized =
Class.new(ROM::Relation)
Class Method Summary collapse
-
.define_query_methods(klass, queries) ⇒ Object
private
Extends provided klass with query methods.
-
.inherited(klass) ⇒ Object
private
Extends a relation with query methods.
-
.load_queries(queries) ⇒ Object
private
Hook called by a gateway to load all configured queries.
-
.queries ⇒ Hash
private
All loaded queries provided by gateway.
Instance Method Summary collapse
-
#query_proc ⇒ Proc
Returns query proc set on a relation class.
Class Method Details
.define_query_methods(klass, queries) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extends provided klass with query methods
61 62 63 64 65 66 67 68 69 |
# File 'lib/rom/yesql/relation.rb', line 61 def self.define_query_methods(klass, queries) queries.each do |name, query| klass.class_eval do define_method(name) do |*args| Materialized.new(dataset.read(query_proc.call(name, query, *args))) end end end end |
.inherited(klass) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Extends a relation with query methods
This will only kick in if the derived dataset name matches the key under which relation queries were registered. If not it is expected that the dataset will be set manually
49 50 51 52 53 |
# File 'lib/rom/yesql/relation.rb', line 49 def self.inherited(klass) super klass.extend(ClassInterface) define_query_methods(klass, queries[klass.dataset] || {}) end |
.load_queries(queries) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Hook called by a gateway to load all configured queries
85 86 87 88 89 90 91 92 93 |
# File 'lib/rom/yesql/relation.rb', line 85 def self.load_queries(queries) @queries = {} queries.each do |ds, ds_queries| @queries[ds] = ds_queries.each_with_object({}) do |(name, query), h| h[name] = query end end @queries end |
.queries ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
All loaded queries provided by gateway
76 77 78 |
# File 'lib/rom/yesql/relation.rb', line 76 def self.queries @queries || {} end |
Instance Method Details
#query_proc ⇒ Proc
Returns query proc set on a relation class
By default this returns whatever was set in the gateway or the default one which simply uses hash % query to evaluate a query string
103 104 105 |
# File 'lib/rom/yesql/relation.rb', line 103 def query_proc self.class.query_proc end |