class ViewModel::Utils

Public Class Methods

array_like?(obj) click to toggle source

Cover arrays and also Rails' array-like types.

# File lib/view_model/utils.rb, line 20
def array_like?(obj)
  obj.respond_to?(:to_ary)
end
map_one_or_many(obj) { |x| ... } click to toggle source
# File lib/view_model/utils.rb, line 11
def map_one_or_many(obj)
  if array_like?(obj)
    obj.map { |x| yield(x) }
  else
    yield(obj)
  end
end
wrap_one_or_many(obj) { |wrap| ... } click to toggle source
# File lib/view_model/utils.rb, line 5
def wrap_one_or_many(obj)
  return_array = array_like?(obj)
  results = yield(Array.wrap(obj))
  return_array ? results : results.first
end