Module: Expandable

Included in:
Hash
Defined in:
lib/core_ext.rb

Overview

Mixin for combining variations

{ a: :b }.expand       #=> [{ a: :b }]
{ a: [:b, :c] }.expand #=> [{ a: :b }, { a: :c }]
{
  a: [:b, :c],
  d: [:e, :f],
}.expand
#=> [{ a: :b, d: :e }, {a: :b, d: :f}, { a: :c, d: :e }, { a: :c, d: :f }]

Instance Method Summary collapse

Instance Method Details

#expandArray

Returns an array composed of all possible variation of the object by combining all the items of it’s array values.

Returns:



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/core_ext.rb', line 17

def expand
  @expand_res = [{}]
  each do |key, value|
    case value
    when Array then expand_array(key)
    when Hash then expand_hash(key)
    else @expand_res.map! { |hash| hash.merge(key => value) }
    end
  end
  @expand_res
end