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)


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

#filtersObject (readonly)

Returns the value of attribute filters.



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

def filters
  @filters
end

#metricsObject (readonly)

Returns the value of attribute metrics.



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

def metrics
  @metrics
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#procObject

Returns the value of attribute proc.



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

def proc
  @proc
end

#reportObject

Returns the value of attribute report.



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

def report
  @report
end

#resultsObject (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, 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



62
63
64
65
# File 'lib/compendium/query.rb', line 62

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)


84
85
86
# File 'lib/compendium/query.rb', line 84

def empty?
  results.blank?
end

#initialize_cloneObject



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

Returns:

  • (Boolean)


79
80
81
# File 'lib/compendium/query.rb', line 79

def nil?
  proc.nil?
end

#ran?Boolean Also known as: has_run?

Returns:

  • (Boolean)


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, *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



57
58
59
# File 'lib/compendium/query.rb', line 57

def render_table(template, *options, &block)
  Compendium::Presenters::Table.new(template, self, *options, &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