Refinements

Gem Version Code Climate GPA Code Climate Coverage Gemnasium Status Travis CI Status Patreon

Provides a collection of refinements for core Ruby objects.

Table of Contents

Features

  • Adds BigDecimal refinements:
    • BigDecimal#inspect - Allows one to inspect a big decimal with numeric representation.
  • Adds Array refinements:
    • Array#compress - Removes nil and empty values without modifying itself.
    • Array#compress! - Removes nil and empty values and modifies itself.
  • Adds Hash refinements:
    • #compact - Removes key/value pairs with nil values without modifying itself.
    • #compact! - Removes key/value pairs with nil values and modifies itself.
    • #deep_merge - Merges deeply nested hashes together without itself.
    • #deep_merge! - Merges deeply nested hashes together and modifies itself.
    • #reverse_merge - Merges calling hash into passed in hash without modifying calling hash.
    • #reverse_merge! - Merges calling hash into passed in hash and modifies calling hash.
  • Adds String refinements:
    • #camelcase - Answers a camelcased string. Example: "ThisIsCamelcase".
    • #snakecase - Answers a snakecased string. Example: "this_is_snakecase".
    • #titleize - Answers titleized string. Example: "This Is Titleized".

Requirements

  1. MRI 2.3.x.
  2. A solid understanding of Ruby refinements and lexical scope.

Setup

For a secure install, type the following from the command line (recommended):

gem cert --add <(curl --location --silent https://www.alchemists.io/gem-public.pem)
gem install refinements --trust-policy MediumSecurity

NOTE: A HighSecurity trust policy would be best but MediumSecurity enables signed gem verification while allowing the installation of unsigned dependencies since they are beyond the scope of this gem.

For an insecure install, type the following (not recommended):

gem install refinements

Add the following to your Gemfile file:

gem "refinements"

Usage

Requires

Due to this gem being a collection of Ruby refinements, none of the refinements are auto-loaded by default in order to reduce code bloat for your app. Instead, require the specific requirement for the code that needs it. You'll want to require one or all of the following:

require "refinements/string_extensions"
require "refinements/big_decimal_extensions"
require "refinements/array_extensions"
require "refinements/hash_extensions"

Using

In addition to requiring the appropriate refinement file for the code that needs it, you'll also need to use the refinement by using the using keyword within your object. You'll want to use one or all of the following:

class Example
  using Refinements::StringExtensions
  using Refinements::BigDecimalExtensions
  using Refinements::ArrayExtensions
  using Refinements::HashExtensions
end

Examples

With the appropriate refinements required and used within your objects, the following sections demonstrates how each refinement enriches your objects with new capabilities.

String

example = "This is-an EXAMPLE"
example.camelcase # => "ThisIsAnExample"
example.snakecase # => "this_is_an_example"
example.titleize # => "This Is An Example"

Big Decimal

big = BigDecimal.new "5.0E-10"
big.inspect # => "#<BigDecimal:3fd3d458fe84 0.0000000005>"

Array

example = ["An", nil, "", "Example"]
example.compress # => ["An", "Example"]
example # => ["An", nil, "", "Example"]

example = ["An", nil, "", "Example"]
example.compress! # => ["An", "Example"]
example # => ["An", "Example"]

Hash

example = {a: "A", b: {one: "One", two: "Two"}}
example.deep_merge b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
example # => {a: "A", b: {one: "One", two: "Two"}}

example = {a: "A", b: {one: "One", two: "Two"}}
example.deep_merge! b: {one: 1} # => {a: "A", b: {one: 1, two: "Two"}}
example # => {a: "A", b: {one: 1, two: "Two"}}

example = {a: 1, b: 2}
example.reverse_merge a: 0, c: 3 # => {a: 1, b: 2, c: 3}
example # => {a: 1, b: 2}

example = {a: 1, b: 2}
example.reverse_merge! a: 0, c: 3 # => {a: 1, b: 2, c: 3}
example # => {a: 1, b: 2, c: 3}

Tests

To test, run:

bundle exec rake

Versioning

Read Semantic Versioning for details. Briefly, it means:

  • Patch (x.y.Z) - Incremented for small, backwards compatible bug fixes.
  • Minor (x.Y.z) - Incremented for new, backwards compatible public API enhancements and/or bug fixes.
  • Major (X.y.z) - Incremented for any backwards incompatible public API changes.

Code of Conduct

Please note that this project is released with a CODE OF CONDUCT. By participating in this project you agree to abide by its terms.

Contributions

Read CONTRIBUTING for details.

License

Copyright (c) 2015 Alchemists. Read the LICENSE for details.

History

Read the CHANGELOG for details. Built with Gemsmith.

Credits

Developed by Brooke Kuhlmann at Alchemists.