Sha256: e3e6d1d849b8447e8af998c8ea6c8b25e80999d629d7540fff2263aebb0eb9d4

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'active_record/migration'

module Ridgepole
  module MigrationExt
    def self.included(klass)
      klass.class_eval do
        cattr_accessor :time_recorder
        cattr_accessor :disable_logging
      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

    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

    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
    include Ridgepole::MigrationExt
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ridgepole-0.6.4.beta6 lib/ridgepole/migration_ext.rb