Sha256: 7bc0ce62e03d8549a9d6eb9b9f1b295d94b0076d64f9c895ed956288fe60d2fc

Contents?: true

Size: 1.27 KB

Versions: 5

Compression:

Stored size: 1.27 KB

Contents

module Dossier
  class Report
    include ActiveSupport::Callbacks
    extend ActiveModel::Naming

    define_callbacks :build_query, :execute

    attr_reader :options

    def initialize(options = {})
      @options = options.dup.with_indifferent_access
    end

    def sql
      raise NotImplementedError, "`sql` method must be defined by each report"
    end

    def query
      build_query unless defined?(@query)
      @query.to_s
    end

    def results
      execute unless defined?(@results)
      @results
    end

    def raw_results
      execute unless defined?(@raw_results)
      @raw_results
    end

    def run
      tap { execute }
    end

    def view
      self.class.name.sub(/Report\Z/, '').underscore
    end

    def formatter
      Dossier::Formatter
    end

    def dossier_client
      Dossier.client
    end

    private

    def build_query
      run_callbacks(:build_query) { @query = Dossier::Query.new(self) }
    end

    def execute
      build_query
      run_callbacks :execute do
        self.results = dossier_client.execute(query, self.class.name)
      end
    end

    def results=(results)
      results.freeze
      @raw_results = Result::Unformatted.new(results, self)
      @results     = Result::Formatted.new(results, self)
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dossier-2.2.0 lib/dossier/report.rb
dossier-2.1.1 lib/dossier/report.rb
dossier-2.1.0 lib/dossier/report.rb
dossier-2.0.1 lib/dossier/report.rb
dossier-2.0.0 lib/dossier/report.rb