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

Instance Method Details

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



115
116
117
# File 'lib/ruby_reportable/report.rb', line 115

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

#_filter(filters, original_sandbox, options) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby_reportable/report.rb', line 89

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



119
120
121
122
123
124
125
126
# File 'lib/ruby_reportable/report.rb', line 119

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

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



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ruby_reportable/report.rb', line 146

def _group(group, data, options = {})
  unless group.to_s.empty?
    data[:results].inject({}) do |hash, element|
      key = element[group]
      hash[key] ||= []
      hash[key] << element
      hash
    end
  else
    data
  end
end

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ruby_reportable/report.rb', line 128

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

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

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

    rows
  end
end

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



159
160
161
162
163
164
165
166
167
168
# File 'lib/ruby_reportable/report.rb', line 159

def _sort(sort, data, options = {})
  unless sort.to_s.empty?
    data.inject(Hash.new([])) do |hash, (group, elements)|
      hash[group] = elements.sort_by {|element| element[sort]}
      hash
    end
  else
    data
  end
end

#_source(options = {}) ⇒ Object



110
111
112
113
# File 'lib/ruby_reportable/report.rb', line 110

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



22
23
24
25
# File 'lib/ruby_reportable/report.rb', line 22

def benchmark(name)
  benchmarks[name] ||= 0.0
  benchmarks[name] += (Benchmark.realtime { yield if block_given? } * 1000)
end

#benchmarksObject

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



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

def benchmarks
  @benchmarks ||= {}
end

#category(string = nil) ⇒ Object



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

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

#clearObject



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

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

#filter(name, &block) ⇒ Object



60
61
62
63
# File 'lib/ruby_reportable/report.rb', line 60

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

#finalize(&block) ⇒ Object



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

def finalize(&block)
  @finalize = block
end

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_reportable/report.rb', line 27

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



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

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



77
78
79
80
81
82
83
# File 'lib/ruby_reportable/report.rb', line 77

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

#report(string = nil) ⇒ Object



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

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

#run(options = {}) ⇒ Object



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
202
# File 'lib/ruby_reportable/report.rb', line 170

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

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

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

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

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

  # transform into {group => [outputs => values]}
  benchmark(:group) do
    grouped = _group(options[:group], data, options)
  end

  # sort grouped data
  benchmark(:sort) do
    _sort(options[:sort], grouped, options)
  end
end

#source(&block) ⇒ Object



55
56
57
58
# File 'lib/ruby_reportable/report.rb', line 55

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

#useable_filters(scope) ⇒ Object



85
86
87
# File 'lib/ruby_reportable/report.rb', line 85

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

Returns:

  • (Boolean)


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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/ruby_reportable/report.rb', line 204

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