Sha256: fb35b98241c7a54ac57fa1f49674430555b2f300ec96ff5bb31cdfd8c128ad29

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

Configliere.use :define
module Configliere
  #
  # ConfigBlock lets you use pure ruby to change and define settings.  Call
  # +#finally+ with a block of code to be run after all other settings are in
  # place.
  #
  #     Settings.finally{|c| c.your_mom[:college] = 'went' unless (! c.mom_jokes_allowed) }
  #
  module ConfigBlock
    #
    # @param &block each +finally+ block is called once, in the order it was
    #   defined, when the resolve! method is invoked. +config_block+ resolution
    #   is guaranteed to run last in the resolve chain, right before validation.
    #
    # @example
    #   Settings.finally do |c|
    #     c.dest_time = (Time.now + 60) if c.username == 'einstein'
    #     # you can use hash syntax too
    #     c[:dest_time] = (Time.now + 60) if c[:username] == 'einstein'
    #   end
    #   # ...
    #   # after rest of setup:
    #   Settings.resolve!
    #
    def finally &block
      self.final_blocks << block if block
    end

    # Processing to reconcile all options
    #
    # The resolve! for config_block is made to run last of all in the +resolve!+
    # chain, and runs each +finally+ block in the order it was defined.
    def resolve!
      super
      resolve_finally_blocks!
      self
    end

  protected
    # Config blocks to be executed at end of resolution (just before validation)
    attr_accessor :final_blocks
    def final_blocks
      @final_blocks ||= []
    end
    # call each +finally+ config block in the order it was defined
    def resolve_finally_blocks!
      final_blocks.each do |block|
        block.call()
      end
    end
  end

  Param.class_eval do
    include Configliere::ConfigBlock
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
configliere-0.3.4 lib/configliere/config_block.rb
configliere-0.3.3 lib/configliere/config_block.rb
configliere-0.2.3 lib/configliere/config_block.rb
configliere-0.3.2 lib/configliere/config_block.rb
configliere-0.3.1 lib/configliere/config_block.rb
configliere-0.3.0 lib/configliere/config_block.rb
configliere-0.2.2 lib/configliere/config_block.rb
configliere-0.2.1 lib/configliere/config_block.rb