Sha256: f84296ade913c72b5c8f622a125c14a21a0bc0a1f4784e72a63bcd6c55887679

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 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
    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!
      begin ; super() ; rescue NoMethodError ; nil ; end
      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(self)
      end
    end
  end

  Param.class_eval do
    include Configliere::ConfigBlock
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
configliere-0.0.3 lib/configliere/config_block.rb