Class: AuxiliaryRails::Application::Query Abstract

Inherits:
Object
  • Object
show all
Extended by:
Dry::Core::ClassAttributes, Dry::Initializer
Includes:
Concerns::Callable, Concerns::Errorable
Defined in:
lib/auxiliary_rails/application/query.rb

Overview

This class is abstract.

Instance Method Summary collapse

Methods included from Concerns::Errorable

#error!

Methods included from Concerns::Callable

included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



36
37
38
39
40
# File 'lib/auxiliary_rails/application/query.rb', line 36

def method_missing(method_name, *args, &block)
  super unless query.respond_to?(method_name)

  query.send(method_name, *args, &block)
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/auxiliary_rails/application/query.rb', line 19

def call
  ensure_proper_relation_types!

  perform

  if !queriable_object?(query)
    error!('Invalid class of resulted query')
  end

  query
end

#ensure_proper_relation_types!Object (private)

rubocop:disable Style/GuardClause



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/auxiliary_rails/application/query.rb', line 49

def ensure_proper_relation_types!
  if self.class.default_relation.nil?
    error!('Undefined `default_relation`')
  end
  if !queriable_object?(self.class.default_relation)
    error!('Invalid class of `default_relation`')
  end
  if !relation.nil? && !queriable_object?(relation)
    error!('Invalid class of `relation` option')
  end
end

#performObject

This method is abstract.

Raises:

  • (NotImplementedError)


32
33
34
# File 'lib/auxiliary_rails/application/query.rb', line 32

def perform
  raise NotImplementedError
end

#queriable_object?(object) ⇒ Boolean (private)

rubocop:enable Style/GuardClause

Returns:

  • (Boolean)


62
63
64
# File 'lib/auxiliary_rails/application/query.rb', line 62

def queriable_object?(object)
  object.is_a?(ActiveRecord::Relation)
end

#query(scoped_query = nil) ⇒ Object (private)



66
67
68
69
70
71
72
# File 'lib/auxiliary_rails/application/query.rb', line 66

def query(scoped_query = nil)
  @query ||= (relation || self.class.default_relation)

  @query = scoped_query unless scoped_query.nil?

  @query
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/auxiliary_rails/application/query.rb', line 42

def respond_to_missing?(method_name, include_private = false)
  query.respond_to?(method_name) || super
end