Sha256: c5c76652215db32d41a4634444b9ba323256cd4b8f5e393ccd8290bc9e973f01

Contents?: true

Size: 1.26 KB

Versions: 57

Compression:

Stored size: 1.26 KB

Contents

require 'active_record/migration'

module Ridgepole
  module MigrationExt
    def self.prepended(klass)
      klass.class_eval do
        cattr_accessor :time_recorder
        cattr_accessor :disable_logging

        def self.record_time
          result = nil

          begin
            self.time_recorder = TimeRecorder.new
            yield
            result = self.time_recorder.result
          ensure
            self.time_recorder = nil
          end

          result
        end
      end
    end

    def write(text = '')
      logger = Ridgepole::Logger.instance
      logger.info(text) unless self.disable_logging
      parse_text(text)
    end

    def parse_text(text)
      return unless self.time_recorder

      case text
      when /\A--\s+(.+)\Z/
        self.time_recorder.add_key($1)
      when /\A\s+->\s+(\d+\.\d+)s\Z/
        self.time_recorder.add_value($1.to_f)
      end
    end

    class TimeRecorder
      attr_reader :result

      def initialize
        @result = {}
      end

      def add_key(key)
        @key = key
      end

      def add_value(value)
        if @key
          @result[@key] = value
        end

        @key = nil
      end
    end
  end
end

module ActiveRecord
  class Migration
    prepend Ridgepole::MigrationExt
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
ridgepole-0.7.3.beta lib/ridgepole/migration_ext.rb
ridgepole-0.7.2 lib/ridgepole/migration_ext.rb
ridgepole-0.7.2.beta lib/ridgepole/migration_ext.rb
ridgepole-0.7.1 lib/ridgepole/migration_ext.rb
ridgepole-0.7.1.beta5 lib/ridgepole/migration_ext.rb
ridgepole-0.7.1.beta4 lib/ridgepole/migration_ext.rb
ridgepole-0.7.1.beta3 lib/ridgepole/migration_ext.rb
ridgepole-0.7.1.beta2 lib/ridgepole/migration_ext.rb
ridgepole-0.7.1.beta lib/ridgepole/migration_ext.rb
ridgepole-0.7.0 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.rc1 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta22 lib/ridgepole/migration_ext.rb
ridgepole-0.6.6 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta21 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta20 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta19 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta18 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta17 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta16 lib/ridgepole/migration_ext.rb
ridgepole-0.7.0.beta15 lib/ridgepole/migration_ext.rb