CabezaDeTermo Ruby Utilities
Some Ruby utilities.
Status
Installation
Add this line to your application's Gemfile:
gem 'cdt-utilities', "~> 0.4"
And then execute:
$ bundle
Or install it yourself as:
$ gem install cdt-utilities
Usage
CdT.bind_block_evaluation_to
Utility to evaluate blocks binding self to an object only if the block
does not take parameters.
If the block takes parameters, call the block with those parameters keeping the binding as it is.
Examples:
require 'cdt/utilities'
# or if you just want this utility
require 'cdt/utilities/bind'
# binds self to 'hello!'
CdT.bind_block_evaluation_to('hello!') { puts self }
# binding to self is not changed
CdT.bind_block_evaluation_to('hello!') { |string| puts string }
CdT.bind_block_evaluation_to('hello', ' world', '!') { |string, arg_1, arg_2| puts string + arg_1 + arg_2}
CdT.subclass_responsibility
Utility to declare the implementation of a method to be the responsibility of a subclass, and raise an error when the subclass did not implement it.
Example:
require 'cdt/utilities'
# or if you just want this utility
require 'cdt/utilities/subclass-responsibility'
class SomeClass
def do_somehting()
CdT.subclass_responsibility
end
end
CdT.if_nil / if_not_nil
Utility to evaluate a block dependending on if an object is nil or not.
Examples:
require 'cdt/utilities'
# or if you just want this utility
require 'cdt/utilities/if-not-nil'
CdT.if_nil(nil) { 7 } # Evaluates to 7
CdT.if_nil(42) { 7 } # Evaluates to 42
CdT.if_not_nil(42) { |object| object + 1 } # Evaluates to 43
CdT.if_not_nil(nil) { |object| object + 1 } # Evaluates to nil
# Evaluates to 43
CdT.object 42,
if_not_nil: proc { |object| object + 1 },
if_nil: proc { 7 }
CdT.object 42,
if_not_nil: proc { |object| object + 1 },
# Evaluates to 7
CdT.object nil,
if_not_nil: proc { |object| object + 1 },
if_nil: proc { 7 }
# Evaluates to 42
CdT.object 42,
if_nil: proc { 7 }
A real world example of use is
def customer_name(customer_id)
CdT.object find_customer_by_id(customer_id),
if_not_nil: proc { |customer| customer.name },
if_nil: proc { raise "Customer with id=#{customer_id} not found." }
end
CdT.undefined
A singleton object that is identically different from all the other objects.
Usually nil is used as an undefined value. However, sometimes nil is a valid value in some contexts, so a different object must be used as an undefined value.
Examples:
require 'cdt/utilities'
# or if you just want this utility
require 'cdt/utilities/undefined'
def do_something(value = CdT.undefined)
if value == CdT.undefined
# ...
else
# ...
end
end
# you can also test the undefined value using
def do_something(value = CdT.undefined)
if value.kind_of?(CdT::Undefined)
# ...
else
# ...
end
end
Running the tests
bundle install
rspec
Contributing
- Fork it ( https://github.com/[my-github-username]/cdt-utilities/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request