PageRenderTime 51ms CodeModel.GetById 46ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/gems/1.8/gems/configatron-2.8.2/lib/configatron/proc.rb

http://github.com/agross/netopenspace
Ruby | 104 lines | 25 code | 8 blank | 71 comment | 1 complexity | 79d9b3682ecef54983cc9e59d5d15531 MD5 | raw file
  1class Configatron
  2  # This class can be used to give special powers to a Configatron setting.
  3  # See Configatron::Delayed and Configatron::Dynamic as examples of how this
  4  # works.
  5  # 
  6  # This class can be subclassed easily. The key is to override the <tt>finalize?</tt>
  7  # method.
  8  # 
  9  # Example:
 10  #   class RunThreeTimes < Configatron::Proc
 11  #     def finalize?
 12  #       self.execution_count == 3
 13  #     end
 14  #   end
 15  # 
 16  #   configatron.some.rand.generator = RunThreeTimes.new do
 17  #     rand
 18  #   end
 19  #   
 20  #   configatron.some.rand.generator # => 0.169280668547299
 21  #   configatron.some.rand.generator # => 0.298880544243205
 22  #   configatron.some.rand.generator # => 0.421091617110779
 23  #   configatron.some.rand.generator # => 0.421091617110779
 24  #   configatron.some.rand.generator # => 0.421091617110779
 25  class Proc
 26
 27    # The number of times this Proc has been executed
 28    attr_accessor :execution_count
 29    # The block that you want executed when you call the <tt>execute</tt> method.
 30    attr_accessor :block
 31    
 32    # Requires a block to be passed into it.
 33    def initialize(&block)
 34      self.execution_count = 0
 35      self.block = block
 36    end
 37    
 38    # Executes the <tt>block</tt> attribute, ticks up the 
 39    # <tt>execution_count</tt> attribute by one and then
 40    # returns the value of the executed <tt>block</tt>
 41    def execute
 42      val = self.block.call
 43      self.execution_count += 1
 44      return val
 45    end
 46    
 47    # Returns <tt>true</tt> if Configatron should cache the
 48    # results of the <tt>execute</tt> method, thereby never calling
 49    # it again.
 50    def finalize?
 51      self.execution_count == 1
 52    end
 53    
 54  end
 55  
 56  # Tells Configatron to always execute the block at runtime.
 57  # The results will never be cached.
 58  # 
 59  # Example:
 60  #   configatron.letters = 'a-b-c-d'
 61  #   configatron.my.letters = Configatron::Delayed.new do
 62  #     "My letters are: #{configatron.letters}"
 63  #   end
 64  #   configatron.my.other.letters = Configatron::Dynamic.new do
 65  #     "My letters are: #{configatron.a.b.c.d}"
 66  #   end
 67  #   
 68  #   configatron.my.letters # => 'My letters are: a-b-c-d'
 69  #   configatron.my.other.letters # => 'My letters are: a-b-c-d'
 70  #   
 71  #   configatron.letters = 'a-b-c-d-e'
 72  #   
 73  #   configatron.my.letters # => 'My letters are: a-b-c-d'
 74  #   configatron.my.other.letters # => 'My letters are: a-b-c-d-e'
 75  class Dynamic < Configatron::Proc
 76    def finalize?
 77      false
 78    end
 79  end
 80  
 81  # Tells Configatron to delay execution of the block until
 82  # runtime. Once run the results of the block will be cached,
 83  # never to be run again.
 84  # 
 85  # Example:
 86  #   configatron.letters = 'a-b-c-d'
 87  #   configatron.my.letters = Configatron::Delayed.new do
 88  #     "My letters are: #{configatron.letters}"
 89  #   end
 90  #   configatron.my.other.letters = Configatron::Dynamic.new do
 91  #     "My letters are: #{configatron.a.b.c.d}"
 92  #   end
 93  #   
 94  #   configatron.my.letters # => 'My letters are: a-b-c-d'
 95  #   configatron.my.other.letters # => 'My letters are: a-b-c-d'
 96  #   
 97  #   configatron.letters = 'a-b-c-d-e'
 98  #   
 99  #   configatron.my.letters # => 'My letters are: a-b-c-d'
100  #   configatron.my.other.letters # => 'My letters are: a-b-c-d-e'
101  class Delayed < Configatron::Proc
102  end
103  
104end