Sha256: 7f5639a24cef4c1688e44a355dd50a1277e0df31b9715c69bf0ba103b632ae78

Contents?: true

Size: 475 Bytes

Versions: 3

Compression:

Stored size: 475 Bytes

Contents

module Nasty
  class Lazy 
    def initialize(factory = ->(key) { key.new }, *arguments)
      @factory = factory
      @arguments = arguments
    end

    def method_missing(name, *args, &block)
      @target ||= @factory.call(*@arguments)
      @target.send(name, args, &block)
    end

    class << self
      def bind_factory(&block)
        @@factory = block
      end

      def load(key, factory = @@factory)
        Lazy.new(factory, key)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nasty-0.0.1395464273 lib/nasty/lazy.rb
nasty-0.0.1388168019 lib/nasty/lazy.rb
nasty-0.0.1388167257 lib/nasty/lazy.rb