Module: ConfigurationDsl

Defined in:
lib/configuration_dsl.rb,
lib/configuration_dsl/dsl.rb,
lib/configuration_dsl/impl.rb

Defined Under Namespace

Classes: Dsl, Error, Impl

Constant Summary collapse

VERSION =
File.read(File.dirname(__FILE__)+"/../VERSION")

Instance Method Summary collapse

Instance Method Details

#configurationObject



47
48
49
50
# File 'lib/configuration_dsl.rb', line 47

def configuration
  @configuration_dsl ||= Impl.new(self)
  @configuration_dsl.find_configuration
end

#configure(&block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/configuration_dsl.rb', line 29

def configure(&block)
  @configuration_dsl ||= Impl.new(self)
  
  # Instance eval the block.
  if block_given?
    _module = @configuration_dsl.find_module
    raise Error, "cannot find configuration module" unless _module
    dsl = Dsl.new(Impl.dup_struct(configuration)) # Dup it to unfreeze it.
    dsl.send(:extend, _module)
    dsl.instance_eval(&block)
    @configuration_dsl.configuration = dsl.configuration.freeze
  end
  
  # Run the callback.
  callback = @configuration_dsl.find_callback
  instance_eval(&callback) if callback
end

#configure_with(configuration_module, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/configuration_dsl.rb', line 10

def configure_with(configuration_module, &block)
  @configuration_dsl ||= Impl.new(self)
  @configuration_dsl.module = configuration_module
  @configuration_dsl.callback = block if block_given?
  @configuration_dsl.default_configuration!
  
  # Automatically define setters.
  @configuration_dsl.module.module_eval do
    self::DEFAULTS.keys.each do |name|
      next if method_defined?(name) # Don't override custom setters.
      module_eval <<-code
        def #{name}(value)
          configuration.#{name} = value
        end
      code
    end
  end
end