Class: Quo::Results

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utilities::Callstack
Defined in:
lib/quo/results.rb

Instance Method Summary collapse

Methods included from Utilities::Callstack

#debug_callstack

Constructor Details

#initialize(query, transformer: nil) ⇒ Results



11
12
13
14
15
# File 'lib/quo/results.rb', line 11

def initialize(query, transformer: nil)
  @query = query
  @unwrapped = query.unwrap
  @transformer = transformer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegate other enumerable methods to underlying collection but also transform



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/quo/results.rb', line 42

def method_missing(method, *args, **kwargs, &block)
  if unwrapped.respond_to?(method)
    debug_callstack
    if block
      unwrapped.send(method, *args, **kwargs) do |*block_args|
        x = block_args.first
        transformed = transformer ? transformer.call(x) : x
        other_args = block_args[1..] || []
        block.call(transformed, *other_args)
      end
    else
      raw = unwrapped.send(method, *args, **kwargs)
      # FIXME: consider how to handle applying a transformer to a Enumerator...
      return raw if raw.is_a?(Quo::Results) || raw.is_a?(::Enumerator)
      transform_results(raw)
    end
  else
    super
  end
end

Instance Method Details

#group_by(&block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/quo/results.rb', line 26

def group_by(&block)
  debug_callstack
  grouped = unwrapped.group_by do |*block_args|
    x = block_args.first
    transformed = transformer ? transformer.call(x) : x
    block ? block.call(transformed, *(block_args[1..] || [])) : transformed
  end

  grouped.tap do |groups|
    groups.transform_values! do |values|
      transformer ? values.map { |x| transformer.call(x) } : values
    end
  end
end

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



63
64
65
# File 'lib/quo/results.rb', line 63

def respond_to_missing?(name, include_private = false)
  enumerable_methods_supported.include?(name)
end