Tools

A library of utility services and concerns.

Class: SleepingKingStudios::Tools::ObjectTools

Parent Namespace
SleepingKingStudios::Tools
Inherited Classes
SleepingKingStudios::Tools::Base > Object
Defined In
lib/sleeping_king_studios/tools/object_tools.rb

Table Of Contents

Overview

Low-level tools for working with objects.

Back To Top

Class Methods

.instance => SleepingKingStudios::Tools::Base (deprecated)

Inherited From
SleepingKingStudios::Tools::Base

Deprecated. v1.3.0 Use Toolbelt.instance instead.

Returns

Back To Top

Constructor

#initialize(toolbelt: nil) => Base

Inherited From
SleepingKingStudios::Tools::Base

Parameters

Returns

Back To Top

Instance Methods

#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.

Examples

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.

Parameters

Returns

#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.

Examples

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' }

Parameters

Returns

See Also

#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.

Examples

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

Parameters

Returns

#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.

Examples

ObjectTools.dig(my_object, :first_method, :second_method, :third_method)
#=> my_object.first_method.second_method.third_method

Parameters

Returns

#eigenclass(obj) => Class (deprecated)

Also known as: metaclass

Deprecated. v1.3.0 Use Object#singleton_class instead.

Returns the object’s eigenclass.

Parameters

Returns

#fetch(obj, key, default = nil, indifferent_key: false) => Object
#fetch(obj, key, indifferent_key: false, &default) => Object

Overloads

#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.

Parameters
  • obj (Object) — the baseobject.
  • key (Object) — the key to retrieve.
  • indifferent_key (true, false) — if true and the key is a String or a Symbol, tries to match both the String and Symbol equivalent. Defaults to false.
  • default (Object) — the default value.
Returns
  • (Object) — the value at the specified key or index.
Raises
  • (IndexError, KeyError, NameError) — if the object does not have a value for the requested key or index and there is no default value.
#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.

Parameters
  • obj (Object) — the baseobject.
  • key (Object) — the key to retrieve.
  • indifferent_key (true, false) — if true and the key is a String or a Symbol, tries to match both the String and Symbol equivalent. Defaults to false.
Yields
  • generates the default value if there is no value at the key.
Yield Parameters
  • key (Object) — the requested key.
Yield Returns
  • (Object) — the default value.
Returns
  • (Object) — the value at the specified key or index.
Raises
  • (IndexError, KeyError, NameError) — if the object does not have a value for the requested key or index and there is no default value.

#immutable?(obj) => Boolean

Checks if the object is immutable.

Examples

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

Parameters

Returns

See Also

#mutable?(obj) => Boolean

Checks if the object is mutable.

Parameters

Returns

See Also

#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.

Examples

ObjectTools.object?(nil)
#=> true

ObjectTools.object?([])
#=> true

ObjectTools.object?({})
#=> true

ObjectTools.object?(1)
#=> true

ObjectTools.object?(BasicObject.new)
#=> false

Parameters

Returns

#toolbelt => SleepingKingStudios::Tools::Toolbelt

Also known as: tools

Inherited From
SleepingKingStudios::Tools::Base

Returns

#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.

Examples

ObjectTools.try(%w(ichi ni san), :count)
#=> 3

ObjectTools.try(nil, :count)
#=> nil

Parameters

Returns

Back To Top


Back to Documentation | Reference | SleepingKingStudios | SleepingKingStudios::Tools