Class: ConfigDSL::LazyValue

Inherits:
Object
  • Object
show all
Defined in:
lib/configdsl.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, args = [], options = {}) ⇒ LazyValue

Returns a new instance of LazyValue.



220
221
222
223
224
225
# File 'lib/configdsl.rb', line 220

def initialize(block, args = [], options = {})
  @block = block
  @args = args
  @options = self.class.default_options.merge(options)
  @cached = false
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



218
219
220
# File 'lib/configdsl.rb', line 218

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



218
219
220
# File 'lib/configdsl.rb', line 218

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



218
219
220
# File 'lib/configdsl.rb', line 218

def options
  @options
end

Class Method Details

.default_optionsObject



212
213
214
215
216
# File 'lib/configdsl.rb', line 212

def self.default_options
  {
    caching: true        
  }
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


246
247
248
# File 'lib/configdsl.rb', line 246

def cached?
  @cached
end

#caching?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/configdsl.rb', line 237

def caching?
  !!options[:caching]
end

#flush_cache!Object



241
242
243
244
# File 'lib/configdsl.rb', line 241

def flush_cache!
  @cache = nil # clean pointer so that GC can do it's work immediately
  @cached = true
end

#value(new_args = nil) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/configdsl.rb', line 227

def value(new_args = nil)
  return @cache if caching? && cached?
  value = block.call(*(new_args ? new_args : args))
  if caching?
    @cache = value
    @cached = true
  end
  value
end