# Copyright (C) 2011 RightScale, Inc, All Rights Reserved Worldwide. # # THIS PROGRAM IS CONFIDENTIAL AND PROPRIETARY TO RIGHTSCALE # AND CONSTITUTES A VALUABLE TRADE SECRET. Any unauthorized use, # reproduction, modification, or disclosure of this program is # strictly prohibited. Any use of this program by an authorized # licensee is strictly subject to the terms and conditions, # including confidentiality obligations, set forth in the applicable # License Agreement between RightScale.com, Inc. and # the licensee module RightConf # DSL implementation class Language # List of configurators resulting from parsing a configuration attr_reader :configurators # Errors generated by configurators validations if any attr_reader :validation_errors # Load given file and run content to retrieve configurators # # === Parameters # file(String):: Path to file being loaded # # === Result # lang(Language):: Instance initialized from file def self.load(file) begin content = IO.read(file) rescue Exception => e raise "Failed to load '#{file}': #{e.message}" end parse(content) end # Parse given configuration text # # === Parameters # content(String):: Content to be parsed # # === Result # lang(Language):: Instance initialized from content def self.parse(content) lang = new lang.instance_eval(content) lang end protected # Initialize configurators and validation errors lists # Do not call directly, instead call 'load' def initialize @configurators = Array.new @validation_errors = Array.new end # 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 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 end end