A library of utility services and concerns.
The core feature of SleepingKingStudios::Tools is the toolbelt, which provides a set of functional tools for operating on Ruby objects, asserting on values, generating human-readable messages, and more.
Unlike other solutions such as ActiveSupport which patch core classes, SleepingKingStudios::Tools uses a functional style inspired by libraries like lodash.js.
While individual tools can be initialized separately, the intended entry point is the toolbelt, an instance of SleepingKingStudios::Tools::Toolbelt. A toolbelt (lazily) references its tools as needed and ensures that cross-dependencies between tools are handled consistently.
toolbelt = SleepingKingStudios::Tools::Toolbelt.instance
# Accessing String tools.
toolbelt.string_tools.camelize('space_program') #=> 'Space Program'
# Accessing Object tools.
toolbelt.object_tools.immutable?(Object.new) #=> false
toolbelt.object_tools.immutable?(Object.new.freeze) #=> true
For a full list of tools, see Defined Tools below, or Reference.
The Toolbelt.instance class method returns a toolbelt singleton.
module Space::LaunchRocket
def call(rocket_name, **options)
rocket_name = tools.string_tools.camelize(rocket_name)
rocket_class = Object.const_get(rocket_name)
rocket_class.new(**options).launch
end
private
def tools = SleepingKingStudios::Tools::Toolbelt.instance
end
Above, we are defining a private #tools method that returns the Toolbelt.instance singleton. We then use the tools.string_tools helper to convert a string to CamelCase, allowing us to use it as a class name.
By default, the Toolbelt.instance method returns the value of Toolbelt.global, a singleton with default configuration that is thread-safe (ensuring all calls to Toolbelt.global return the same object).
However, Toolbelt.instance is intended to be overwritten by the end user (at the application level, not by individual libraries or dependencies). For example, an application might define a custom inflector, or define its own rules for handling deprecated code. Overwriting Toolbelt.instance ensures that custom configuration will be used in every toolbelt reference, even in code that does not reference the application directly.
Each toolbelt defines the following tools:
array_toolsTools for working with Arrays and Array-like objects (countable, enumerable, indexed collections).
ArrayTools methods include:
#deep_dup(ary)#deep_freeze(ary)#fetch(ary, index, default = nil, &default_block)#humanize_list(ary)For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Methods for asserting on the type or content of values. See Assertions documentation.
Assertions methods include:
#assert_boolean(value)true or false.
#assert_exclusion(value, expected:)#assert_inclusion(value, expected:)#assert_instance_of(value, expected:)#assert_name(value)String or Symbol.
#assert_presence(value)nil or #empty?.
For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Tools for working with an application or working environment.
CoreTools methods include:
#deprecate(details, message: nil)To configure the deprecation strategy, set ENV['DEPRECATION_STRATEGY'] to one of the following:
'ignore' will silently ignore deprecations.'raise' will raise a DeprecationError for each deprecation.'warn' will print a deprecation warning using Kernel.warn.For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Tools for working with Hashes and Hash-like objects (enumerable key-value collections).
HashTools methods include:
#convert_keys_to_strings(hsh)Strings.
#convert_keys_to_symbols(hsh)Symbols.
#deep_dup(hsh)#deep_freeze(hsh)#fetch(hsh, key, default = nil, &default_block)For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Tools for working with Integer values.
IntegerTools methods include:
#pluralize(count, single, plural = nil)#romanize(int)For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Utility for generating configured, user-readable output strings. See Messages documentation.
Messages methods include:
#message(key, parameters: {}, scope: nil, **)For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Low-level tools for working with objects.
ObjectTools methods include:
#deep_dup(obj)#deep_freeze(obj)#deep_freeze(obj)#dig(obj, *method_names, indifferent_keys: false)#fetch(obj, key_or_index, default = nil, indifferent_key: false, &block)For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Tools for working with Strings.
StringTools methods include:
#camelize(str)#indent(str, count = 2)#pluralize(str)#singularize(str)#underscore(str)For a full list of methods, see Reference.
Back to Defined Tools | Back to Top
Back to Documentation