Class: RightConf::Language

Inherits:
Object
  • Object
show all
Defined in:
lib/rconf/language.rb

Overview

DSL implementation

Instance Attribute Summary collapse

Class Method Summary collapse

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/rconf/language.rb', line 72

def method_missing(meth, *args, &blk)
  if blk 
    klass = ConfiguratorRegistry[meth]
    if klass
      configurator = klass.new(*args)
      configurator.instance_eval(&blk)
      error = configurator.validate
      @validation_errors << error if error
      @configurators << configurator
    else
      @validation_errors << "Unknown configurator '#{meth}'"
    end
  else
    @validation_errors << "Invalid syntax, expecting block after '#{meth}'"
  end
  true
end

Instance Attribute Details

#configuratorsObject (readonly)

List of configurators resulting from parsing a configuration



18
19
20
# File 'lib/rconf/language.rb', line 18

def configurators
  @configurators
end

#validation_errorsObject (readonly)

Errors generated by configurators validations if any



21
22
23
# File 'lib/rconf/language.rb', line 21

def validation_errors
  @validation_errors
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



30
31
32
33
34
35
36
37
# File 'lib/rconf/language.rb', line 30

def self.load(file)
  begin
    content = IO.read(file)
  rescue Exception => e
    raise "Failed to load '#{file}': #{e.message}"
  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



46
47
48
49
50
# File 'lib/rconf/language.rb', line 46

def self.parse(content)
  lang = new
  lang.instance_eval(content)
  lang
end