Sha256: 7a2f12cd8160d3023c46550da77dbe119062e2c4cbbd49083ab2e43fd3272c48

Contents?: true

Size: 1.4 KB

Versions: 16

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true
module Sapience
  module Extensions
    module ActiveRecord
      module ModelMetrics
        def self.included(base) # rubocop:disable AbcSize
          base.extend(ClassMethods)
          base.class_eval do
            const_set(:SAPIENCE_MODEL_CREATE_METRICS_KEY,  "model.#{tableized_name}.create")
            const_set(:SAPIENCE_MODEL_UPDATE_METRICS_KEY,  "model.#{tableized_name}.update")
            const_set(:SAPIENCE_MODEL_DESTROY_METRICS_KEY, "model.#{tableized_name}.destroy")

            if respond_to?(:before_create)
              before_create do
                Sapience.metrics.increment(self.class.const_get(:SAPIENCE_MODEL_CREATE_METRICS_KEY))
              end
            end

            if respond_to?(:before_update)
              before_update do
                Sapience.metrics.increment(self.class.const_get(:SAPIENCE_MODEL_UPDATE_METRICS_KEY))
              end
            end

            if respond_to?(:before_destroy)
              before_destroy do
                Sapience.metrics.increment(self.class.const_get(:SAPIENCE_MODEL_DESTROY_METRICS_KEY))
              end
            end
          end
        end

        module ClassMethods
          def tableized_name
            @tableized_name ||= name.tableize.singularize.tr("/", ".")
          end
        end

        def tableized_name
          self.class.tableized_name
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
sapience-3.0 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.15 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.14 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.13 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.12 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.11 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.10 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.9.1 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.8 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.7.0 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.6.1 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.6.0 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.5.4 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.5.3 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.5.2 lib/sapience/extensions/active_record/model_metrics.rb
sapience-2.5.0 lib/sapience/extensions/active_record/model_metrics.rb