Module Lazy
In: lib/grit/lazy.rb

Allows attributes to be declared as lazy, meaning that they won‘t be computed until they are asked for.

Works by delegating each lazy_reader to a cached lazy_source method.

class Person

  lazy_reader :eyes

  def lazy_source
    OpenStruct.new(:eyes => 2)
  end

end

>> Person.new.eyes

> 2

Methods

Public Instance methods

[Source]

    # File lib/grit/lazy.rb, line 19
19:   def lazy_reader(*args)
20:     args.each do |arg|
21:       ivar = "@#{arg}"
22:       define_method(arg) do
23:         if instance_variable_defined?(ivar)
24:           val = instance_variable_get(ivar)
25:           return val if val
26:         end
27:         instance_variable_set(ivar, (@lazy_source ||= lazy_source).send(arg))
28:       end
29:     end
30:   end

[Validate]