darrencauthon/subtle

View on GitHub
lib/subtle/lazy_cover.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Subtle
  class LazyCover < Cover
    include Subtle::StrictCoverMethods

    def initialize(block_that_creates_the_object)
      @block = block_that_creates_the_object
    end

    alias :the_original_subject_from_the_base_class :the_original_subject
    def the_original_subject
      setup_the_subject
      the_original_subject_from_the_base_class
    end

    private

    alias :old_method_missing :method_missing
    def method_missing(meth, *args, &blk)
      setup_the_subject
      old_method_missing meth, *args, &blk
    end

    def setup_the_subject
      @subject = @block.call if @subject.nil?
    end
  end
end