Class: Nasty::Lazy

Inherits:
Object show all
Defined in:
lib/nasty/lazy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory = ->(key) { key.new }, *arguments) ⇒ Lazy

Returns a new instance of Lazy.



3
4
5
6
# File 'lib/nasty/lazy.rb', line 3

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



8
9
10
11
# File 'lib/nasty/lazy.rb', line 8

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

Class Method Details

.bind_factory(&block) ⇒ Object



14
15
16
# File 'lib/nasty/lazy.rb', line 14

def bind_factory(&block)
  @@factory = block
end

.load(key, factory = @@factory) ⇒ Object



18
19
20
# File 'lib/nasty/lazy.rb', line 18

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