Module: Modulation::ExportDefault

Defined in:
lib/modulation/export_default.rb

Overview

default export functionality

Constant Summary collapse

DEFAULT_VALUE_ERROR_MSG =

Error message to be displayed when trying to set a singleton value as default export

'Default export cannot be boolean, numeric, or symbol'

Class Method Summary collapse

Class Method Details

.get_module_constant(mod, value) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/modulation/export_default.rb', line 24

def get_module_constant(mod, value)
  unless mod.singleton_class.constants(true).include?(value)
    Exports.raise_exported_symbol_not_found_error(value, :const)
  end

  mod.singleton_class.const_get(value)
end

.get_module_method(mod, value) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/modulation/export_default.rb', line 32

def get_module_method(mod, value)
  unless mod.singleton_class.instance_methods(true).include?(value)
    Exports.raise_exported_symbol_not_found_error(value, :method)
  end

  proc { |*args, &block| mod.send(value, *args, &block) }
end

.set_module_default_value(value, info, mod, caller) ⇒ any

Sets the default value for a module using export_default

Parameters:

  • value (any)

    default value

  • info (Hash)

    module info

  • mod (Module)

    module

Returns:

  • (any)

    default value



50
51
52
53
54
55
56
57
58
# File 'lib/modulation/export_default.rb', line 50

def set_module_default_value(value, info, mod, caller)
  value = transform_export_default_value(value, mod)
  case value
  when nil, true, false, Numeric, Symbol
    raise(TypeError, DEFAULT_VALUE_ERROR_MSG, caller)
  end
  set_reload_info(value, mod.__module_info)
  Modulation.loaded_modules[info[:location]] = value
end

.set_reload_info(value, info) ⇒ void

This method returns an undefined value.

Adds methods for module_info and reloading to a value exported as default

Parameters:

  • value (any)

    export_default value

  • info (Hash)

    module info



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

def set_reload_info(value, info)
  value.define_singleton_method(:__module_info) { info }
  value.define_singleton_method(:__reload!) do
    Modulation::Builder.make(info)
  end
end

.transform_export_default_value(value, mod) ⇒ any

Returns exported value for a default export If the given value is a symbol, returns the value of the corresponding constant. If the symbol refers to a method, returns a proc enveloping the method. Raises if symbol refers to non-existent constant or method.

Parameters:

  • value (any)

    export_default value

  • mod (Module)

    module

Returns:

  • (any)

    exported value



14
15
16
17
18
19
20
21
22
# File 'lib/modulation/export_default.rb', line 14

def transform_export_default_value(value, mod)
  return value unless value.is_a?(Symbol)

  if value =~ Modulation::RE_CONST
    get_module_constant(mod, value)
  else
    get_module_method(mod, value)
  end
end