Class: Nanoc::Int::LazyValue Private

Inherits:
Object
  • Object
show all
Includes:
ContractsSupport
Defined in:
lib/nanoc/base/entities/lazy_value.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Holds a value that might be generated lazily.

Instance Method Summary collapse

Methods included from ContractsSupport

included

Constructor Details

#initialize(value_or_proc) ⇒ LazyValue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of LazyValue.

Parameters:

  • value_or_proc (Object, Proc)

    A value or a proc to generate the value



9
10
11
# File 'lib/nanoc/base/entities/lazy_value.rb', line 9

def initialize(value_or_proc)
  @value = { raw: value_or_proc }
end

Instance Method Details

#freezevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



35
36
37
38
39
# File 'lib/nanoc/base/entities/lazy_value.rb', line 35

def freeze
  super
  @value.__nanoc_freeze_recursively unless @value[:raw]
  self
end

#map { ... } ⇒ Nanoc::Int::LazyValue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new lazy value that will apply the given transformation when the value is requested.

Yields:

  • resolved value

Returns:



29
30
31
# File 'lib/nanoc/base/entities/lazy_value.rb', line 29

def map
  Nanoc::Int::LazyValue.new(-> { yield(value) })
end

#valueObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The value, generated when needed.

Returns:

  • (Object)

    The value, generated when needed



14
15
16
17
18
19
20
21
# File 'lib/nanoc/base/entities/lazy_value.rb', line 14

def value
  if @value.key?(:raw)
    value = @value.delete(:raw)
    @value[:final] = value.respond_to?(:call) ? value.call : value
    @value.__nanoc_freeze_recursively if frozen?
  end
  @value[:final]
end