Class: PgHistogram::Histogram
- Inherits:
-
Object
- Object
- PgHistogram::Histogram
- Defined in:
- lib/pg_histogram/histogram.rb
Constant Summary collapse
- BUCKET_COL =
'bucket'- FREQUENCY_COL =
'frequency'- ROUND_METHODS_BY_DIRECTION =
{ nil => :round, down: :floor, up: :ceil }
Instance Attribute Summary collapse
-
#bucket_size ⇒ Object
readonly
Returns the value of attribute bucket_size.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#initialize(query, column_name, bucket_size = 0.5) ⇒ Histogram
constructor
column_name name must be safe for SQL injection.
- #max ⇒ Object
- #min ⇒ Object
-
#results ⇒ Object
returns histogram as hash bucket minimum as a key frequency as value.
Constructor Details
#initialize(query, column_name, bucket_size = 0.5) ⇒ Histogram
column_name name must be safe for SQL injection
14 15 16 17 18 |
# File 'lib/pg_histogram/histogram.rb', line 14 def initialize(query, column_name, bucket_size = 0.5) @query = query @column = column_name.to_s @bucket_size = bucket_size end |
Instance Attribute Details
#bucket_size ⇒ Object (readonly)
Returns the value of attribute bucket_size.
3 4 5 |
# File 'lib/pg_histogram/histogram.rb', line 3 def bucket_size @bucket_size end |
#column ⇒ Object (readonly)
Returns the value of attribute column.
3 4 5 |
# File 'lib/pg_histogram/histogram.rb', line 3 def column @column end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
3 4 5 |
# File 'lib/pg_histogram/histogram.rb', line 3 def query @query end |
Instance Method Details
#max ⇒ Object
36 37 38 |
# File 'lib/pg_histogram/histogram.rb', line 36 def max @max ||= round_to_increment(query.maximum(column), :up) end |
#min ⇒ Object
32 33 34 |
# File 'lib/pg_histogram/histogram.rb', line 32 def min @min ||= round_to_increment(query.minimum(column), :down) end |
#results ⇒ Object
returns histogram as hash bucket minimum as a key frequency as value
23 24 25 26 27 28 29 30 |
# File 'lib/pg_histogram/histogram.rb', line 23 def results # error handling case if max == min { min => query.where("#{column} = ?", min).count } else labeled_histogram end end |