Module: RubyReportable::Report

Defined in:
lib/ruby_reportable/report.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#data_sourceObject

Returns the value of attribute data_source.



5
6
7
# File 'lib/ruby_reportable/report.rb', line 5

def data_source
  @data_source
end

#filtersObject

Returns the value of attribute filters.



5
6
7
# File 'lib/ruby_reportable/report.rb', line 5

def filters
  @filters
end

#records_returnedObject

Returns the value of attribute records_returned.



5
6
7
# File 'lib/ruby_reportable/report.rb', line 5

def records_returned
  @records_returned
end

Instance Method Details

#_data(sandbox, options = {}) ⇒ Object



124
125
126
# File 'lib/ruby_reportable/report.rb', line 124

def _data(sandbox, options = {})
  _filter(@filters, sandbox, options)
end

#_filter(filters, original_sandbox, options) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby_reportable/report.rb', line 98

def _filter(filters, original_sandbox, options)
  # sort filters by priority then apply to sandbox
  filters.sort_by do |filter_name, filter|
    filter[:priority]
  end.inject(original_sandbox) do |sandbox, (filter_name, filter)|

    # find input for given filter
    sandbox[:input] = options[:input][filter[:key]] if options[:input].is_a?(Hash)

    if filter[:valid].nil? || sandbox.instance_eval(&filter[:valid])
      if filter[:logic].nil?
        sandbox
      else
        sandbox.build(:source, filter[:logic])
      end
    else
      sandbox
    end
  end
end

#_finalize(sandbox, options = {}) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/ruby_reportable/report.rb', line 128

def _finalize(sandbox, options = {})
  if @finalize.nil?
    sandbox
  else
    sandbox[:inputs] = options[:input] || {}
    sandbox.build(:source, @finalize)
  end
end

#_group(group, data, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ruby_reportable/report.rb', line 163

def _group(group, data, options = {})
  # Run through elements in data which are ordered
  # from the previous call to #_sort via #run
  #
  # Since the elements are already sorted, as we pop
  # them into their grouping they will remain sorted as
  # intended
  #
  if group.to_s.empty?
    {:results => data}
  else
    group = [group] unless group.is_a?(Array)
    group.map!(&:to_s)

    # the last group critieria should contain an array
    # so lets pop it off for special use
    last_group = group.pop

    data.inject({}) do |hash, element|
      # grab a local reference to the hash
      ref = hash

      # run through initial groupings to grab local ref
      # and default them to {}
      group.map do |grouping|
        key = element[grouping]
        ref[key] ||= {}
        ref = ref[key]
      end

      # handle our last grouping
      key = element[last_group]
      ref[key] ||= []
      ref[key] << element

      hash
    end
  end
end

#_output(source_data, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ruby_reportable/report.rb', line 137

def _output(source_data, options = {})
  # build sandbox for building outputs
  sandbox = RubyReportable::Sandbox.new(:meta => @meta, @data_source[:as] => nil)

  source_data.inject([]) do |rows, element|
    # fill sandbox with data element
    sandbox[@data_source[:as]] = element

    # grab outputs
    rows << @outputs.inject({}) do |row, output|
      row[output.name] = sandbox.instance_eval(&output.logic)
      row
    end

    rows
  end
end

#_sort(sort, data, options = {}) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/ruby_reportable/report.rb', line 155

def _sort(sort, data, options = {})
  if sort.to_s.empty?
    data
  else
    data.sort_by {|element| element[sort]}
  end
end

#_source(options = {}) ⇒ Object



119
120
121
122
# File 'lib/ruby_reportable/report.rb', line 119

def _source(options = {})
  # build sandbox for getting the data
  RubyReportable::Sandbox.new(:meta => @meta, :source => @data_source[:logic], :inputs => options[:input] || {}, :input => nil)
end

#benchmark(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_reportable/report.rb', line 23

def benchmark(name)
  benchmarks[name] ||= 0.0

  @result = nil

  time = Benchmark.realtime do
    @result = yield
  end

  benchmarks[name] += time
  @result
end

#benchmarksObject

:sandbox, :filters, :finalize, :output, :group, :sort



19
20
21
# File 'lib/ruby_reportable/report.rb', line 19

def benchmarks
  @benchmarks ||= {}
end

#category(string = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ruby_reportable/report.rb', line 56

def category(string = nil)
  if string.nil?
    @category
  else
    @category = string
  end
end

#clearObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/ruby_reportable/report.rb', line 7

def clear
  @outputs = []
  @filters = {}
  @data_source = nil
  @report = self.to_s
  @category = 'Reports'
  @meta = {}
  @benchmarks = {}
  @records_returned = 0
end

#filter(name, &block) ⇒ Object



69
70
71
72
# File 'lib/ruby_reportable/report.rb', line 69

def filter(name, &block)
  @filters[name] = RubyReportable::Filter.new(name)
  @filters[name].instance_eval(&block)
end

#finalize(&block) ⇒ Object



74
75
76
# File 'lib/ruby_reportable/report.rb', line 74

def finalize(&block)
  @finalize = block
end

#meta(key, value = nil, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_reportable/report.rb', line 36

def meta(key, value = nil, &block)
  if block_given?
    @meta[key] = block
  else
    if value.nil?
      @meta[key]
    else
      @meta[key] = value
    end
  end
end

#output(name, options = {}, &block) ⇒ Object



78
79
80
# File 'lib/ruby_reportable/report.rb', line 78

def output(name, options = {}, &block)
  @outputs << RubyReportable::Output.new(name, options, block)
end

#outputs(hidden = false) ⇒ Object

methods you shouldn’t use inside the blocks



86
87
88
89
90
91
92
# File 'lib/ruby_reportable/report.rb', line 86

def outputs(hidden = false)
  if hidden
    @outputs
  else
    @outputs.select {|output| output[:hidden] != true}
  end
end

#report(string = nil) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/ruby_reportable/report.rb', line 48

def report(string = nil)
  if string.nil?
    @report
  else
    @report = string
  end
end

#run(options = {}) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ruby_reportable/report.rb', line 203

def run(options = {})
  options = {:input => {}}.merge(options)

  # initial sandbox
  sandbox = benchmark(:sandbox) do
    _source(options)
  end

  # apply filters to source
  filtered_sandbox = benchmark(:filters) do
    _data(sandbox, options)
  end

  # finalize raw data from source
  source_data = benchmark(:finalize) do
    _finalize(filtered_sandbox, options).source
  end

  # {:default => [{outputs => values}]
  data = benchmark(:output) do
    _output(source_data, options)
  end

  # now that we have all of our data go ahead and cache the size
  records_returned = data.size

  # sort the data first cause that makes sense you know
  sorted = benchmark(:sort) do
    _sort(options[:sort], data, options)
  end

  # transform into {group => {group => [outputs => values]}}
  # level of grouping depends on how many groups are passed in
  benchmark(:group) do
    _group(options[:group], sorted, options)
  end
end

#source(&block) ⇒ Object



64
65
66
67
# File 'lib/ruby_reportable/report.rb', line 64

def source(&block)
  @data_source = RubyReportable::Source.new
  @data_source.instance_eval(&block)
end

#useable_filters(scope) ⇒ Object



94
95
96
# File 'lib/ruby_reportable/report.rb', line 94

def useable_filters(scope)
  @filters.values.select {|filter| !filter[:input].nil? && (filter[:use].nil? || filter[:use].call(scope))}.sort_by {|filter| filter[:priority].to_i}
end

#valid?(options = {}) ⇒ Boolean

end def run



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/ruby_reportable/report.rb', line 241

def valid?(options = {})
  options = {:input => {}}.merge(options)
  errors = []

  # initial sandbox
  sandbox = _source(options)

  # add in inputs
  sandbox[:inputs] = options[:input]

  validity = @filters.map do |filter_name, filter|

    # find input for given filter
    sandbox[:input] = options[:input][filter[:key]] if options[:input].is_a?(Hash)

    filter_validity = filter[:valid].nil? || sandbox.instance_eval(&filter[:valid])

    if filter_validity == false
      # Ignore an empty filter unless it's required
      if !sandbox[:input].to_s.blank?
        errors << "#{filter_name} is invalid."
        false
      else
        if sandbox[:input].to_s.blank? && !filter[:require].blank?
          errors << "#{filter_name} is required."
          false
        else
          true
        end
      end
    elsif filter_validity == true
      if sandbox[:input].to_s.blank? && !filter[:require].blank?
        errors << "#{filter_name} is required."
        false
      else
        true
      end
    elsif !filter_validity.nil? && !filter_validity[:status].nil? && filter_validity[:status] == false
      # Ignore an empty filter unless it's required
      if !sandbox[:input].to_s.blank?
        errors << filter_validity[:errors]
        false
      else
        if sandbox[:input].to_s.blank? && !filter[:require].blank?
          errors << "#{filter_name} is required."
          false
        else
          true
        end
      end
    end
  end

  return {:status => !validity.include?(false), :errors => errors}

end