Class: SimpleDSL
- Inherits:
-
Object
- Object
- 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.
Direct Known Subclasses
Defined Under Namespace
Classes: ConfigFileMissingError
Instance Method Summary collapse
-
#config(action = nil) ⇒ Object
Returns the code with the DSL that was executed.
-
#initialize(method = nil, file = nil, &block) ⇒ SimpleDSL
constructor
Processes a DSL.
- #parse(method = nil, actions = nil, &block) ⇒ Object
Constructor Details
#initialize(method = nil, file = nil, &block) ⇒ SimpleDSL
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.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rbbt/util/simpleDSL.rb', line 64 def initialize(method = nil, file = nil, &block) @config = {} if file raise ConfigFileMissingError.new "File '#{ file }' is missing. Have you installed the config files? (use rbbt_config)." unless File.exists? file parse(method, file) end if block parse(method, block) end end |
Instance Method Details
#config(action = nil) ⇒ Object
Returns the code with the DSL that was executed. If it came from a block it was turned to string using ruby2ruby.
78 79 80 81 82 83 84 |
# File 'lib/rbbt/util/simpleDSL.rb', line 78 def config(action = nil) if action @config[action.to_sym] else @config[:DSL_action] end end |
#parse(method = nil, actions = nil, &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rbbt/util/simpleDSL.rb', line 37 def parse(method = nil, actions = nil, &block) actions ||= block hook_method(method) # Execute if actions.is_a? Proc @config[@@method_name] = actions.to_ruby.collect[1..-2].join instance_eval &actions elsif File.exists?(actions) @config[@@method_name] = File.open(actions).read eval File.open(actions).read end unhook_method end |