Class: DProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/stack-service-base/db_ext/db_proxy.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataset, model, path = [], &block) ⇒ DProxy

Returns a new instance of DProxy.



6
7
8
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 6

def initialize(dataset, model, path = [], &block)
  @model, @dataset, @final, @path = model, dataset, block, path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 11

def method_missing(name, *args, **kwargs, &block)
  all_args = [*args, **kwargs, block:].reject(&:nil?)
  # indent "DProxy::#{@model} #{name}(#{all_args}) => IN" unless QUIET

  response = if @final && name.to_sym == :all
     @final.call self, *all_args
   else
     @dataset.send(name, *args, **kwargs, &block)
   end
  # indent "DProxy::#{@model} #{name}(#{all_args}) OUT => #{response.inspect}" unless QUIET
  wrap?(response) ? self.class.new(response, @model, @path + [name => all_args, prev: self], &@final) : response
end

Instance Method Details

#_cloneObject



30
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 30

alias_method :_clone, :clone

#clone(*args, **kwargs, &block) ⇒ Object



32
33
34
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 32

def clone( *args, **kwargs, &block)
  method_missing(:clone, *args, **kwargs, &block)
end

#indent(str) ⇒ Object



10
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 10

def indent(str) = puts "\t" * @path.size + str

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

Returns:

  • (Boolean)


26
27
28
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 26

def respond_to_missing?(method_name, include_private = false)
  @dataset.respond_to?(method_name, include_private)
end

#wrap?(r) ⇒ Boolean

Returns:

  • (Boolean)


24
# File 'lib/stack-service-base/db_ext/db_proxy.rb', line 24

def wrap?(r) = r.is_a?(Sequel::Dataset::PlaceholderLiteralizer) || r.is_a?(Sequel::Dataset)