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
-
#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, #meta_def, #meta_eval, #metaclass
Constructor Details
#initialize(exec_mode = DEFAULT_EXEC_MODE) ⇒ Interpreter
Returns a new instance of Interpreter.
11 12 13 14 |
# File 'lib/dsl_companion/interpreter.rb', line 11 def initialize(exec_mode=DEFAULT_EXEC_MODE) @interpreter = self self.exec_mode = exec_mode end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbolic_name, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/dsl_companion/interpreter.rb', line 58 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
#exec_mode ⇒ Object
Returns the value of attribute exec_mode.
47 48 49 |
# File 'lib/dsl_companion/interpreter.rb', line 47 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
31 32 33 |
# File 'lib/dsl_companion/interpreter.rb', line 31 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
54 55 56 |
# File 'lib/dsl_companion/interpreter.rb', line 54 def add_feature(mod) self.extend(mod) end |
#exec_strict_mode? ⇒ Boolean
50 51 52 |
# File 'lib/dsl_companion/interpreter.rb', line 50 def exec_strict_mode? @exec_mode == :strict end |
#run(file = nil, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dsl_companion/interpreter.rb', line 17 def run file=nil, &block @context = self 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 |