module SimpleDSL
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.
Public Instance Methods
Source
# 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
Source
# 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.exist? file parse(method, file) end if block parse(method, block) end end
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
.
Source
# File lib/rbbt/util/simpleDSL.rb, line 34 def parse(method = nil, actions = nil, &block) actions ||= block hook_method(method) # Execute @config ||= {} if actions.is_a? Proc begin require 'method_source' @config[@@method_name] = actions.source.split("\n")[1..-2] * "\n" rescue Exception Log.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.exist?(actions) @config[@@method_name] = File.open(actions).read instance_eval Open.read(actions), actions end unhook_method end
Private Instance Methods
Source
# File lib/rbbt/util/simpleDSL.rb, line 13 def hook_method(method = nil) method ||= :DSL_action @@restore_name = ("restore_DSL_" + method.to_s).to_sym @@method_name = method.to_sym class << self @restore_stack ||= [] @restore_stack << @@restore_name alias_method(@@restore_name, :method_missing) alias_method(:method_missing, @@method_name) end end
Source
# File lib/rbbt/util/simpleDSL.rb, line 26 def unhook_method class << self alias_method(:method_missing, @restore_stack.pop) end end