Sha256: a52e519561491bfecaba18c0d3f9992efe1d79ef9b3dc4eec86de9b692c5bd54

Contents?: true

Size: 1.53 KB

Versions: 11

Compression:

Stored size: 1.53 KB

Contents

module Martyr
  module Runtime
    class BaseFactScope
      attr_reader :fact_definition, :scope
      delegate :name, :supports_metric?, :supports_dimension_level?, :dimensions, to: :fact_definition

      # = Scope accessors

      # @param fact_definition [Schema::MainFactDefinition]
      def initialize(fact_definition)
        @fact_definition = fact_definition
        @scope = fact_definition.scope
      end

      def run_scope
        @_run_scope ||= scope.call
      end

      def scope_sql
        run_scope.try(:to_sql)
      end

      def null?
        @scope.is_a?(NullScope)
      end

      def set_null_scope
        @scope = NullScope.new
      end

      # @return [String] how to add a where condition to the level
      def level_key_for_where(level_id)
        fact_definition.find_level_association(level_id).fact_key
      end

      # = Scope support check

      # = Scope updater

      # Decorator pattern. The block must return a new scope.
      # @example
      #   Let @scope be: -> { Invoice.all }
      #
      #   decorate_scope {|scope| scope.where(id: 5)}
      #
      #   @scope is now: -> { ->{ Invoice.all }.call.where(id: 5) }
      #
      #   decorate_scope {|scope| scope.where.not(id: 7)}
      #
      #   @scope is now: -> { -> { ->{ Invoice.all }.call.where(id: 5) }.call.where.not(id: 7) }
      #
      def decorate_scope(&block)
        return if null?
        original_scope = self.scope
        @scope = Proc.new do
          block.call(original_scope.call, self)
        end
      end

    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
martyr-0.1.85.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.84.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.82.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.81.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.80.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.79.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.78.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.77.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.76.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.75.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb
martyr-0.1.74.pre lib/martyr/runtime/fact_scopes/base_fact_scope.rb