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



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

Parameters:

  • queries (Hash)

    A hash with 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

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


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

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)


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

def query_proc
  self.class.query_proc
end