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.



353
354
355
356
357
358
359
360
361
362
363
# File 'lib/loquacious/configuration.rb', line 353

def initialize( opts = {}, &block )
  @description = nil
  @transform_to_store = nil
  @__config = opts[:config] || Configuration.new
  @__config.__defaults_mode = opts.key?(:defaults_mode) ? opts[:defaults_mode] : false
  @__config.__name = opts[:config_name] || nil
  @__config.__parent = opts[:parent_config] || nil
  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.



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/loquacious/configuration.rb', line 370

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
    self.transform(opts[:transform]) if opts.has_key? :transform
  end

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

Instance Attribute Details

#__configObject (readonly)

Returns the configuration object.



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

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.



342
343
344
345
# File 'lib/loquacious/configuration.rb', line 342

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.



391
392
393
394
395
396
# File 'lib/loquacious/configuration.rb', line 391

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

#transform(transform_proc) ⇒ Object



398
399
400
# File 'lib/loquacious/configuration.rb', line 398

def transform( transform_proc )
  @transform_to_store = transform_proc
end