Sha256: 2578c64b95d10677dc474d466433f7b65cdcf6311d1406d1d50e1776419c7466

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

=begin rdoc

= NackClass

The NackClass is is like the NilClass except one step down.
Nack is never used to to mean nothingness or emptiness.
The Nack class is only used to report something is "a miss" w/o
the system raising an Exception. It's a mechanism for
lazy error evaluation.

== Usage

  def scalar( x )
    if x.kind_of?(Enumerable)
      return nack(ArgumentError, "Value must not be enumerable")
    end
    x
  end

  a = scaler( 1 )            #=> 1
  b = scaler( [1,2] )        #=> nack
  b.first                    #=> ArgumentError (previous)
  
  a = nail scaler( 1 )       #=> 1
  b = nail scaler( [1,2] )   #=> ArgumentError

== TODO

* I beleive this class has even more potential yet.
  It would be interesting to see if a catch/try correction
  facility could be built into it too.

== Author(s)

* Thomas Sawyer

== Legal

Ruby License
Copyright (c)2004 Thomas Sawyer


== History

* 2005.04.11 Passed basic test.

=end

class InvalidNackError < ArgumentError
end

class NackClass

  attr_reader :error, :data

  def initialize(error=nil, *data, &ctrl)
    if Class === error and error <= Exception
      @error = error.new(*data)
    elsif error.kind_of?( Exception )
      @error = error
    elsif error.kind_of?( String )
      @error = StandardError.new(error)
    elsif error == nil
      @error = StandardError.new
    else
      raise InvalidNackError
    end
    @data = data
    @ctrl = ctrl
  end

  def call(*args)
    @ctrl.call(*args)
  end

  def call_with_data
    @ctrl.call(*@data)
  end

  def raise_error
    raise @error
  end

  def to_s; "n/a"; end
  
  def ==(x)
    x.is_a?(NackClass)
  end

  def method_missing(meth, *args)
    raise_error
  end

end

module Kernel
  def nack(err=nil, *data, &ctrl)
    NackClass.new(err, *data, &ctrl)
  end
end

# private Object method
def nail( arg )
  if NackClass === arg
    arg.raise_error
  else
    return arg
  end
end

#class NilClass
#  def method_missing(meth, *args)
#    nack()
#  end
#end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carats-0.3.0 lib/carat/nack.rb