Class: Dbee::Query::Field

Inherits:
Object
  • Object
show all
Includes:
Aggregator
Defined in:
lib/dbee/query/field.rb

Overview

This class is an abstraction of the SELECT part of a SQL statement. The key_path is the relative path to the column while the display is the AS part of the SQL SELECT statement.

Defined Under Namespace

Modules: Aggregator

Constant Summary

Constants included from Aggregator

Aggregator::AVE, Aggregator::COUNT, Aggregator::MAX, Aggregator::MIN, Aggregator::SUM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_path:, aggregator: nil, display: nil, filters: []) ⇒ Field

Returns a new instance of Field.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
# File 'lib/dbee/query/field.rb', line 29

def initialize(key_path:, aggregator: nil, display: nil, filters: [])
  raise ArgumentError, 'key_path is required' if key_path.to_s.empty?

  @aggregator = aggregator ? Aggregator.const_get(aggregator.to_s.upcase.to_sym) : nil
  @display    = (display.to_s.empty? ? key_path : display).to_s
  @filters    = Filters.array(filters).uniq
  @key_path   = KeyPath.get(key_path)

  freeze
end

Instance Attribute Details

#aggregatorObject (readonly)

Returns the value of attribute aggregator.



27
28
29
# File 'lib/dbee/query/field.rb', line 27

def aggregator
  @aggregator
end

#displayObject (readonly)

Returns the value of attribute display.



27
28
29
# File 'lib/dbee/query/field.rb', line 27

def display
  @display
end

#filtersObject (readonly)

Returns the value of attribute filters.



27
28
29
# File 'lib/dbee/query/field.rb', line 27

def filters
  @filters
end

#key_pathObject (readonly)

Returns the value of attribute key_path.



27
28
29
# File 'lib/dbee/query/field.rb', line 27

def key_path
  @key_path
end

Instance Method Details

#<=>(other) ⇒ Object



66
67
68
# File 'lib/dbee/query/field.rb', line 66

def <=>(other)
  "#{key_path}#{display}" <=> "#{other.key_path}#{other.display}"
end

#==(other) ⇒ Object Also known as: eql?



57
58
59
60
61
62
63
# File 'lib/dbee/query/field.rb', line 57

def ==(other)
  other.instance_of?(self.class) &&
    other.aggregator == aggregator &&
    other.key_path == key_path &&
    other.filters == filters &&
    other.display == display
end

#aggregator?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dbee/query/field.rb', line 44

def aggregator?
  !aggregator.nil?
end

#filters?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dbee/query/field.rb', line 40

def filters?
  filters.any?
end

#hashObject



48
49
50
51
52
53
54
55
# File 'lib/dbee/query/field.rb', line 48

def hash
  [
    aggregator,
    display,
    filters,
    key_path
  ].hash
end

#key_pathsObject



70
71
72
# File 'lib/dbee/query/field.rb', line 70

def key_paths
  [key_path] + filters.map(&:key_path)
end