| Class | Configatron::Proc |
| In: |
lib/configatron/proc.rb
|
| Parent: | Object |
This class can be used to give special powers to a Configatron setting. See Configatron::Delayed and Configatron::Dynamic as examples of how this works.
This class can be subclassed easily. The key is to override the finalize? method.
Example:
class RunThreeTimes < Configatron::Proc
def finalize?
self.execution_count == 3
end
end
configatron.some.rand.generator = RunThreeTimes.new do
rand
end
configatron.some.rand.generator # => 0.169280668547299
configatron.some.rand.generator # => 0.298880544243205
configatron.some.rand.generator # => 0.421091617110779
configatron.some.rand.generator # => 0.421091617110779
configatron.some.rand.generator # => 0.421091617110779
Requires a block to be passed into it.
# File lib/configatron/proc.rb, line 33
33: def initialize(&block)
34: self.execution_count = 0
35: self.block = block
36: end
Executes the block attribute, ticks up the execution_count attribute by one and then returns the value of the executed block
# File lib/configatron/proc.rb, line 41
41: def execute
42: val = self.block.call
43: self.execution_count += 1
44: return val
45: end
Returns true if Configatron should cache the results of the execute method, thereby never calling it again.
# File lib/configatron/proc.rb, line 50
50: def finalize?
51: self.execution_count == 1
52: end