Module: SpookAndPay::ErroringReader

Included in:
CreditCard
Defined in:
lib/spook_and_pay/erroring_reader.rb

Instance Method Summary collapse

Instance Method Details

#attr_erroring_reader(*ivars) ⇒ Object

Defines a set of readers which will error if the underlying ivar is nil. It is intended to be used with a sub-set of readers which are important, but which may be nil. This is preferable to returning nil, which is falsy and will screw up any predicates.

Parameters:

  • Symbol

    ivars

Returns:

  • nil



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spook_and_pay/erroring_reader.rb', line 10

def attr_erroring_reader(*ivars)
  ivars.each do |i|
    class_eval %{
      def #{i}
        raise MissingValueError.new(:#{i}, self) unless defined?(@#{i})
        @#{i}
      end
    }
  end

  nil
end