Module: Modulation::ModuleMixin

Defined in:
lib/modulation/module_mixin.rb

Overview

Extension methods for loaded modules

Constant Summary collapse

EXPORT_DEFAULT_ERROR_MSG =
<<~MSG
  Cannot mix calls to export_from_receiver and export_default in same module
MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__export_default_infoObject (readonly)

Returns the value of attribute __export_default_info.



8
9
10
# File 'lib/modulation/module_mixin.rb', line 8

def __export_default_info
  @__export_default_info
end

#__module_infoObject

read and write module information



7
8
9
# File 'lib/modulation/module_mixin.rb', line 7

def __module_info
  @__module_info
end

Instance Method Details

#__add_dependency(mod) ⇒ Object



110
111
112
# File 'lib/modulation/module_mixin.rb', line 110

def __add_dependency(mod)
  __dependencies << mod unless __dependencies.include?(mod)
end

#__add_dependent_module(mod) ⇒ Object



127
128
129
# File 'lib/modulation/module_mixin.rb', line 127

def __add_dependent_module(mod)
  __dependent_modules << mod unless __dependent_modules.include?(mod)
end

#__before_reloadObject



10
11
12
13
14
# File 'lib/modulation/module_mixin.rb', line 10

def __before_reload
  @__module_info[:exported_symbols] = []
  @__export_directives = nil
  __reset_dependencies
end

#__dependenciesObject



106
107
108
# File 'lib/modulation/module_mixin.rb', line 106

def __dependencies
  @__dependencies ||= []
end

#__dependent_modulesObject



123
124
125
# File 'lib/modulation/module_mixin.rb', line 123

def __dependent_modules
  @__dependent_modules ||= []
end

#__export_directivesObject



16
17
18
# File 'lib/modulation/module_mixin.rb', line 16

def __export_directives
  @__export_directives || []
end

#__exported_symbolsObject



20
21
22
# File 'lib/modulation/module_mixin.rb', line 20

def __exported_symbols
  __module_info[:exported_symbols]
end

#__expose!Module

Exposes all private methods and private constants as public

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/modulation/module_mixin.rb', line 92

def __expose!
  singleton = singleton_class

  singleton.private_instance_methods.each do |sym|
    singleton.send(:public, sym)
  end

  __module_info[:private_constants].each do |sym|
    const_set(sym, singleton.const_get(sym))
  end

  self
end

#__reload!Module

Reload module

Returns:



80
81
82
# File 'lib/modulation/module_mixin.rb', line 80

def __reload!
  Modulation.reload(self)
end

#__reset_dependenciesObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/modulation/module_mixin.rb', line 131

def __reset_dependencies
  return unless @__dependencies

  @__dependencies.each do |mod|
    next unless mod.respond_to?(:__dependent_modules)

    mod.__dependent_modules.delete(self)
  end
  @__dependencies.clear
end

#__traverse_dependencies(&block) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/modulation/module_mixin.rb', line 114

def __traverse_dependencies(&block)
  __dependencies.each do |mod|
    block.(mod)
    if mod.respond_to?(:__traverse_dependencies)
      mod.__traverse_dependencies(&block)
    end
  end
end

#export(*symbols) ⇒ void

This method returns an undefined value.

Adds given symbols to the exported_symbols array

Parameters:

  • symbols (Array)

    array of symbols



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/modulation/module_mixin.rb', line 27

def export(*symbols)
  if @__export_default_info
    raise 'Cannot mix calls to export and export_default in same module'
  end

  @__export_directives ||= []
  @__export_directives << {
    method:        :export,
    args:          symbols,
    export_caller: caller
  }
end

#export_default(value) ⇒ void

This method returns an undefined value.

Sets a module’s value, so when imported it will represent the given value, instead of a module facade

Parameters:

  • value (Symbol, any)

    symbol or value



59
60
61
62
63
64
65
# File 'lib/modulation/module_mixin.rb', line 59

def export_default(value)
  unless __export_directives.empty?
    raise 'Cannot mix calls to export and export_default in the same module'
  end

  @__export_default_info = { value: value, caller: caller }
end

#export_from_receiver(name) ⇒ Object



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

def export_from_receiver(name)
  raise EXPORT_DEFAULT_ERROR_MSG if @__export_default_info

  @__export_directives ||= []
  @__export_directives << {
    method:        :export_from_receiver,
    args:          name,
    export_caller: caller
  }
end

#inspectString

Returns a text representation of the module for inspection

Returns:

  • (String)

    module string representation



69
70
71
72
73
74
75
76
# File 'lib/modulation/module_mixin.rb', line 69

def inspect
  module_name = name || 'Module'
  if __module_info[:location]
    "#{module_name}:#{__module_info[:location]}"
  else
    module_name
  end
end