
class Module

  # Like attr_writer, but the writer method validates the 
  # setting against the given block.

  def attr_validator(*symbols, &validator)
    symbols.each do |symbol|
      define_method "#{symbol}=" do |val|
        unless validator.call(val)
          raise ArgumentError, "Invalid value provided for #{symbol}"
        end
        instance_variable_set("@#{symbol}", val)
      end
    end
  end

end
