Class: DSLCompanion::Interpreter
- Inherits:
-
Object
- Object
- DSLCompanion::Interpreter
- Includes:
- Features::Basic
- Defined in:
- lib/dsl_companion/interpreter.rb
Constant Summary collapse
- DEFAULT_EXEC_MODE =
:lazy
Instance Attribute Summary collapse
-
#current_context ⇒ Object
readonly
Returns the value of attribute current_context.
-
#exec_mode ⇒ Object
Returns the value of attribute exec_mode.
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #add_feature(mod) ⇒ Object
- #exec_strict_mode? ⇒ Boolean
-
#initialize(exec_mode = DEFAULT_EXEC_MODE) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #method_missing(symbolic_name, *args) ⇒ Object
- #run(file = nil, &block) ⇒ Object
Methods included from Features::Basic
#define, #execute_within_context, #interpreter, #interpreter?, #logger
Methods included from MetaHelper
#class_def, #inject_variable, inject_variable, #meta_def, #meta_eval, #metaclass
Constructor Details
#initialize(exec_mode = DEFAULT_EXEC_MODE) ⇒ Interpreter
Returns a new instance of Interpreter.
12 13 14 15 16 |
# File 'lib/dsl_companion/interpreter.rb', line 12 def initialize(exec_mode=DEFAULT_EXEC_MODE) @interpreter = self self.exec_mode = exec_mode @current_context = self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbolic_name, *args) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/dsl_companion/interpreter.rb', line 61 def method_missing(symbolic_name, *args) method_name ||= symbolic_name.to_s = "DSL Interpreter: method '#{method_name}'" += block_given? ? ' (with a code block passed).' : ' (with no code block passed)' += ' is unknown' += " within DSL file: '#{@source_code_file}'" unless @source_code_file.nil? += '.' logger , :error unless exec_strict_mode? raise if exec_strict_mode? end |
Instance Attribute Details
#current_context ⇒ Object (readonly)
Returns the value of attribute current_context.
8 9 10 |
# File 'lib/dsl_companion/interpreter.rb', line 8 def current_context @current_context end |
#exec_mode ⇒ Object
Returns the value of attribute exec_mode.
48 49 50 |
# File 'lib/dsl_companion/interpreter.rb', line 48 def exec_mode @exec_mode end |
#logger=(value) ⇒ Object (writeonly)
Sets the attribute logger
7 8 9 |
# File 'lib/dsl_companion/interpreter.rb', line 7 def logger=(value) @logger = value end |
Class Method Details
.run(file = nil, exec_mode = DEFAULT_EXEC_MODE, &block) ⇒ Object
32 33 34 |
# File 'lib/dsl_companion/interpreter.rb', line 32 def self.run(file=nil, exec_mode=DEFAULT_EXEC_MODE, &block) new(exec_mode).run file, &block end |
Instance Method Details
#add_feature(mod) ⇒ Object
55 56 57 58 59 |
# File 'lib/dsl_companion/interpreter.rb', line 55 def add_feature(mod) self. do include mod end end |
#exec_strict_mode? ⇒ Boolean
51 52 53 |
# File 'lib/dsl_companion/interpreter.rb', line 51 def exec_strict_mode? @exec_mode == :strict end |
#run(file = nil, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/dsl_companion/interpreter.rb', line 19 def run(file=nil, &block) if file.nil? self.instance_eval &block else @source_code_file = file code = File.read file self.instance_eval code end ensure @last_source_code_file = @source_code_file @source_code_file = nil end |