safer

http://safer.rubyforge.org

DESCRIPTION:

Safer is an umbrella library, with components designed to make it simple to verify and improve the safety of your ruby code. There are at present three modules under the safer umbrella:

[Safer::IVar] generates specially-named accessor functions for class instance variables. [Safer::Protocol] is used to provide a ruby analogue to Objective-C Protocols (which are similar to Java interfaces, but do not require inheritance). [Safer::HashProtocol] verifies that a Hash keys follow a defined format. Intended to help use of Hash objects as keyword parameters.

FEATURES/PROBLEMS:

  • Name instance variables after the class in which they are defined. Generate accessor functions for these instance variables.
  • Define a "Protocol" class, and derive a signature from it. Verify that another class (or an instance of that class) implements the protocol signature.
  • Define a check if a Hash contains a desired combination of keys.

SYNOPSIS:

require 'safer/ivar'
require 'safer/protocol'
require 'safer/hashprotocol'

module MyModule
class MyProtocolClass
  def self.class_method_foo(arg1, arg2)
  end
  def instance_method(arg3)
  end
end
PROTOCOL = Safer::Protocol.create_from_class(MyProtocolClass)
class MyClass
  Safer::IVar.instance_variable(self, :var1, :var2)
  def initialize(var1, var2)
    PROTOCOL.instance_conforms?(var1)
    PROTOCOL.instance_conforms?(var2)
    self.mymodule_myclass__var1 = var1
    self.mymodule_myclass__var2 = var2
  end

  SHP = Safer::HashProtocol
  V1Keywords = SHP::HasKey.create(:name, :type)
  V2Keywords = SHP::HasKey.create(:first, :last, :type)
  V1Check = SHP::Only.new(SHP::Any.new(*V1Keywords))
  V2Check = SHP::Only.new(SHP::Any.new(*V2Keywords))
  ValidCheck = SHP::Any.new(V1Check, V2Check)
  def my_fn(h)
    if ! (ValidCheck === h)
      raise ArgumentError, "h should conform to #{ValidCheck.description}"
    end
    case h
    when V1Check
      v1_process(h[:name], h[:type])
    when V2Check
      v2_process(h[:first], h[:last], h[:type])
    end
  end
end
end

REQUIREMENTS:

  • None

INSTALL:

  • sudo gem install safer

DEVELOPERS:

After checking out the source, run:

$ rake newb

This task will install any missing dependencies, run the tests/specs, and generate the RDoc.

LICENSE:

This software is placed into the public domain.