module Tracksperanto::Safety

Implements the safe_reader class method which will define (or override) readers that raise if ivar is nil

Public Class Methods

included(into) click to toggle source
Calls superclass method
# File lib/tracksperanto/safety.rb, line 4
def self.included(into)
  into.extend(self)
  super
end

Public Instance Methods

safe_reader(*attributes) click to toggle source

Inject a reader that checks for nil

# File lib/tracksperanto/safety.rb, line 10
def safe_reader(*attributes)
  attributes.each do | an_attr |
    alias_method "#{an_attr}_without_nil_protection", an_attr
    define_method(an_attr) do
      val = send("#{an_attr}_without_nil_protection")
      raise "Expected #{an_attr} on #{self} not to be nil" if val.nil?
      val
    end
  end
end