Class: Observed::ConfigDSL
- Inherits:
-
Object
- Object
- Observed::ConfigDSL
- Extended by:
- Forwardable
- Includes:
- Configurable
- Defined in:
- lib/observed/config_dsl.rb
Overview
The DSL to describe Observed’s configuration. 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
Instance Method Summary collapse
-
#config ⇒ Observed::Config
Build and returns the Observed configuration.
- #eval_file(file) ⇒ Object
-
#initialize(args) ⇒ ConfigDSL
constructor
A new instance of ConfigDSL.
-
#load!(file) ⇒ Object
Load the file and evaluate the containing code in context of this object(a.k.a DSL).
-
#require_relative(lib) ⇒ Object
The replacement for Ruby’s built-in ‘require_relative`.
-
#working_directory(wd = nil) ⇒ Object
The ‘current directory` in which `require_relative` finds source files.
Methods included from Configurable
#configure, #get_attribute_value, #has_attribute_value?, included
Constructor Details
#initialize(args) ⇒ ConfigDSL
Returns a new instance of ConfigDSL.
23 24 25 26 27 28 |
# 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 |
Instance Method Details
#config ⇒ Observed::Config
Build and returns the Observed configuration
59 60 61 |
# File 'lib/observed/config_dsl.rb', line 59 def config @builder.build end |
#eval_file(file) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/observed/config_dsl.rb', line 30 def eval_file(file) @file = File.(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) ⇒ Object
Load the file and evaluate the containing code in context of this object(a.k.a DSL). typically ‘observed.conf`.
66 67 68 |
# File 'lib/observed/config_dsl.rb', line 66 def load!(file) eval_file file end |
#require_relative(lib) ⇒ Object
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`.
51 52 53 54 55 |
# File 'lib/observed/config_dsl.rb', line 51 def require_relative(lib) path = File.("#{working_directory}/#{lib}") logger.debug "Require '#{path}'" require path end |
#working_directory(wd = nil) ⇒ Object
The ‘current directory` in which `require_relative` finds source files
40 41 42 43 |
# File 'lib/observed/config_dsl.rb', line 40 def working_directory(wd=nil) @working_directory = wd if wd @working_directory end |