Class: DSLCompanion::Interpreter

Inherits:
Object
  • Object
show all
Includes:
Features::Basic
Defined in:
lib/dsl_companion/interpreter.rb

Constant Summary collapse

DEFAULT_EXEC_MODE =
:lazy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
  message = "DSL Interpreter: method '#{method_name}'"
  message += block_given? ? ' (with a code block passed).' : ' (with no code block passed)'
  message += ' is unknown'
  message += " within DSL file: '#{@source_code_file}'" unless @source_code_file.nil?
  message += '.'
  logger message, :error unless exec_strict_mode?
  raise message if exec_strict_mode?
end

Instance Attribute Details

#exec_modeObject

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

Parameters:

  • value

    the value to set the attribute logger to.



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

Returns:

  • (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