Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/modulation/ext.rb

Overview

Module extensions

Instance Method Summary collapse

Instance Method Details

#alias_method_once(new_name, old_name) ⇒ Module

Aliases the given method only if the alias does not exist, implementing in effect idempotent method aliasing

Parameters:

  • new_name (Symbol)

    alias name

  • old_name (Symbol)

    original name

Returns:



87
88
89
90
91
# File 'lib/modulation/ext.rb', line 87

def alias_method_once(new_name, old_name)
  return self if method_defined?(new_name)

  alias_method(new_name, old_name)
end

#auto_import(sym, path = nil, caller_location = caller(CALLER_RANGE).first) ⇒ void

This method returns an undefined value.

Registers a constant to be lazy-loaded upon lookup

Parameters:

  • sym (Symbol, Hash)

    constant name or hash mapping names to paths

  • path (String) (defaults to: nil)

    path if sym is Symbol



45
46
47
48
49
50
51
52
# File 'lib/modulation/ext.rb', line 45

def auto_import(sym, path = nil, caller_location = caller(CALLER_RANGE).first)
  setup_auto_import_registry unless @__auto_import_registry
  if path
    @__auto_import_registry[sym] = [path, caller_location]
  else
    sym.each { |k, v| @__auto_import_registry[k] = [v, caller_location] }
  end
end

#extend_from(path) ⇒ void

This method returns an undefined value.

Extends the receiver with exported methods from the given file name

Parameters:

  • path (String)

    module filename



65
66
67
68
69
# File 'lib/modulation/ext.rb', line 65

def extend_from(path)
  mod = import(path, caller(CALLER_RANGE).first)
  Modulation::Builder.add_module_methods(mod, self.singleton_class)
  Modulation::Builder.add_module_constants(mod, self)
end

#include_from(path, *symbols) ⇒ void

This method returns an undefined value.

Includes exported methods from the given file name in the receiver The module’s methods will be available as instance methods

Parameters:

  • path (String)

    module filename

  • symbols (Array<Symbol>)

    list of symbols to include



76
77
78
79
80
# File 'lib/modulation/ext.rb', line 76

def include_from(path, *symbols)
  mod = import(path, caller(CALLER_RANGE).first)
  Modulation::Builder.add_module_methods(mod, self, *symbols)
  Modulation::Builder.add_module_constants(mod, self, *symbols)
end

#setup_auto_import_registryObject



54
55
56
57
58
59
60
# File 'lib/modulation/ext.rb', line 54

def setup_auto_import_registry
  @__auto_import_registry = {}
  Modulation::Builder.define_auto_import_const_missing_method(
    self,
    @__auto_import_registry
  )
end