Class: RightConf::Language
- Inherits:
-
Object
- Object
- RightConf::Language
- Defined in:
- lib/rconf/language.rb
Overview
DSL implementation
Instance Attribute Summary collapse
-
#configurators ⇒ Object
readonly
List of configurators resulting from parsing a configuration.
-
#validation_errors ⇒ Object
readonly
Errors generated by configurators validations if any.
-
#warnings ⇒ Object
readonly
Warnings.
Class Method Summary collapse
-
.load(file) ⇒ Object
Load given file and run content to retrieve configurators.
-
.parse(content) ⇒ Object
Parse given configuration text.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &blk) ⇒ Object (protected)
Each missing method should correspond to a configurator section. Such sections consist of a block which gets eval’ed in the contect of the corresponding configurator instance.
Parameters
- meth(Symbol)
-
Method symbol, should be a configurator
- args(Array)
-
List of arguments
- blk(Proc)
-
Block to be evaled in configurator context
Return
- true
-
Always return true
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rconf/language.rb', line 76 def method_missing(meth, *args, &blk) if blk klass = ConfiguratorRegistry[meth] if klass configurator = klass.new(@configurators.size) configurator.instance_eval(&blk) error = configurator.validate @validation_errors << error if error @configurators << configurator else @warnings << "Unknown configurator '#{meth}'" end else @validation_errors << "Invalid syntax, expecting block after '#{meth}'" end true end |
Instance Attribute Details
#configurators ⇒ Object (readonly)
List of configurators resulting from parsing a configuration
18 19 20 |
# File 'lib/rconf/language.rb', line 18 def configurators @configurators end |
#validation_errors ⇒ Object (readonly)
Errors generated by configurators validations if any
21 22 23 |
# File 'lib/rconf/language.rb', line 21 def validation_errors @validation_errors end |
#warnings ⇒ Object (readonly)
Warnings
24 25 26 |
# File 'lib/rconf/language.rb', line 24 def warnings @warnings end |
Class Method Details
.load(file) ⇒ Object
Load given file and run content to retrieve configurators
Parameters
- file(String)
-
Path to file being loaded
Result
- lang(Language)
-
Instance initialized from file
33 34 35 36 37 38 39 40 |
# File 'lib/rconf/language.rb', line 33 def self.load(file) begin content = IO.read(file) rescue Exception => e raise "Failed to load '#{file}': #{e.}" end parse(content) end |
.parse(content) ⇒ Object
Parse given configuration text
Parameters
- content(String)
-
Content to be parsed
Result
- lang(Language)
-
Instance initialized from content
49 50 51 52 53 |
# File 'lib/rconf/language.rb', line 49 def self.parse(content) lang = new lang.instance_eval(content) lang end |