Sha256: 9d799331fa9b5f29dafc20149417c563628b985737e144ca61fcb067e03e4e95

Contents?: true

Size: 659 Bytes

Versions: 2

Compression:

Stored size: 659 Bytes

Contents

# frozen_string_literal: true

# ObjectIdentifier::ArrayWrap mirrors the implementation of Rails'
# Array.wrap method. This allows us to get around objects that respond to
# `to_a` (such as Struct) and, instead, either utilize `to_ary` or just
# actually wrap the object in an Array ourselves.
module ObjectIdentifier::ArrayWrap
  # :reek:NilCheck
  # :reek:ManualDispatch

  # Dispatch "Array Wrapping" logic on the given `object`.
  #
  # @param object The object(s) to attempt to Array wrap.
  def self.call(object)
    if object.nil?
      []
    elsif object.respond_to?(:to_ary)
      object.to_ary || [object]
    else
      [object]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
object_identifier-0.9.0 lib/object_identifier/array_wrap.rb
object_identifier-0.8.0 lib/object_identifier/array_wrap.rb