module Evrone module CI class Router class BuildMatrix KEYS = (Travis::LANGS + %w{ env }).freeze attr_reader :travis def initialize(travis) @travis = travis end def keys extract_pair_of_key_and_values.map(&:first).sort end def travises attributes_for_new_travises.map do |attrs| Travis.new attrs end end def attributes_for_new_travises permutate_and_build_values.inject([]) do |ac, values| ac << values.inject({}) do |a,val| a[val.key] = val.value a end ac end end def permutate_and_build_values values = extract_pair_of_key_and_values.map do |key, vals| vals.map{|it| Value.new(key, it) } end if matrix_values?(values) array_permutations(values).map do |it| if it.is_a?(Array) it.flatten else [it] end end else values end.sort_by(&:to_s) end def extract_pair_of_key_and_values KEYS.map.inject([]) do |a, k| if (val = travis[k]) && !val.empty? a << [k, val] end a end end private def matrix_values?(values) !values.all?{|i| i.size == 1 } end def array_permutations array, index=0 # index is 0 by default : start at the beginning, more elegant. return array[-1] if index == array.size - 1 # Return last element if at end. result = [] array[index].each do |element| # For each array array_permutations(array, index + 1).each do |x| # Permute permute permute result << [element, x] end end result end Value = Struct.new(:key, :value) do def to_s [key, value].join(":") end end end end end end