Class: Loquacious::Configuration::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/loquacious/configuration.rb

Overview

Implementation of a domain specific language for creating configuration objects. Blocks of code are evaluted by the DSL which returns a new configuration object.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ DSL

Creates a new DSL and evaluates the given block in the context of the DSL.



314
315
316
317
318
319
320
321
# File 'lib/loquacious/configuration.rb', line 314

def initialize( opts = {}, &block )
  @description = nil
  @__config = opts[:config] || Configuration.new
  @__config.__defaults_mode = opts.key?(:defaults_mode) ? opts[:defaults_mode] : false
  instance_eval(&block)
ensure
  @__config.__defaults_mode = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Dynamically adds the given method to the configuration as an attribute. The args will be used to set the value of the attribute. If a block is given then the args are ignored and the attribute will be a nested configuration object.



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/loquacious/configuration.rb', line 328

def method_missing( method, *args, &block )
  m = method.to_s.delete('=').to_sym

  if args.length > 1
    opts = args.last.instance_of?(Hash) ? args.pop : {}
    self.desc(opts[:desc]) if opts.has_key? :desc
  end

  rv = __config.__send(m, *args, &block)
  __config.__desc[m] = @description if @description
  @description = nil
  rv
end

Instance Attribute Details

#__configObject (readonly)

Returns the configuration object.



309
310
311
# File 'lib/loquacious/configuration.rb', line 309

def __config
  @__config
end

Class Method Details

.evaluate(opts = {}, &block) ⇒ Object

Create a new DSL and evaluate the given block in the context of the DSL. Returns a newly created configuration object.



303
304
305
306
# File 'lib/loquacious/configuration.rb', line 303

def self.evaluate( opts = {}, &block )
  dsl = self.new(opts, &block)
  dsl.__config
end

Instance Method Details

#desc(string) ⇒ Object

Store the string as the description for the next attribute that will be configured. This description will be overwritten if the attribute has a description passed as an options hash.



346
347
348
349
350
351
# File 'lib/loquacious/configuration.rb', line 346

def desc( string )
  string = string.to_s
  string.strip!
  string.gutter!
  @description = string.empty? ? nil : string
end