Class: Quo::Results

Inherits:
Object
  • Object
show all
Defined in:
lib/quo/results.rb,
sig/generated/quo/results.rbs

Overview

Base results wrapper providing enumeration with optional transformation

Direct Known Subclasses

CollectionResults, RelationResults

Constant Summary collapse

FILTER_METHODS =

Returns:

  • (Object)
%i[
  select filter find_all reject
  find detect
  take_while drop_while
  sort_by min_by max_by
  uniq
].to_set.freeze
PAIR_FILTER_METHODS =

Returns:

  • (Object)
%i[partition minmax_by].to_set.freeze
PASSTHROUGH_METHODS =

Returns:

  • (Object)
%i[
  any? all? none? one? include? member?
  count tally sum
  each_with_object inject reduce
  map collect flat_map collect_concat filter_map
].to_set.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

This method returns an undefined value.

Delegate other enumerable methods to underlying collection but also transform



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/quo/results.rb', line 62

def method_missing(method, *args, **kwargs, &block)
  return super unless respond_to_missing?(method)

  if block
    raw = @configured_query.send(method, *args, **kwargs) do |*block_args|
      x = block_args.first
      transformed = transform? ? @transformer.call(x) : x
      other_args = block_args[1..] || []
      block.call(transformed, *other_args)
    end
    return raw if PASSTHROUGH_METHODS.include?(method)
    return transform_pair(raw) if PAIR_FILTER_METHODS.include?(method)
    return transform_results(raw) if FILTER_METHODS.include?(method)
    raw
  else
    raw = @configured_query.send(method, *args, **kwargs)
    return raw if raw.is_a?(Quo::RelationResults) || raw.is_a?(::Enumerator)
    return raw if PASSTHROUGH_METHODS.include?(method)
    transform_results(raw)
  end
end

Instance Method Details

#countInteger

Alias for total_count

Returns:

  • (Integer)


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

def count #: Integer
  total_count
end

#empty?Boolean

: bool

Returns:

  • (Boolean)


8
9
10
# File 'lib/quo/results.rb', line 8

def empty? #: bool
  !exists?
end

#group_by {|arg0, arg1| ... } ⇒ Hash[untyped, Array[untyped]]

Yields:

Yield Parameters:

  • arg0 (Object)
  • arg1 (Object)

Yield Returns:

  • (Object)

Returns:

  • (Hash[untyped, Array[untyped]])


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/quo/results.rb', line 46

def group_by(&block)
  grouped = @configured_query.group_by do |*block_args|
    x = block_args.first
    transformed = transform? ? @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

#page_sizeInteger

Alias for page_count

Returns:

  • (Integer)


23
24
25
# File 'lib/quo/results.rb', line 23

def page_size #: Integer
  page_count
end

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

Parameters:

  • name (Symbol)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


87
88
89
# File 'lib/quo/results.rb', line 87

def respond_to_missing?(name, include_private = false)
  @configured_query.respond_to?(name, include_private)
end

#sizeInteger

Alias for total_count

Returns:

  • (Integer)


18
19
20
# File 'lib/quo/results.rb', line 18

def size #: Integer
  total_count
end

#transform?Boolean

: bool

Returns:

  • (Boolean)


91
92
93
# File 'lib/quo/results.rb', line 91

def transform? #: bool
  @transformer.present?
end

#transform_pair(pair) ⇒ Object

Parameters:

  • pair (Object)

Returns:

  • (Object)


113
114
115
116
# File 'lib/quo/results.rb', line 113

def transform_pair(pair)
  return pair unless transform?
  pair.map { |part| transform_results(part) }
end

#transform_results(results) ⇒ Object

Parameters:

  • results (Object)

Returns:

  • (Object)


101
102
103
104
105
106
107
108
109
# File 'lib/quo/results.rb', line 101

def transform_results(results)
  return results unless transform?

  if results.is_a?(Enumerable)
    results.map.with_index { |item, i| @transformer.call(item, i) }
  else
    @transformer.call(results)
  end
end