refactored ReportCache.prepare_result - reportable - Fork of reportable required by WarVox, from hdm/reportable.
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit ef0e7538ed36be1760669e5dad94482ef4789418
 (DIR) parent b3c5f1a04b24429fe96a1b2eeb1dadc63214574d
 (HTM) Author: Marco Otte-Witte <marco.otte-witte@simplabs.com>
       Date:   Mon, 19 Jan 2009 13:21:56 +0100
       
       refactored ReportCache.prepare_result
       
       Diffstat:
         M lib/kvlr/reports_as_sparkline/repo… |      10 +++++-----
         M spec/classes/report_cache_spec.rb   |      36 ++++++++++++++++----------------
       
       2 files changed, 23 insertions(+), 23 deletions(-)
       ---
 (DIR) diff --git a/lib/kvlr/reports_as_sparkline/report_cache.rb b/lib/kvlr/reports_as_sparkline/report_cache.rb
       @@ -14,19 +14,19 @@ module Kvlr #:nodoc:
                    last_reporting_period_to_read = ReportingPeriod.new(options[:grouping], cached_data.last.reporting_period).next unless cached_data.empty?
                  end
                  new_data = yield(last_reporting_period_to_read.date_time)
       -          prepare_result(new_data, cached_data, last_reporting_period_to_read, report, options[:grouping], cache)[0..(options[:limit] - 1)]
       +          prepare_result(new_data, cached_data, last_reporting_period_to_read, report, options, cache)[0..(options[:limit] - 1)]
                end
              end
        
              private
        
       -        def self.prepare_result(new_data, cached_data, last_reporting_period_to_read, report, grouping, cache = true)
       -          new_data.map! { |data| [ReportingPeriod.from_db_string(grouping, data[0]), data[1]] }
       +        def self.prepare_result(new_data, cached_data, last_reporting_period_to_read, report, options, cache = true)
       +          new_data.map! { |data| [ReportingPeriod.from_db_string(options[:grouping], data[0]), data[1]] }
                  result = cached_data.map { |cached| [cached.reporting_period, cached.value] }
       -          current_reporting_period = ReportingPeriod.new(grouping)
       +          current_reporting_period = ReportingPeriod.new(options[:grouping])
                  reporting_period = last_reporting_period_to_read
                  while reporting_period < current_reporting_period
       -            cached = build_cached_data(report, grouping, reporting_period, find_value(new_data, reporting_period))
       +            cached = build_cached_data(report, options[:grouping], reporting_period, find_value(new_data, reporting_period))
                    cached.save! if cache
                    result << [reporting_period.date_time, cached.value]
                    reporting_period = reporting_period.next
 (DIR) diff --git a/spec/classes/report_cache_spec.rb b/spec/classes/report_cache_spec.rb
       @@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), '..', 'spec_helper')
        describe Kvlr::ReportsAsSparkline::ReportCache do
        
          before do
       -    @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations)
       +    @report = Kvlr::ReportsAsSparkline::Report.new(User, :registrations, :limit => 10)
          end
        
          describe '.process' do
       @@ -15,19 +15,19 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
        
            it 'should raise an ArgumentError if no block is given' do
              lambda do
       -        Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] })
       +        Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options)
              end.should raise_error(ArgumentError)
            end
        
            it 'sould start a transaction' do
              Kvlr::ReportsAsSparkline::ReportCache.should_receive(:transaction)
        
       -      Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }) {}
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) {}
            end
        
            it 'should yield to the given block' do
              lambda {
       -        Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }) { raise YieldMatchException.new }
       +        Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) { raise YieldMatchException.new }
              }.should raise_error(YieldMatchException)
            end
        
       @@ -46,7 +46,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
                :order => 'reporting_period ASC'
              )
        
       -      Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }) { [] }
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) { [] }
            end
        
            it "should read existing data from the cache for the correct grouping if one other than the report's default grouping is specified" do
       @@ -73,13 +73,13 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              cached_data = []
              Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return(cached_data)
              last_reporting_period_to_read = Kvlr::ReportsAsSparkline::ReportingPeriod.first(@report.options[:grouping], 10)
       -      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:prepare_result).once.with(new_data, cached_data, last_reporting_period_to_read, @report, @report.options[:grouping], true)
       +      Kvlr::ReportsAsSparkline::ReportCache.should_receive(:prepare_result).once.with(new_data, cached_data, last_reporting_period_to_read, @report, @report.options, true)
        
       -      Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }) { new_data }
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) { new_data }
            end
        
            it 'should yield the first reporting period if the cache is empty' do
       -      Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }) do |begin_at|
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) do |begin_at|
                begin_at.should == Kvlr::ReportsAsSparkline::ReportingPeriod.first(@report.options[:grouping], 10).date_time
                []
              end
       @@ -97,7 +97,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              })
              Kvlr::ReportsAsSparkline::ReportCache.stub!(:find).and_return([cached])
        
       -      Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }) do |begin_at|
       +      Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options) do |begin_at|
                begin_at.should == reporting_period.next.date_time
                []
              end
       @@ -108,11 +108,11 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              it 'should not read any data from cache' do
                Kvlr::ReportsAsSparkline::ReportCache.should_not_receive(:find)
        
       -        Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }, false) {}
       +        Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options, false) {}
              end
        
              it 'should yield the first reporting period' do
       -        Kvlr::ReportsAsSparkline::ReportCache.process(@report, { :limit => 10, :grouping => @report.options[:grouping] }, false) do |begin_at|
       +        Kvlr::ReportsAsSparkline::ReportCache.process(@report, @report.options, false) do |begin_at|
                  begin_at.should == Kvlr::ReportsAsSparkline::ReportingPeriod.first(@report.options[:grouping], 10).date_time
                  []
                end
       @@ -136,7 +136,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
            it 'should convert the date strings from the newly read data to reporting periods' do
              Kvlr::ReportsAsSparkline::ReportingPeriod.should_receive(:from_db_string).once.with(@report.options[:grouping], @new_data[0][0]).and_return(Kvlr::ReportsAsSparkline::ReportingPeriod.new(@report.options[:grouping]))
        
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options[:grouping])
       +      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options)
            end
        
            it 'should create (:limit - 1) instances of Kvlr::ReportsAsSparkline::ReportCache with value 0.0 if no new data has been read and nothing was cached' do
       @@ -147,7 +147,7 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
                0.0
              ).and_return(@cached)
        
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], @last_reporting_period_to_read, @report, @report.options[:grouping])
       +      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, [], [], @last_reporting_period_to_read, @report, @report.options)
            end
        
            it 'should create a new Kvlr::ReportsAsSparkline::ReportCache with the correct value if new data has been read' do
       @@ -164,17 +164,17 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
                1.0
              ).and_return(@cached)
        
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options[:grouping])
       +      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options)
            end
        
            it 'should save the created Kvlr::ReportsAsSparkline::ReportCache if no_cache is not specified' do
              @cached.should_receive(:save!).once
        
       -      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options[:grouping])
       +      Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options)
            end
        
            it 'should return an array of arrays of Dates and Floats' do
       -      result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options[:grouping], true)
       +      result = Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options, true)
        
              result.should be_kind_of(Array)
              result[0].should be_kind_of(Array)
       @@ -187,14 +187,14 @@ describe Kvlr::ReportsAsSparkline::ReportCache do
              it 'should not save the created Kvlr::ReportsAsSparkline::ReportCache' do
                @cached.should_not_receive(:save!)
        
       -        Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options[:grouping], false)
       +        Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [], @last_reporting_period_to_read, @report, @report.options, false)
              end
        
              it 'should not update the last cached record if new data has been read for the last reporting period to read' do
                Kvlr::ReportsAsSparkline::ReportingPeriod.stub!(:from_db_string).and_return(@last_reporting_period_to_read)
                @cached.should_not_receive(:update_attributes!)
        
       -        Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report, @report.options[:grouping], false)
       +        Kvlr::ReportsAsSparkline::ReportCache.send(:prepare_result, @new_data, [@cached], @last_reporting_period_to_read, @report, @report.options, false)
              end
        
            end