Class: ROM::Yesql::Relation

Inherits:
Relation
  • Object
show all
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

Examples:

conf = ROM::Configuration.new(
  :yesql, ['sqlite::memory', path: '/my/sql/queries/are/here']
)

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

conf.register_relation(Reports)

rom = ROM.container(conf)

rom.relations[:reports] # use like a normal rom relation

Defined Under Namespace

Modules: ClassInterface

Constant Summary collapse

Materialized =
Class.new(ROM::Relation)

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • klass (Class)

    A relation class

  • queries (Hash)

    A hash with name, query pairs for the relation



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

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



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

def self.inherited(klass)
  super
  klass.extend(ClassInterface)
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

Parameters:

  • queries (Hash)

    A hash with queries



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

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

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

Returns:

  • (Hash)


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

def self.queries
  @queries || {}
end

Instance Method Details

#query_procProc

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

Returns:

  • (Proc)


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

def query_proc
  self.class.query_proc
end