/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

  1. class 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. # The number of times this Proc has been executed
  27. attr_accessor :execution_count
  28. # The block that you want executed when you call the <tt>execute</tt> method.
  29. attr_accessor :block
  30. # Requires a block to be passed into it.
  31. def initialize(&block)
  32. self.execution_count = 0
  33. self.block = block
  34. end
  35. # Executes the <tt>block</tt> attribute, ticks up the
  36. # <tt>execution_count</tt> attribute by one and then
  37. # returns the value of the executed <tt>block</tt>
  38. def execute
  39. val = self.block.call
  40. self.execution_count += 1
  41. return val
  42. end
  43. # Returns <tt>true</tt> if Configatron should cache the
  44. # results of the <tt>execute</tt> method, thereby never calling
  45. # it again.
  46. def finalize?
  47. self.execution_count == 1
  48. end
  49. end
  50. # Tells Configatron to always execute the block at runtime.
  51. # The results will never be cached.
  52. #
  53. # Example:
  54. # configatron.letters = 'a-b-c-d'
  55. # configatron.my.letters = Configatron::Delayed.new do
  56. # "My letters are: #{configatron.letters}"
  57. # end
  58. # configatron.my.other.letters = Configatron::Dynamic.new do
  59. # "My letters are: #{configatron.a.b.c.d}"
  60. # end
  61. #
  62. # configatron.my.letters # => 'My letters are: a-b-c-d'
  63. # configatron.my.other.letters # => 'My letters are: a-b-c-d'
  64. #
  65. # configatron.letters = 'a-b-c-d-e'
  66. #
  67. # configatron.my.letters # => 'My letters are: a-b-c-d'
  68. # configatron.my.other.letters # => 'My letters are: a-b-c-d-e'
  69. class Dynamic < Configatron::Proc
  70. def finalize?
  71. false
  72. end
  73. end
  74. # Tells Configatron to delay execution of the block until
  75. # runtime. Once run the results of the block will be cached,
  76. # never to be run again.
  77. #
  78. # Example:
  79. # configatron.letters = 'a-b-c-d'
  80. # configatron.my.letters = Configatron::Delayed.new do
  81. # "My letters are: #{configatron.letters}"
  82. # end
  83. # configatron.my.other.letters = Configatron::Dynamic.new do
  84. # "My letters are: #{configatron.a.b.c.d}"
  85. # end
  86. #
  87. # configatron.my.letters # => 'My letters are: a-b-c-d'
  88. # configatron.my.other.letters # => 'My letters are: a-b-c-d'
  89. #
  90. # configatron.letters = 'a-b-c-d-e'
  91. #
  92. # configatron.my.letters # => 'My letters are: a-b-c-d'
  93. # configatron.my.other.letters # => 'My letters are: a-b-c-d-e'
  94. class Delayed < Configatron::Proc
  95. end
  96. end