class Cardio::Mod::Loader

The mods are given by a Mod::Dirs object. SetLoader can use three different strategies to load the set modules.

Attributes

mod_dirs[R]

Public Class Methods

load_dir(dir) click to toggle source

load all files in directory @param dir [String] directory name

# File lib/cardio/mod/loader.rb, line 37
def load_dir dir
  Dir["#{dir}/*.rb"].sort.each do |file|
    # puts Benchmark.measure("from #load_dir: rd: #{file}") {
    # require file
    # "require" breaks the reloading in development env
    load file
    # }.format('%n: %t %r')
  end
end
load_initializers() click to toggle source
# File lib/cardio/mod/loader.rb, line 29
def load_initializers
  Cardio.config.paths["late/initializers"].existent.sort.each do |init|
    load init
  end
end
load_mods() click to toggle source
# File lib/cardio/mod/loader.rb, line 15
def load_mods
  SetPatternLoader.new.load
  SetLoader.new.load
  Card::Set.process_base_modules
  load_initializers
end
new(load_strategy: nil, mod_dirs: nil) click to toggle source
# File lib/cardio/mod/loader.rb, line 50
def initialize load_strategy: nil, mod_dirs: nil
  load_strategy ||= Cardio.config.load_strategy
  @mod_dirs = mod_dirs || Mod.dirs
  @load_strategy = load_strategy_class(load_strategy).new self
end
reload_sets() click to toggle source
# File lib/cardio/mod/loader.rb, line 22
def reload_sets
  Card::Set::Pattern.reset
  Card::Set.reset
  SetPatternLoader.new.load
  SetLoader.new(no_all: true).load
end

Public Instance Methods

load() click to toggle source
# File lib/cardio/mod/loader.rb, line 56
def load
  @load_strategy.load_modules
end
parts_from_path(path) click to toggle source
# File lib/cardio/mod/loader.rb, line 60
def parts_from_path path
  # remove file extension and number prefixes
  parts = path.gsub(/\.rb/, "").gsub(%r{(?<=\A|/)\d+_}, "").split(File::SEPARATOR)
  parts.map(&:camelize)
end

Private Instance Methods

each_file_in_dir(base_dir, subdir=nil) { |abs_path, const_parts| ... } click to toggle source
# File lib/cardio/mod/loader.rb, line 72
def each_file_in_dir base_dir, subdir=nil
  pattern = File.join(*[base_dir, subdir, "**/*.rb"].compact)
  Dir.glob(pattern).sort.each do |abs_path|
    rel_path = abs_path.sub("#{base_dir}/", "")
    const_parts = parts_from_path rel_path
    yield abs_path, const_parts
  end
end
each_mod_dir(module_type, &block) click to toggle source
# File lib/cardio/mod/loader.rb, line 68
def each_mod_dir module_type, &block
  @mod_dirs.each module_type, &block
end