Class: Plist4r::Script
Overview
A module which is an instance of the Script class encapsulates in its scope the top-level methods, top-level constants, and instance variables defined in a ruby script file (and its subfiles) loaded by a ruby program. This allows use of script files to define objects that can be loaded into a program in much the same way that objects can be loaded from YAML or Marshal files.
See intro.txt for an overview.
Usable under the Ruby license. Copyright ©2004 Joel VanderWerf. Questions to [email protected].
Defined Under Namespace
Modules: ScriptModuleMethods Classes: MissingFile
Instance Attribute Summary collapse
-
#__dir ⇒ Object
readonly
The directory in which main_file is located, and relative to which #load searches for files before falling back to Kernel#load.
-
#__loaded_features ⇒ Object
readonly
A hash that maps
filename=>truefor each file that has been required locally by the script. -
#__main_file ⇒ Object
readonly
The file with which the Script was instantiated.
Instance Method Summary collapse
-
#initialize(main_file) {|_self| ... } ⇒ Script
constructor
Creates new Script, and loads main_file in the scope of the Script.
-
#load(file, wrap = false) ⇒ Object
Loads file into this Script.
-
#load_in_module(__file__) ⇒ Object
Loads file in this module’s context.
-
#require(feature) ⇒ Object
Analogous to
Kernel#require. -
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(main_file) {|_self| ... } ⇒ Script
Creates new Script, and loads main_file in the scope of the Script. If a block is given, the script is passed to it before loading from the file, and constants can be defined as inputs to the script.
33 34 35 36 37 38 39 40 41 |
# File 'lib/plist4r/mixin/script.rb', line 33 def initialize(main_file) # :yields: self extend ScriptModuleMethods @__main_file = File.(main_file) @__dir = File.dirname(@__main_file) @__loaded_features = {} yield self if block_given? load_in_module(@__main_file) end |
Instance Attribute Details
#__dir ⇒ Object (readonly)
The directory in which main_file is located, and relative to which #load searches for files before falling back to Kernel#load.
18 19 20 |
# File 'lib/plist4r/mixin/script.rb', line 18 def __dir @__dir end |
#__loaded_features ⇒ Object (readonly)
A hash that maps filename=>true for each file that has been required locally by the script. This has the same semantics as $", alias $LOADED_FEATURES, except that it is local to this script.
23 24 25 |
# File 'lib/plist4r/mixin/script.rb', line 23 def __loaded_features @__loaded_features end |
#__main_file ⇒ Object (readonly)
The file with which the Script was instantiated.
14 15 16 |
# File 'lib/plist4r/mixin/script.rb', line 14 def __main_file @__main_file end |
Instance Method Details
#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.
57 58 59 60 61 62 |
# File 'lib/plist4r/mixin/script.rb', line 57 def load(file, wrap = false) load_in_module(file) true rescue MissingFile super 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.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/plist4r/mixin/script.rb', line 92 def load_in_module(__file__) __file__ = File.(__file__, @__dir) module_eval("@__script_scope ||= binding\n" + IO.read(__file__), __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__}$/ =~ $!. # No extra locals in this scope. raise MissingFile, $!. else raise end 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.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/plist4r/mixin/script.rb', line 73 def require(feature) unless @__loaded_features[feature] @__loaded_features[feature] = true file = feature file += ".rb" unless /\.rb$/ =~ file load_in_module(file) end rescue MissingFile @__loaded_features[feature] = false super end |
#to_s ⇒ Object
:nodoc:
106 107 108 |
# File 'lib/plist4r/mixin/script.rb', line 106 def to_s # :nodoc: "#<#{self.class}:#{File.join(__dir, File.basename(__main_file))}>" end |