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.
-
#records_returned ⇒ Object
Returns the value of attribute records_returned.
Instance Method Summary collapse
- #_convert_for_sort(value) ⇒ Object
- #_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
- #allow_group(string = nil) ⇒ Object
- #allow_sort(string = nil) ⇒ 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.
6 7 8 |
# File 'lib/ruby_reportable/report.rb', line 6 def data_source @data_source end |
#filters ⇒ Object
Returns the value of attribute filters.
6 7 8 |
# File 'lib/ruby_reportable/report.rb', line 6 def filters @filters end |
#records_returned ⇒ Object
Returns the value of attribute records_returned.
6 7 8 |
# File 'lib/ruby_reportable/report.rb', line 6 def records_returned @records_returned end |
Instance Method Details
#_convert_for_sort(value) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ruby_reportable/report.rb', line 174 def _convert_for_sort(value) case value.class when NilClass value.to_s when Date, DateTime, Time value when Array value.map {|sub| _convert_for_sort(sub)} else value.to_s end end |
#_data(sandbox, options = {}) ⇒ Object
143 144 145 |
# File 'lib/ruby_reportable/report.rb', line 143 def _data(sandbox, = {}) _filter(@filters, sandbox, ) end |
#_filter(filters, original_sandbox, options) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ruby_reportable/report.rb', line 117 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
147 148 149 150 151 152 153 154 |
# File 'lib/ruby_reportable/report.rb', line 147 def _finalize(sandbox, = {}) if @finalize.nil? sandbox else sandbox[:inputs] = [:input] || {} sandbox.build(:source, @finalize) end end |
#_group(group, data, options = {}) ⇒ Object
197 198 199 200 201 202 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 |
# File 'lib/ruby_reportable/report.rb', line 197 def _group(group, data, = {}) # 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
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/ruby_reportable/report.rb', line 156 def _output(source_data, = {}) # build sandbox for building outputs sandbox = RubyReportable::Sandbox.new(:meta => @meta, :inputs => [:input] || {}, @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
187 188 189 190 191 192 193 194 195 |
# File 'lib/ruby_reportable/report.rb', line 187 def _sort(sort, data, = {}) if sort.to_s.empty? data else sort = [sort] unless sort.is_a?(Array) data.sort_by {|element| sort.map {|column| _convert_for_sort(element[column]) }} end end |
#_source(options = {}) ⇒ Object
138 139 140 141 |
# File 'lib/ruby_reportable/report.rb', line 138 def _source( = {}) # build sandbox for getting the data RubyReportable::Sandbox.new(:meta => @meta, :source => @data_source[:logic], :inputs => [:input] || {}, :input => nil) end |
#allow_group(string = nil) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/ruby_reportable/report.rb', line 59 def allow_group(string = nil) if string.nil? @allow_group else @allow_group = string end end |
#allow_sort(string = nil) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/ruby_reportable/report.rb', line 67 def allow_sort(string = nil) if string.nil? @allow_sort else @allow_sort = string end end |
#benchmark(name) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ruby_reportable/report.rb', line 26 def benchmark(name) benchmarks[name] ||= 0.0 @result = nil time = Benchmark.realtime do @result = yield end benchmarks[name] += time @result end |
#benchmarks ⇒ Object
:sandbox, :filters, :finalize, :output, :group, :sort
22 23 24 |
# File 'lib/ruby_reportable/report.rb', line 22 def benchmarks @benchmarks ||= {} end |
#category(string = nil) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/ruby_reportable/report.rb', line 75 def category(string = nil) if string.nil? @category else @category = string end end |
#clear ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_reportable/report.rb', line 8 def clear @outputs = [] @filters = {} @data_source = nil @report = self.to_s @category = 'Reports' @allow_group = true @allow_sort = true @meta = {} @benchmarks = {} @records_returned = 0 end |
#filter(name, &block) ⇒ Object
88 89 90 91 |
# File 'lib/ruby_reportable/report.rb', line 88 def filter(name, &block) @filters[name] = RubyReportable::Filter.new(name) @filters[name].instance_eval(&block) end |
#finalize(&block) ⇒ Object
93 94 95 |
# File 'lib/ruby_reportable/report.rb', line 93 def finalize(&block) @finalize = block end |
#meta(key, value = nil, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/ruby_reportable/report.rb', line 39 def (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
97 98 99 |
# File 'lib/ruby_reportable/report.rb', line 97 def output(name, = {}, &block) @outputs << RubyReportable::Output.new(name, , block) end |
#outputs(hidden = false) ⇒ Object
methods you shouldn’t use inside the blocks
105 106 107 108 109 110 111 |
# File 'lib/ruby_reportable/report.rb', line 105 def outputs(hidden = false) if hidden @outputs else @outputs.select {|output| output[:hidden] != true} end end |
#report(string = nil) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/ruby_reportable/report.rb', line 51 def report(string = nil) if string.nil? @report else @report = string end end |
#run(options = {}) ⇒ Object
237 238 239 240 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 |
# File 'lib/ruby_reportable/report.rb', line 237 def run( = {}) = {:input => {}}.merge() # initial sandbox sandbox = benchmark(:sandbox) do _source() end # apply filters to source filtered_sandbox = benchmark(:filters) do _data(sandbox, ) end # finalize raw data from source source_data = benchmark(:finalize) do _finalize(filtered_sandbox, ).source end # {:default => [{outputs => values}] data = benchmark(:output) do _output(source_data, ) 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([:sort], data, ) end # transform into {group => {group => [outputs => values]}} # level of grouping depends on how many groups are passed in benchmark(:group) do _group([:group], sorted, ) end end |
#source(&block) ⇒ Object
83 84 85 86 |
# File 'lib/ruby_reportable/report.rb', line 83 def source(&block) @data_source = RubyReportable::Source.new @data_source.instance_eval(&block) end |
#useable_filters(scope) ⇒ Object
113 114 115 |
# File 'lib/ruby_reportable/report.rb', line 113 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
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/ruby_reportable/report.rb', line 275 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 or the error is forced. if !sandbox[:input].to_s.blank? || filter_validity[:force_error] == true 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 |