Class: Compendium::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/compendium/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Query

Returns a new instance of Query.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
# File 'lib/compendium/query.rb', line 13

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

#filtersObject (readonly)

Returns the value of attribute filters.



10
11
12
# File 'lib/compendium/query.rb', line 10

def filters
  @filters
end

#metricsObject (readonly)

Returns the value of attribute metrics.



10
11
12
# File 'lib/compendium/query.rb', line 10

def metrics
  @metrics
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/compendium/query.rb', line 10

def name
  @name
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/compendium/query.rb', line 11

def options
  @options
end

#procObject

Returns the value of attribute proc.



11
12
13
# File 'lib/compendium/query.rb', line 11

def proc
  @proc
end

#reportObject

Returns the value of attribute report.



11
12
13
# File 'lib/compendium/query.rb', line 11

def report
  @report
end

#resultsObject (readonly)

Returns the value of attribute results.



10
11
12
# File 'lib/compendium/query.rb', line 10

def results
  @results
end

Instance Method Details

#add_filter(filter) ⇒ Object



51
52
53
# File 'lib/compendium/query.rb', line 51

def add_filter(filter)
  @filters << filter
end

#add_metric(name, proc, options = {}) ⇒ Object



47
48
49
# File 'lib/compendium/query.rb', line 47

def add_metric(name, proc, options = {})
  Compendium::Metric.new(name, self.name, proc, options).tap { |m| @metrics << m }
end

#chart(template, *options, &block) ⇒ Object

Allow access to the chart object without having to explicitly render it



60
61
62
63
# File 'lib/compendium/query.rb', line 60

def chart(template, *options, &block)
  # Access the actual chart object
  Compendium::Presenters::Chart.new(template, self, *options, &block)
end

#empty?Boolean

A query is empty if it has no results

Returns:

  • (Boolean)


82
83
84
# File 'lib/compendium/query.rb', line 82

def empty?
  results.blank?
end

#initialize_cloneObject



23
24
25
26
# File 'lib/compendium/query.rb', line 23

def initialize_clone(*)
  super
  @metrics = @metrics.clone
end

#nil?Boolean

A query is nil if it has no proc

Returns:

  • (Boolean)


77
78
79
# File 'lib/compendium/query.rb', line 77

def nil?
  proc.nil?
end

#ran?Boolean Also known as: has_run?

Returns:

  • (Boolean)


71
72
73
# File 'lib/compendium/query.rb', line 71

def ran?
  !@results.nil?
end

#render_chart(template, *options, &block) ⇒ Object



65
66
67
68
69
# File 'lib/compendium/query.rb', line 65

def render_chart(template, *options, &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, *options, &block).render
end

#render_table(template, *options, &block) ⇒ Object



55
56
57
# File 'lib/compendium/query.rb', line 55

def render_table(template, *options, &block)
  Compendium::Presenters::Table.new(template, self, *options, &block).render unless empty?
end

#run(params, context = self) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/compendium/query.rb', line 28

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)



43
44
45
# File 'lib/compendium/query.rb', line 43

def url(params = {})
  report.url(params.merge(query: self.name))
end