Module: Compendium::DSL

Included in:
Report
Defined in:
lib/compendium/dsl.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Allow defined queries to be redefined by name, eg: query :main_query main_query { collect_records_here }



98
99
100
101
102
103
104
105
106
107
# File 'lib/compendium/dsl.rb', line 98

def method_missing(name, *args, &block)
  if queries.keys.include?(name.to_sym)
    query = queries[name.to_sym]
    query.proc = block if block_given?
    query.options = args.extract_options!
    return query
  end

  super
end

Class Method Details

.extended(klass) ⇒ Object



8
9
10
11
12
# File 'lib/compendium/dsl.rb', line 8

def self.extended(klass)
  klass.inheritable_attr :queries, default: ::Collection[Query]
  klass.inheritable_attr :options, default: ::Collection[Option]
  klass.inheritable_attr :exporters, default: {}
end

Instance Method Details

#exports(type, *opts) ⇒ Object

Define any exports the report has



76
77
78
79
80
81
82
83
84
# File 'lib/compendium/dsl.rb', line 76

def exports(type, *opts)
  exporters[type] = if opts.empty?
    true
  elsif opts.length == 1
    opts.first
  else
    opts
  end
end

#filter(*query_names, &block) ⇒ Object

Define a filter to modify the results from specified query (in this case :deliveries) For example, this can be useful to translate columns prior to rendering, as it will apply for all render types (table, chart, JSON) Multiple queries can be set up with the same filter



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

def filter(*query_names, &block)
  each_query(query_names) do |query|
    query.add_filter(block)
  end
end

#metric(name, *args, &block) ⇒ Object

Define a metric from a query or implicitly A metric is a derived statistic from a report, for instance a count of rows



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/compendium/dsl.rb', line 39

def metric(name, *args, &block)
  proc = args.first.is_a?(Proc) ? args.first : block
  opts = args.extract_options!

  if opts.key?(:through)
    [opts.delete(:through)].flatten.each do |query|
      raise ArgumentError, "query #{query} is not defined" unless queries.key?(query)
      queries[query].add_metric(name, proc, opts)
    end
  else
    # Allow metrics to define queries implicitly
    # ie. if you need a metric that counts a column, there's no need to explicitly create a query
    # and just pass it into a metric
    query = define_query("__metric_#{name}", {}, &block)
    query.add_metric(name, -> result { result.first }, opts)
  end
end

#option(name, *args) ⇒ Object

Define a parameter for the report



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/compendium/dsl.rb', line 22

def option(name, *args)
  opts = args.extract_options!
  type = args.shift

  add_params_validations(name, opts.delete(:validates))

  if options[name]
    options[name].type = type if type
    options[name].default = opts.delete(:default) if opts.key?(:default)
    options[name].merge!(opts)
  else
    options << Compendium::Option.new(opts.merge(name: name, type: type))
  end
end

#params_classObject

Each Report will have its own descendant of Params in order to safely add validations



87
88
89
# File 'lib/compendium/dsl.rb', line 87

def params_class
  @params_class ||= Class.new(Params)
end

#params_class=(klass) ⇒ Object



91
92
93
# File 'lib/compendium/dsl.rb', line 91

def params_class=(klass)
  @params_class = klass
end

#query(name, opts = {}, &block) ⇒ Object Also known as: chart, data

Define a query



15
16
17
# File 'lib/compendium/dsl.rb', line 15

def query(name, opts = {}, &block)
  define_query(name, opts, &block)
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
112
# File 'lib/compendium/dsl.rb', line 109

def respond_to_missing?(name, *args)
  return true if queries.keys.include?(name)
  super
end

#table(*query_names, &block) ⇒ Object

Allow default table settings to be defined for a query. These settings are used when rendering a query to an HTML table or to CSV



69
70
71
72
73
# File 'lib/compendium/dsl.rb', line 69

def table(*query_names, &block)
  each_query(query_names) do |query|
    query.table_settings = block
  end
end