Module: SimpleDSL

Defined in:
lib/rbbt/util/simpleDSL.rb

Overview

This class helps designing DSL in ruby based on method_missing. Class is initialize with a block of code or a file with the code, and it is given a method to be invoked instead of method missing. This class deals simply with making the method_missing alias and removing it and executing the block of file with code.

Defined Under Namespace

Classes: ConfigFileMissingError, NoRuby2Ruby

Instance Method Summary collapse

Instance Method Details

#config(action = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rbbt/util/simpleDSL.rb', line 79

def config(action = nil)
  if action
    config = @config[action.to_sym]
  else
    config = @config[:DSL_action]
  end

  raise config if NoRuby2Ruby === config

  config
end

#load_config(method = nil, file = nil, &block) ⇒ Object

Processes a DSL. method is the name of the method executed instead of method_missing. The code to be evaluated as a DSL is either specified in &block or in the file pointed by file.



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbbt/util/simpleDSL.rb', line 67

def load_config(method = nil, file = nil, &block)
  @config = {}
  if file
    raise ConfigFileMissingError.new "Config file '#{ file }' is missing" unless File.exists? file
    parse(method, file)
  end

  if block
    parse(method, block)
  end
end

#parse(method = nil, actions = nil, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rbbt/util/simpleDSL.rb', line 34

def parse(method = nil, actions = nil, &block)

  actions ||= block
  
  hook_method(method)

  # Execute
  if actions.is_a? Proc
    begin
      require 'parse_tree_extensions'
      require 'parse_tree'
      require 'ruby2ruby'
      @config[@@method_name] = actions.to_ruby.collect[1..-2].join
    rescue Exception
      @config[@@method_name] = NoRuby2Ruby.new "The gem ruby2ruby is not installed. It will not work on ruby 1.9."
    end
    
    instance_eval &actions
  elsif File.exists?(actions)
    
    @config[@@method_name] = File.open(actions).read

    instance_eval Open.read(actions), actions
  end

  unhook_method

end