A library of utility services and concerns.
Low-level tools for working with objects.
.instance => SleepingKingStudios::Tools::Base (deprecated)
Deprecated. v1.3.0 Use Toolbelt.instance instead.
#initialize(toolbelt: nil) => Base
#apply(receiver, proc, *args, **kwargs, &block) => Object
Calls a Proc or lambda on the given receiver with the given parameters.
Unlike calling #instance_exec with the block, ObjectTools#apply allows you to specify a block parameter.
my_object = double('object', :to_s => 'A mock object')
my_proc = ->() { puts %{#{self.to_s} says "Greetings, programs!"} }
ObjectTools.apply my_object, my_proc
#=> Writes 'A mock object says "Greetings, programs!"' to STDOUT.#deep_dup(obj) =>
Creates a deep copy of the object.
If the object is an Array, returns a new Array with deep copies of each array item. If the object is a Hash, returns a new Hash with deep copies of each hash key and value. Otherwise, returns Object#dup.
data = {
:songs => [
{
:name => 'Welcome to the Jungle',
:artist => "Guns N' Roses",
:album => 'Appetite for Destruction'
},
{
:name => 'Hells Bells',
:artist => 'AC/DC',
:album => 'Back in Black'
},
{
:name => "Knockin' on Heaven's Door",
:artist => 'Bob Dylan',
:album => 'Pat Garrett & Billy The Kid'
}
]
}
copy = ObjectTools.deep_dup data
copy[:songs] << { :name => 'Sympathy for the Devil', :artist => 'The Rolling Stones', :album => 'Beggars Banquet' }
data[:songs].count
#=> 3
copy[:songs][1][:name] = 'Shoot to Thrill'
data[:songs][1]
#=> { :name => 'Hells Bells', :artist => 'AC/DC', :album => 'Back in Black' }#deep_freeze(obj) => Object
Performs a deep freeze of the object.
If the object is an Array, freezes the array and performs a deep freeze on each array item. If the object is a hash, freezes the hash and performs a deep freeze on each hash key and value. Otherwise, calls Object#freeze.
data = {
:songs => [
{
:name => 'Welcome to the Jungle',
:artist => "Guns N' Roses",
:album => 'Appetite for Destruction'
},
{
:name => 'Hells Bells',
:artist => 'AC/DC',
:album => 'Back in Black'
},
{
:name => "Knockin' on Heaven's Door",
:artist => 'Bob Dylan',
:album => 'Pat Garrett & Billy The Kid'
}
]
}
ObjectTools.deep_freeze(data)
data.frozen?
#=> true
data[:songs].frozen?
#=> true
data[:songs][0].frozen?
#=> true
data[:songs][0].name.frozen?
#=> true#dig(obj, *method_names, indifferent_keys: false) => Object, nil
Accesses deeply nested properties on an object.
This method finds the first named property on the given object, and then each subsequent property on the result of the previous call.
Using #[] access allows digging through Array (using integer indices) and Hash data structures as well as object methods.
ObjectTools.dig(my_object, :first_method, :second_method, :third_method)
#=> my_object.first_method.second_method.third_method#eigenclass(obj) => Class (deprecated)
Also known as:
metaclass
Deprecated. v1.3.0 Use Object#singleton_class instead.
Returns the object’s eigenclass.
#fetch(obj, key, default = nil, indifferent_key: false) => Object#fetch(obj, key, indifferent_key: false, &default) => Object#fetch(obj, key, default = nil, indifferent_key: false) => Object
Retrieves the value at the specified method, key, or index.
If the value does not exist, returns the default value, or raises am exception if there is no default value. If the object defines a native #fetch method, delegates to the native implementation.
#fetch(obj, key, indifferent_key: false, &default) => Object
Retrieves the value at the specified method, key, or index.
If the value does not exist, returns the default value, or raises am exception if there is no default value. If the object defines a native #fetch method, delegates to the native implementation.
#immutable?(obj) => Boolean
Checks if the object is immutable.
ObjectTools.immutable?(nil)
#=> true
ObjectTools.immutable?(false)
#=> true
ObjectTools.immutable?(0)
#=> true
ObjectTools.immutable?(:hello)
#=> true
ObjectTools.immutable?('Greetings, programs!')
#=> true
ObjectTools.immutable?(+'Greetings, programs!')
#=> false
ObjectTools.immutable?([1, 2, 3])
#=> false
ObjectTools.immutable?([1, 2, 3].freeze)
#=> false#mutable?(obj) => Boolean
Checks if the object is mutable.
#object?(obj) => Boolean
Returns true if the object is an Object.
This should return false only for objects that have an alternate inheritance chain from BasicObject, such as a Proxy.
ObjectTools.object?(nil)
#=> true
ObjectTools.object?([])
#=> true
ObjectTools.object?({})
#=> true
ObjectTools.object?(1)
#=> true
ObjectTools.object?(BasicObject.new)
#=> false#toolbelt => SleepingKingStudios::Tools::Toolbelt
Also known as:
tools
#try(obj, method_name, *args) => Object, nil (deprecated)
Deprecated. v1.3.0 Use the safe access operator &. instead.
As #send, but returns nil if the object does not respond to the method.
This method relies on #respond_to?, so methods defined with method_missing will not be called.
ObjectTools.try(%w(ichi ni san), :count)
#=> 3
ObjectTools.try(nil, :count)
#=> nilBack to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools