Tektite's Ruby Utils

Gem Version Build Status Code Climate Test Coverage Inline docs

This gem contains a number of utilities, extensions, and helpers for Ruby to make developing applications easier and to enable better security and integration with other services. Enjoy! Suggestions are most welcome.

Installation

Add this line to your application's Gemfile:

gem 'tektite_ruby_utils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install tektite_ruby_utils

Usage

Below is a list of everything this gem includes.

PresentClass and present

PresentClass adds a new class and related helpers designed to be the opposite of NilClass, and includes present in the global namespace as the opposite of nil. It is used to represent that a value is not nil without exposing the value itself. Unlike simple using !nil?, PresentClass returns an object that can be configured to carry variable amounts of data about the value behind it, such as the object's type and class. PRESENT is also included as a constant, which is a frozen instance of PresentClass that present returns.

Furthermore, the extensions includes several helpers for working with any object, Hashes, and Arrays.

This is extremely useful if for example you would like to pass information about whether certain data exists to a method without telling the method what the actual values of that data are.

For any object, you can use the present? method:

class MyObject
  attr_accessor :my_value
  ...
end

my_object = MyObject.new my_value: "test"

my_object.my_value.present?
# => true

nil.present?
# => false

present.present?
# => true

present.nil?
# => false

nil
# => nil

present
# => present

For Arrays and Hashes, the all_present?, each_present?, and mask_present helper methods are available:

array = [1, 2, nil]
array.all_present?
# => false
array.each_present?
# => [true, true, false]
array.mask_present
# => [present, present, nil]

hash = {one: 1, two: 2, three: 3}
hash.all_present?
# => true
hash.each_present?
# => {one: true, two: true, three: true}
hash.mask_present
# => {one: present, two: present, three: present}

If you would like to return an instance of PresentClass that includes the type of the original value, just create a new instance of PresentClass directly specifying the :type attribute as a Class. For all other users, simply set a variable to present as if you were using nil. As a helper for this, the present_with_type method is available:

test_array = [1, 'two', :three]
test_array.each do |e|
  puts e.present_with_type.type
  puts "\n"
end

# => Fixnum
# => String
# => Symbol

Documentation

Documentation for this gem can be found here: http://www.rubydoc.info/github/tektite-software/ruby_utils/

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tektite-software/ruby_utils.

License

The gem is available as open source under the terms of the MIT License.