Class: ROM::Yesql::Relation

Inherits:
Relation
  • Object
show all
Defined in:
lib/rom/yesql/relation.rb

Overview

Yesql relation subclass

Class that inherits from this relation will be extended with methods based on its repository queries hash

It also supports overriding query_proc

Examples:

ROM.setup(:yesql, [uri, path: '/my/sql/queries/are/here'])

class Reports < ROM::Relation[:yesql]
  query_proc(proc { |name, query, *args|
    # magic if needed
  })
end

rom = ROM.finalize.env

rom.relation(:reports) # use like a normal rom relation

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dataset(name = Undefined) ⇒ Symbol

Set dataset name for the relation class

The class will be extended with queries registered under that name. By default dataset name is derived from the class name, which doesn't have to match they key under which its queries were registered

Returns:

  • (Symbol)

50
51
52
53
54
55
# File 'lib/rom/yesql/relation.rb', line 50

def self.dataset(name = Undefined)
  return @dataset if name == Undefined
  @dataset = name
  define_query_methods(self, Relation.queries[name] || {})
  @dataset
end

.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.

Extend provided klass with query methods

Parameters:

  • klass (Class)

    A relation class

  • queries (Hash)

    A hash with name, query pairs for the relation


63
64
65
66
67
68
69
70
71
# File 'lib/rom/yesql/relation.rb', line 63

def self.define_query_methods(klass, queries)
  queries.each do |name, query|
    klass.class_eval do
      define_method(name) do |*args|
        ROM::Relation.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


36
37
38
39
# File 'lib/rom/yesql/relation.rb', line 36

def self.inherited(klass)
  super
  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 repository to load all configured queries

Parameters:

  • queries (Hash)

    A hash with queries


87
88
89
90
91
92
93
94
95
# File 'lib/rom/yesql/relation.rb', line 87

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

.queriesHash

All loaded queries provided by repository

Returns:

  • (Hash)

78
79
80
# File 'lib/rom/yesql/relation.rb', line 78

def self.queries
  @queries || {}
end

Instance Method Details

#query_procProc

Return query proc set on a relation class

By default this returns whatever was set in the repository or the default one which simply uses hash % query to evaluate a query string

Returns:

  • (Proc)

105
106
107
# File 'lib/rom/yesql/relation.rb', line 105

def query_proc
  self.class.query_proc
end