Class: Plist4r::Script
- Extended by:
- Script::ScriptModuleMethods
- Defined in:
- lib/plist4r/mixin/script.rb
Modules
Classes
Instance Methods
Methods included from Script::ScriptModuleMethods
__local_variable_get, __local_variables, method_added
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 (C)2004 Joel VanderWerf. Questions to vjoel@users.sourceforge.net.
Instance Attributes
-
- (Object) __dir
readonly
The directory in which main_file is located, and relative to which #load searches for files before falling back to Kernel#load.
-
- (Object) __loaded_features
readonly
A hash that maps filename=>true for each file that has been required locally by the script.
-
- (Object) __main_file
readonly
The file with which the Script was instantiated.
Constructor Details
- (Script) initialize(main_file) {|_self| ... }
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 Method Details
- (Object) load(file, wrap = false)
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 |
- (Object) load_in_module(__file__)
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 |
- (Object) require(feature)
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 |
- (Object) to_s
:nodoc:
106 107 108 |
# File 'lib/plist4r/mixin/script.rb', line 106 def to_s "#<#{self.class}:#{File.join(__dir, File.basename(__main_file))}>" end |