Sha256: 3a39ad65e240fe4073642cc400cec4fa942b999a4423823ee26d4c6d6a507a59

Contents?: true

Size: 1.08 KB

Versions: 41

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module Nanoc::Int
  # Holds a value that might be generated lazily.
  #
  # @api private
  class LazyValue
    include Nanoc::Int::ContractsSupport

    # @param [Object, Proc] value_or_proc A value or a proc to generate the value
    def initialize(value_or_proc)
      @value = { raw: value_or_proc }
    end

    # @return [Object] The value, generated when needed
    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

    contract C::Func[C::Any => C::Any] => self
    # Returns a new lazy value that will apply the given transformation when the value is requested.
    #
    # @yield resolved value
    #
    # @return [Nanoc::Int::LazyValue]
    def map
      Nanoc::Int::LazyValue.new(-> { yield(value) })
    end

    contract C::None => self
    # @return [void]
    def freeze
      super
      @value.__nanoc_freeze_recursively unless @value[:raw]
      self
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
nanoc-4.11.0 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.10.4 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.10.3 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.10.2 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.10.1 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.10.0 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.9 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.8 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.7 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.6 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.5 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.4 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.3 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.2 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.1 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.9.0 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.8.19 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.8.18 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.8.17 lib/nanoc/base/entities/lazy_value.rb
nanoc-4.8.16 lib/nanoc/base/entities/lazy_value.rb