Module: Genericode::Utils Private

Defined in:
lib/genericode/utils.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Utility functions

Class Method Summary collapse

Class Method Details

.array_wrap(object) ⇒ Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wraps object in an array unless it is already an array (or array-like)

Examples:

Utils.array_wrap(nil)       # => []
Utils.array_wrap([1, 2, 3]) # => [1, 2, 3]
Utils.array_wrap(0)         # => [0]
Utils.array_wrap(foo: :bar) # => [{:foo=>:bar}]

Parameters:

  • object (Object)

Returns:

  • (Array)

    An array containing the object. If the object is nil, returns an empty array. If the object responds to ‘to_ary`, converts it to an array. Otherwise, wraps the object in an array.



23
24
25
26
27
28
29
30
31
# File 'lib/genericode/utils.rb', line 23

def self.array_wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary
  else
    [object]
  end
end

.one_or_all(array) ⇒ Object, Array

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns either the only element of the array or the array itself

Examples:

Utils.one_or_all([0])       # => 0
Utils.one_or_all([1, 2, 3]) # => [1, 2, 3]
Utils.one_or_all([])        # => []

Parameters:

  • array (Array)

Returns:

  • (Object, Array)

    The first element of the array if the array has only one element, otherwise returns the entire array.



46
47
48
# File 'lib/genericode/utils.rb', line 46

def self.one_or_all(array)
  array.one? ? array.first : array
end