Class: Dbee::Query::Field
- Inherits:
-
Object
- Object
- Dbee::Query::Field
- 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
-
#aggregator ⇒ Object
readonly
Returns the value of attribute aggregator.
-
#display ⇒ Object
readonly
Returns the value of attribute display.
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#key_path ⇒ Object
readonly
Returns the value of attribute key_path.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #aggregator? ⇒ Boolean
- #filters? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(key_path:, aggregator: nil, display: nil, filters: []) ⇒ Field
constructor
A new instance of Field.
- #key_paths ⇒ Object
Constructor Details
#initialize(key_path:, aggregator: nil, display: nil, filters: []) ⇒ Field
Returns a new instance of Field.
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
#aggregator ⇒ Object (readonly)
Returns the value of attribute aggregator.
27 28 29 |
# File 'lib/dbee/query/field.rb', line 27 def aggregator @aggregator end |
#display ⇒ Object (readonly)
Returns the value of attribute display.
27 28 29 |
# File 'lib/dbee/query/field.rb', line 27 def display @display end |
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
27 28 29 |
# File 'lib/dbee/query/field.rb', line 27 def filters @filters end |
#key_path ⇒ Object (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
44 45 46 |
# File 'lib/dbee/query/field.rb', line 44 def aggregator? !aggregator.nil? end |
#filters? ⇒ Boolean
40 41 42 |
# File 'lib/dbee/query/field.rb', line 40 def filters? filters.any? end |
#hash ⇒ Object
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_paths ⇒ Object
70 71 72 |
# File 'lib/dbee/query/field.rb', line 70 def key_paths [key_path] + filters.map(&:key_path) end |