Class: ViewModel::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/utils.rb,
lib/view_model/utils/collections.rb

Defined Under Namespace

Modules: Collections

Class Method Summary collapse

Class Method Details

.array_like?(obj) ⇒ Boolean

Cover arrays and also Rails’ array-like types.

Returns:

  • (Boolean)


20
21
22
# File 'lib/view_model/utils.rb', line 20

def array_like?(obj)
  obj.respond_to?(:to_ary)
end

.map_one_or_many(obj) ⇒ Object



11
12
13
14
15
16
17
# 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) ⇒ Object



5
6
7
8
9
# 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