Skip to content
forked from brightbox/rujitsu

Various Helper Methods to smooth your Ruby development day

Notifications You must be signed in to change notification settings

rahoulb/rujitsu

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rujitsu

A ruby gem with various helper methods to smooth out your Ruby development.

Install

Add the following to your Gemfile

gem 'rujitsu'

Including

require "rujitsu"
require "rujitsu/grammar"

For using in irb, there’s a nice shortcut for you to use:

$ irb -rubygems -rujitsu

(This works because -r is the argument to require a file, and both ‘ubygems.rb’ and ‘ujitsu.rb’ exist.)

Rails

Documentation

Run rake docs and open doc/index.html.

Usage

Generating random strings

The Fixnum class has a couple of extensions allowing you to generate random strings.

5.random_letters
5.random_numbers
5.random_characters

You can also generate a variable length string.

(3..5).random_letters

This generates a string of random letters whose length is 3, 4 or 5 characters.

(All the above methods exist in the singular too. eg, 1.random_letter)

URL-friendly strings

The String class has an extension that strips out URL-unfriendly characters.

""$%hello!@ 123 there'".to_url # => "hello-123-there"

Truncating strings

The String class has an extension that truncates it to a customisable length with a customisable suffix.

"this is a string".truncate(:length => 15) # => "this is a st..."

Testing for inclusion within a container

As Rubyists we are all familiar with using include? to test if our variable is one of a number of values.

if ['hello', 'world'].include?(@my_value) ...

However, it doesn’t read correctly. Rujitsu allows you to write the following as it reads much more like English.

if @my_value.in? ['hello', 'world'] ...

Testing for mandatory parameters

We like named parameters. They are good. But, unlike Smalltalk, they don’t form part of the method signature, so if you pass them in as a Hash, you need to test that the parameters you expect have been supplied. Like this …

def my_method parameters = {}
  parameters.must_have_keys_for :this, :that, :the_other
end

This will raise an ‘ArgumentError` if parameters does not contain the given keys.

There’s also:

def my_method parameters = {}
  parameters.must_have_values_for :this, :that, :the_other
end

This will raise an ‘ArgumentError` if any of the supplied keys have a nil value associated with them.

Grammar

So far the grammar library just adds the method should_recieve for rspec assertions. Use it to find out what it does!

Released under the MIT Licence

Copyright © 2008 Brightbox Systems Ltd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  • The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

See www.brightbox.co.uk/ for contact details.

About

Various Helper Methods to smooth your Ruby development day

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%