Class: Loom::Mods::ModLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/loom/mods/mod_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loom_config) ⇒ ModLoader

Returns a new instance of ModLoader.



9
10
11
# File 'lib/loom/mods/mod_loader.rb', line 9

def initialize(loom_config)
  @loom_config = loom_config
end

Class Method Details

.define_mod_factory(name, mod_klass) ⇒ Object

TODO: add some documentation here, this is the entrypoint for all mod factories and returning the ActionProxy or running a ModBlock. This is just as hidden as ActionProxy+install_root_actions+

Raises:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/loom/mods/mod_loader.rb', line 53

def define_mod_factory(name, mod_klass)
  raise ModDefinedError, name if instance_methods.include? name
  registered_mods[mod_klass.name] = [name]

  define_method name do |shell, *args, **kwargs, &pattern_block|
    Loom.log.debug3(self) do
      "handling mod call => #{mod_klass}##{name} #{args} #{kwargs} #{pattern_block}"
    end
    load_mod_klass mod_klass, shell

    mod = mod_klass.new shell, @loom_config
    mod.execute *args, **kwargs, &pattern_block
  end
end

.register_mod(klass, name, **opts) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/loom/mods/mod_loader.rb', line 32

def register_mod(klass, name, **opts)
  name = name.to_sym
  raise AnonymousModLoadError, 'cannot load anonymous mods' unless name
  raise ModDefinedError, name if instance_methods.include? name

  define_mod_factory name, klass
  Loom.log.debug(self) { "registered mod => #{klass} as #{name}" }

  opts.each do |k,v|
    case k
    when :alias
      [v].flatten.each { |v| alias_module klass, v }
    else
      raise "unknown option #{k}"
    end
  end
end

.registered_modsObject



68
69
70
# File 'lib/loom/mods/mod_loader.rb', line 68

def registered_mods
  @registered_mods ||= {}
end

Instance Method Details

#load_mod_klass(mod_klass, shell) ⇒ Object



13
14
15
# File 'lib/loom/mods/mod_loader.rb', line 13

def load_mod_klass(mod_klass, shell)
  verify_shell_cmds mod_klass, shell
end