Sha256: 845c03f7ae098f6400c1f71c3d43ccf8e02a4bd8d45affb52f3301d0b15673a0

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

require_relative '../../../lib/core_ext/object_space'

module Grape
  module Reload
    module Storage
      class << self
        def clear!
          files.each_key do |file|
            remove(file)
            Watcher.remove_feature(file)
          end
          @files = {}
        end

        def remove(name)
          file = files[name] || return
          file[:constants].each{ |constant| Watcher.remove_constant(constant) }
          file[:features].each{ |feature| Watcher.remove_feature(feature) }
          files.delete(name)
        end

        def prepare(name)
          file = remove(name)
          @old_entries ||= {}
          @old_entries[name] = {
              :constants => ObjectSpace.classes,
              :features  => old_features = Set.new($LOADED_FEATURES.dup)
          }
          features = file && file[:features] || []
          features.each{ |feature| Watcher.safe_load(feature, :force => true) unless Watcher.feature_excluded?(feature)}
          Watcher.remove_feature(name) if old_features.include?(name)
        end

        def commit(name)
          entry = {
              :constants => ObjectSpace.new_classes(@old_entries[name][:constants]),
              :features  => Set.new($LOADED_FEATURES) - @old_entries[name][:features] - [name]
          }
          files[name] = entry
          @old_entries.delete(name)
        end

        def rollback(name)
          new_constants = ObjectSpace.new_classes(@old_entries[name][:constants])
          new_constants.each{ |klass| Watcher.remove_constant(klass) }
          @old_entries.delete(name)
        end

        private

        def files
          @files ||= {}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-reload-0.0.2 lib/grape/reload/storage.rb