Module: DSLCompanion::Features::Basic

Includes:
MetaHelper
Included in:
Interpreter
Defined in:
lib/dsl_companion/features/basic.rb

Instance Method Summary collapse

Methods included from MetaHelper

#class_def, #inject_variable, inject_variable, #meta_def, #meta_eval, #metaclass

Instance Method Details

#define(*args, &block) ⇒ Anything returned by the method

If any method named define_<something>(*args) is in the DSL, then it provides an alternate  generic syntax of define(:something, *args)

Parameters:

  • args (Object[])
  • block (Proc)

Returns:

  • (Anything returned by the method)


13
14
15
16
17
18
19
20
21
# File 'lib/dsl_companion/features/basic.rb', line 13

def define(*args, &block)
  extra = args.shift
  method_name = "define_#{extra}"
  if respond_to? method_name.to_sym
    block_given? ? self.send(method_name, *args, &block) : self.send(method_name, *args)
  else
    block_given? ? method_missing(method_name.to_sym, *args, &block) : method_missing(method_name.to_sym, *args)
  end
end

#execute_within_context(context = @context, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dsl_companion/features/basic.rb', line 23

def execute_within_context(context=@context, &block)
  # Execute the block if any
  if block_given?
    last_saved_context = @current_context
    @current_context = context
    begin
      logger "Switching to context: #{@current_context} (from #{last_saved_context})"
      MetaHelper.inject_variable @current_context, :interpreter, @interpreter
      @current_context.instance_eval(&block)
    ensure
      @current_context = last_saved_context
      logger "Back to context: #{@current_context}"
    end
  end
end

#interpreterObject



43
44
45
# File 'lib/dsl_companion/features/basic.rb', line 43

def interpreter
  self if interpreter?
end

#interpreter?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dsl_companion/features/basic.rb', line 39

def interpreter?
  self.is_a? DSLCompanion::Interpreter
end

#logger(msg, level = :info) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/dsl_companion/features/basic.rb', line 47

def logger(msg, level=:info)
  if @logger.nil?
    STDERR.puts "#{level.to_s.upcase}: #{msg}"
  else
    @logger.send level, msg
  end
end