Sha256: 53e5e7d58ce079f12faa85216e639759e954f50be84d82711cac8cce6b934efc

Contents?: true

Size: 579 Bytes

Versions: 5

Compression:

Stored size: 579 Bytes

Contents

# Implements the +safe_reader+ class method which will define (or override) readers that
# raise if ivar is nil
module Tracksperanto::Safety
  def self.included(into)
    into.extend(self)
    super
  end
  
  # Inject a reader that checks for nil
  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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
tracksperanto-4.2.0 lib/tracksperanto/safety.rb
tracksperanto-4.1.3 lib/tracksperanto/safety.rb
tracksperanto-4.1.2 lib/tracksperanto/safety.rb
tracksperanto-4.1.0 lib/tracksperanto/safety.rb
tracksperanto-4.0.0 lib/tracksperanto/safety.rb