class Ak::HotFile

Attributes

const[R]
descendants[R]
name[R]

Public Class Methods

new(name, **options) click to toggle source
# File lib/ak.rb, line 40
def initialize(name, **options)
  @name        = get_name(name)
  @const       = options[:const]
  @require     = options[:require]
  @descendants = []
end

Public Instance Methods

add_descendant(file) click to toggle source
# File lib/ak.rb, line 47
def add_descendant(file)
  @descendants << file
end
load_file() click to toggle source
# File lib/ak.rb, line 51
def load_file
  require name if require_file?
  set_const unless const
  set_parents if const
end
reload() click to toggle source
# File lib/ak.rb, line 57
def reload
  unload
  require name
  descendants.each &:reload
  true
end
unload() click to toggle source
# File lib/ak.rb, line 64
def unload
  if const
    match = const.to_s.match(/\:\:(\w+)$/)

    if match
      parent = const.to_s.gsub("::#{match[1]}", '')
      Object.const_get(parent).send(:remove_const, match[1])
    else
      Object.send(:remove_const, const.to_s)
    end
  end

  $LOADED_FEATURES.delete(name)
end
unload_and_reload() click to toggle source
# File lib/ak.rb, line 79
def unload_and_reload
  unload and descendants.each(&:reload)
end

Private Instance Methods

get_name(name) click to toggle source
# File lib/ak.rb, line 85
def get_name(name)
  File.expand_path(name, Ak.root)
end
require_file?() click to toggle source
# File lib/ak.rb, line 117
def require_file?
  @require && !$LOADED_FEATURES.include?(name)
end
set_const() click to toggle source
# File lib/ak.rb, line 89
def set_const
  path = name.gsub(/#{Ak.root}\/|\.rb/, '').split('/')
  consts = path.map do |el|
    index = path.find_index { |_el| el == _el }
    Inflector.camelize path[index..(path.count)].join('/')
  end
  valid_const = nil
  
  consts.each do |const|
    valid_const = Object.const_get(const) rescue nil
    break if valid_const
  end

  @const = valid_const
end
set_parents() click to toggle source
# File lib/ak.rb, line 105
def set_parents
  parents = const.ancestors.map do |ancestor|
    unless ancestor == const
      Ak.registry.find_by(:const, ancestor)
    end
  end.compact

  parents.each do |parent|
    parent.add_descendant(self)
  end
end