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, 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
  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

#current_contextObject (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_modeObject

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

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



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.meta_eval do
    include mod
  end
end

#exec_strict_mode?Boolean

Returns:

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