module Expandable
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 }]
Public Instance Methods
expand()
click to toggle source
Returns an array composed of all possible variation of the object by combining all the items of it’s array values.
@return [Array]
# 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
Private Instance Methods
expand_array(key)
click to toggle source
# File lib/core_ext.rb, line 31 def expand_array(key) orig = @expand_res @expand_res = [] fetch(key).each do |value| @expand_res += orig.map { |hash| hash.merge(key => value) } end end
expand_hash(key)
click to toggle source
# File lib/core_ext.rb, line 39 def expand_hash(key) orig = @expand_res @expand_res = [] fetch(key).expand.each do |value| @expand_res += orig.map { |hash| hash.merge(key => value) } end end