Module: Gin::Reloadable::ClassMethods

Defined in:
lib/gin/reloadable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#clear_constants(root, names = nil) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/gin/reloadable.rb', line 30

def clear_constants root, names=nil
  each_constant(root, names) do |name, const, parent|
    if Module === const || !gin_constants[const.object_id]
      parent.send(:remove_const, name) rescue nil
    end
  end
end

#each_constant(parent, names = nil, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gin/reloadable.rb', line 51

def each_constant parent, names=nil, &block
  names ||= parent.constants
  names.each do |name|
    const = parent.const_get(name)
    next unless const

    if ::Module === const
      next unless parent == ::Object || const.name =~ /(^|::)#{parent.name}::/
      each_constant(const, &block)
    end

    block.call name, const, parent
  end
end

#erase!(files, consts, parent = nil) ⇒ Object



23
24
25
26
27
# File 'lib/gin/reloadable.rb', line 23

def erase! files, consts, parent=nil
  parent ||= ::Object
  files.each{|f| $LOADED_FEATURES.delete f }
  clear_constants parent, consts
end

#erase_dependencies!Object



15
16
17
18
19
20
# File 'lib/gin/reloadable.rb', line 15

def erase_dependencies!
  reloadables.each do |key, (_, files, consts)|
    erase! files, consts
  end
  true
end

#gin_constantsObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/gin/reloadable.rb', line 39

def gin_constants
  return @gin_constants if defined?(@gin_constants) && @gin_constants
  @gin_constants = {Gin.object_id => ::Gin}

  each_constant(Gin) do |name, const, _|
    @gin_constants[const.object_id] = const
  end

  @gin_constants
end

#reloadablesObject



10
11
12
# File 'lib/gin/reloadable.rb', line 10

def reloadables
  @reloadables ||= {}
end

#track_require(file) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gin/reloadable.rb', line 76

def track_require file
  old_consts   = ::Object.constants
  old_features = $LOADED_FEATURES.dup

  filepath = Gin.find_loadpath file

  if !reloadables[filepath]
    success = ::Object.send(:require, file)

  else reloadables[filepath]
    without_warnings{
      success = ::Object.send(:require, file)
    }
  end

  reloadables[filepath] = [
    file,
    $LOADED_FEATURES - old_features,
    ::Object.constants - old_consts
  ] if success

  success
end

#without_warnings(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/gin/reloadable.rb', line 67

def without_warnings &block
  warn_level = $VERBOSE
  $VERBOSE = nil
  yield
ensure
  $VERBOSE = warn_level
end