Module: RubyReportable::Report
- Defined in:
- lib/ruby_reportable/report.rb
Instance Attribute Summary collapse
-
#data_source ⇒ Object
Returns the value of attribute data_source.
-
#filters ⇒ Object
Returns the value of attribute filters.
Instance Method Summary collapse
- #_data(sandbox, options = {}) ⇒ Object
- #_filter(filters, original_sandbox, options) ⇒ Object
- #_finalize(sandbox, options = {}) ⇒ Object
- #_group(group, data, options = {}) ⇒ Object
- #_output(source_data, options = {}) ⇒ Object
- #_sort(sort, data, options = {}) ⇒ Object
- #_source(options = {}) ⇒ Object
- #benchmark(name) ⇒ Object
-
#benchmarks ⇒ Object
:sandbox, :filters, :finalize, :output, :group, :sort.
- #category(string = nil) ⇒ Object
- #clear ⇒ Object
- #filter(name, &block) ⇒ Object
- #finalize(&block) ⇒ Object
- #meta(key, value = nil, &block) ⇒ Object
- #output(name, options = {}, &block) ⇒ Object
-
#outputs(hidden = false) ⇒ Object
methods you shouldn’t use inside the blocks.
- #report(string = nil) ⇒ Object
- #run(options = {}) ⇒ Object
- #source(&block) ⇒ Object
- #useable_filters(scope) ⇒ Object
-
#valid?(options = {}) ⇒ Boolean
end def run.
Instance Attribute Details
#data_source ⇒ Object
Returns the value of attribute data_source.
5 6 7 |
# File 'lib/ruby_reportable/report.rb', line 5 def data_source @data_source end |
#filters ⇒ Object
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, = {}) _filter(@filters, sandbox, ) 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, ) # 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] = [:input][filter[:key]] if [: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, = {}) if @finalize.nil? sandbox else sandbox[:inputs] = [: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, = {}) 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, = {}) # build sandbox for building outputs sandbox = RubyReportable::Sandbox.new(: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, = {}) 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( = {}) # build sandbox for getting the data RubyReportable::Sandbox.new(:meta => , :source => @data_source[:logic], :inputs => [: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 |
#benchmarks ⇒ Object
: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 |
#clear ⇒ Object
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' = {} @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 (key, value = nil, &block) if block_given? [key] = block else if value.nil? [key] else [key] = value end end end |
#output(name, options = {}, &block) ⇒ Object
69 70 71 |
# File 'lib/ruby_reportable/report.rb', line 69 def output(name, = {}, &block) @outputs << RubyReportable::Output.new(name, , 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( = {}) = {:input => {}}.merge() # initial sandbox benchmark(:sandbox) do sandbox = _source() end # apply filters to source benchmark(:filters) do filtered_sandbox = _data(sandbox, ) end # finalize raw data from source benchmark(:finalize) do source_data = _finalize(filtered_sandbox, ).source end # {:default => [{outputs => values}] benchmark(:output) do data = _output(source_data, ) end # transform into {group => [outputs => values]} benchmark(:group) do grouped = _group([:group], data, ) end # sort grouped data benchmark(:sort) do _sort([:sort], grouped, ) 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
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?( = {}) = {:input => {}}.merge() errors = [] # initial sandbox sandbox = _source() # add in inputs sandbox[:inputs] = [:input] validity = @filters.map do |filter_name, filter| # find input for given filter sandbox[:input] = [:input][filter[:key]] if [: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 |