Module: WrapInModule::LoadInModuleMethods

Defined in:
lib/wrap_in_module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__script_scopeObject (readonly)

Returns the value of attribute __script_scope.



67
68
69
# File 'lib/wrap_in_module.rb', line 67

def __script_scope
  @__script_scope
end

Instance Method Details

#__local_variable_get(name) ⇒ Object

Gets value of local var in the script. Does not see local vars in files loaded or required by that script.



77
78
79
# File 'lib/wrap_in_module.rb', line 77

def __local_variable_get(name)
  eval(name.to_s, __script_scope)
end

#__local_variablesObject

Gets list of local vars in the script. Does not see local vars in files loaded or required by that script.



71
72
73
# File 'lib/wrap_in_module.rb', line 71

def __local_variables
  eval("local_variables", __script_scope)
end

#load(file, wrap = false) ⇒ Object

Loads file into this Script. Searches relative to the local dir, that is, the dir of the file given in the original call to Script.load(file), loads the file, if found, into this Script’s scope, and returns true. If the file is not found, falls back to Kernel.load, which searches on $LOAD_PATH, loads the file, if found, into global scope, and returns true. Otherwise, raises LoadError.

The wrap argument is passed to Kernel.load in the fallback case, when the file is not found locally.

Typically called from within the main file to load additional sub files, or from those sub files.



96
97
98
99
100
101
102
103
# File 'lib/wrap_in_module.rb', line 96

def load(file, wrap = false)
  begin
    load_in_module(File.join(@__dir, file))
    true
  rescue ::WrapInModule::MissingFile
    super
  end
end

#load_in_module(__file__) ⇒ Object

Loads file in this module’s context. Note that _\FILE\_ and _\LINE\_ work correctly in file. Called by #load and #require; not normally called directly.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/wrap_in_module.rb', line 46

def load_in_module(__file__)
  module_eval("@__script_scope ||= binding\n" + IO.read(__file__),
    File.expand_path(__file__), 0)
    # start numbering at 0 because of the extra line.
    # The extra line does nothing in sub-script files.
rescue Errno::ENOENT
  if /#{__file__}$/ =~ $!.message # No extra locals in this scope.
    # raise MissingFile, $!.message
    raise ::WrapInModule::MissingFile, $!.message
  else
    raise
  end
end

#method_added(name) ⇒ Object

This is so that def meth... behaves like in Ruby’s top-level context. The implementation simply calls Module#module_function(name).



63
64
65
# File 'lib/wrap_in_module.rb', line 63

def method_added(name) # :nodoc:
  module_function(name)
end

#require(feature) ⇒ Object

Analogous to Kernel#require. First tries the local dir, then falls back to Kernel#require. Will load a given feature only once.

Note that extensions (*.so, *.dll) can be required in the global scope, as usual, but not in the local scope. (This is not much of a limitation in practice–you wouldn’t want to load an extension more than once.) This implementation falls back to Kernel#require when the argument is an extension or is not found locally.



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wrap_in_module.rb', line 114

def require(feature)
  begin
    unless @__loaded_features[feature]
      @__loaded_features[feature] = true
      file = File.join(@__dir, feature)
      file += ".rb" unless /\.rb$/ =~ file
      load_in_module(file)
    end
  rescue ::WrapInModule::MissingFile
    @__loaded_features[feature] = false
    super
  end
end