Sha256: 5c1438bc948e11bc6f7d3ffe85394b90273da6ef4e383eeb1c3b481e4e7c6d76

Contents?: true

Size: 877 Bytes

Versions: 1

Compression:

Stored size: 877 Bytes

Contents

module Simple
  class Lazy
    VERSION = "1.0.1"

    attr_reader :value

    def initialize(value, &block)
      @block = block
      @value = value
      @cached = nil
      @is_cached = false
    end

    def hash
      value.hash
    end

    def eql?(other)
      other.is_a?(Simple::Lazy) ? value == other.value : value == other
    end

    def <=>(other)
      other.is_a?(Simple::Lazy) ? value <=> other.value : value <=> other
    end

    def method_missing(method, *args, &block)
      cached.public_send(method, *args, &block)
    end

    def cached
      unless @is_cached
        @cached = @block.call(value)
        @is_cached = true
      end

      @cached
    end

    def cached?
      @is_cached
    end

    def to_s
      cached.to_s
    end

    def inspect
      "#<Simple::Lazy @value=#{@value.inspect} cached?=#{cached?.inspect}>"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple-lazy-1.0.1 lib/simple/lazy.rb