Class: Compendium::Query
- Inherits:
-
Object
- Object
- Compendium::Query
- Defined in:
- lib/compendium/query.rb
Direct Known Subclasses
CollectionQuery, CountQuery, Presenters::Chart, SumQuery, ThroughQuery
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#proc ⇒ Object
Returns the value of attribute proc.
-
#report ⇒ Object
Returns the value of attribute report.
-
#results ⇒ Object
readonly
Returns the value of attribute results.
Instance Method Summary collapse
- #add_filter(filter) ⇒ Object
- #add_metric(name, proc, options = {}) ⇒ Object
-
#chart(template, *options, &block) ⇒ Object
Allow access to the chart object without having to explicitly render it.
-
#empty? ⇒ Boolean
A query is empty if it has no results.
-
#initialize(*args) ⇒ Query
constructor
A new instance of Query.
- #initialize_clone ⇒ Object
-
#nil? ⇒ Boolean
A query is nil if it has no proc.
- #ran? ⇒ Boolean (also: #has_run?)
- #render_chart(template, *options, &block) ⇒ Object
- #render_table(template, *options, &block) ⇒ Object
- #run(params, context = self) ⇒ Object
-
#url(params = {}) ⇒ Object
Get a URL for this query (format: :json set by default).
Constructor Details
#initialize(*args) ⇒ Query
Returns a new instance of Query.
14 15 16 17 18 19 20 21 22 |
# File 'lib/compendium/query.rb', line 14 def initialize(*args) @report = args.shift if arg_is_report?(args.first) raise ArgumentError, "wrong number of arguments (#{args.size + (@report ? 1 : 0)} for 3..4)" unless args.size == 3 @name, @options, @proc = args @metrics = ::Collection[Metric] @filters = ::Collection[Proc] end |
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
11 12 13 |
# File 'lib/compendium/query.rb', line 11 def filters @filters end |
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
11 12 13 |
# File 'lib/compendium/query.rb', line 11 def metrics @metrics end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/compendium/query.rb', line 11 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/compendium/query.rb', line 12 def @options end |
#proc ⇒ Object
Returns the value of attribute proc.
12 13 14 |
# File 'lib/compendium/query.rb', line 12 def proc @proc end |
#report ⇒ Object
Returns the value of attribute report.
12 13 14 |
# File 'lib/compendium/query.rb', line 12 def report @report end |
#results ⇒ Object (readonly)
Returns the value of attribute results.
11 12 13 |
# File 'lib/compendium/query.rb', line 11 def results @results end |
Instance Method Details
#add_filter(filter) ⇒ Object
53 54 55 |
# File 'lib/compendium/query.rb', line 53 def add_filter(filter) @filters << filter end |
#add_metric(name, proc, options = {}) ⇒ Object
49 50 51 |
# File 'lib/compendium/query.rb', line 49 def add_metric(name, proc, = {}) Compendium::Metric.new(name, self.name, proc, ).tap { |m| @metrics << m } end |
#chart(template, *options, &block) ⇒ Object
Allow access to the chart object without having to explicitly render it
62 63 64 65 |
# File 'lib/compendium/query.rb', line 62 def chart(template, *, &block) # Access the actual chart object Compendium::Presenters::Chart.new(template, self, *, &block) end |
#empty? ⇒ Boolean
A query is empty if it has no results
84 85 86 |
# File 'lib/compendium/query.rb', line 84 def empty? results.blank? end |
#initialize_clone ⇒ Object
24 25 26 27 28 |
# File 'lib/compendium/query.rb', line 24 def initialize_clone(*) super @metrics = @metrics.clone @filters = @filters.clone end |
#nil? ⇒ Boolean
A query is nil if it has no proc
79 80 81 |
# File 'lib/compendium/query.rb', line 79 def nil? proc.nil? end |
#ran? ⇒ Boolean Also known as: has_run?
73 74 75 |
# File 'lib/compendium/query.rb', line 73 def ran? !@results.nil? end |
#render_chart(template, *options, &block) ⇒ Object
67 68 69 70 71 |
# File 'lib/compendium/query.rb', line 67 def render_chart(template, *, &block) # A query can be rendered regardless of if it has data or not # Rendering a chart with no result set builds a chart scaffold which can be updated through AJAX chart(template, *, &block).render end |
#render_table(template, *options, &block) ⇒ Object
57 58 59 |
# File 'lib/compendium/query.rb', line 57 def render_table(template, *, &block) Compendium::Presenters::Table.new(template, self, *, &block).render unless empty? end |
#run(params, context = self) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/compendium/query.rb', line 30 def run(params, context = self) if report.is_a?(Class) # If running a query directly from a class rather than an instance, the class's query should # not be affected/modified, so run the query without a reference back to the report. # Otherwise, if the class is subsequently instantiated, the instance will already have results. dup.tap{ |q| q.report = nil }.run(params, context) else collect_results(context, params) collect_metrics(context) @results end end |
#url(params = {}) ⇒ Object
Get a URL for this query (format: :json set by default)
45 46 47 |
# File 'lib/compendium/query.rb', line 45 def url(params = {}) report.url(params.merge(query: self.name)) end |