class Observed::ConfigDSL

The DSL to describe Observed's configuration. @example context = ConfigDSL.new(builder: the_builder) context.eval_file observed_conf_file(a.k.a user code describes Observed configuration) context.config #=> can be used to instantiate Observed::System

Public Class Methods

new(args) click to toggle source
# File lib/observed/config_dsl.rb, line 23
def initialize(args)
  args[:builder] || fail("The key :builder must exist in #{args}")
  @builder = args[:builder]

  configure(args)
end

Public Instance Methods

config() click to toggle source

Build and returns the Observed configuration @return [Observed::Config]

# File lib/observed/config_dsl.rb, line 59
def config
  @builder.build
end
eval_file(file) click to toggle source
# File lib/observed/config_dsl.rb, line 30
def eval_file(file)
  @file = File.expand_path(file)
  working_directory File.dirname(@file)
  logger.debug "Reading the file: #{@file}"
  code = File.read(@file)
  logger.debug "Evaluating: #{code}"
  instance_eval(code, @file)
end
load!(file) click to toggle source

Load the file and evaluate the containing code in context of this object(a.k.a DSL). @param [String|Pathname] file The path to Ruby script containing the code in Observed's configuration DSL, typically `observed.conf`.

# File lib/observed/config_dsl.rb, line 66
def load!(file)
  eval_file file
end
require_relative(lib) click to toggle source

The replacement for Ruby's built-in `require_relative`. Although the built-in one can not be used in `eval` or `instance_eval` or etc because there is no `current file` semantics in `eval`, this replacement takes the file which is going to be evaluated as the `current file`. Thanks to this method, we can use `require_relative` in observed.conf files both when it is evaluated with `eval` and when it is evaluated in result of `require`.

# File lib/observed/config_dsl.rb, line 51
def require_relative(lib)
  path = File.expand_path("#{working_directory}/#{lib}")
  logger.debug "Require '#{path}'"
  require path
end
working_directory(wd=nil) click to toggle source

The `current directory` in which `require_relative` finds source files

# File lib/observed/config_dsl.rb, line 40
def working_directory(wd=nil)
  @working_directory = wd if wd
  @working_directory
end