Sha256: f070a58a0753db59661b7df3ad633e852b5e37e42f6686097a05af30fbd93671

Contents?: true

Size: 1.39 KB

Versions: 8

Compression:

Stored size: 1.39 KB

Contents

module Paraduct
  class VariableConverter
    # @param variables [Hash{String => Array<String>}] Key: variable name, Value: values_pattern which you want to product
    # @return [Array<Hash{String => String}>]
    def self.product(variables)
      return [] if variables.empty?

      values_patterns = product_array(variables.values)

      product_variables = []
      values_patterns.each do |values_pattern|
        entry  = {}
        keys   = variables.keys.to_enum
        values = values_pattern.to_enum

        loop do
          key   = keys.next
          value = values.next
          entry[key] = value
        end

        product_variables << entry
      end
      product_variables
    end

    def self.reject(product_variables, exclude_variables)
      product_variables.inject([]) do |rejected_product_variables, variables|
        rejected_product_variables << variables unless exclude_variables.any?{ |exclude| partial_match?(variables, exclude) }
        rejected_product_variables
      end
    end

    def self.partial_match?(parent_hash, child_hash)
      child_hash.each do |k, v|
        return false unless parent_hash[k] == v
      end
      true
    end

    def self.product_array(array)
      first_values = array.shift
      if array.empty?
        first_values.product
      else
        first_values.product(*array)
      end
    end
    private_class_method :product_array
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
paraduct-1.0.0.beta6 lib/paraduct/variable_converter.rb
paraduct-1.0.0.beta5 lib/paraduct/variable_converter.rb
paraduct-1.0.0.beta4 lib/paraduct/variable_converter.rb
paraduct-1.0.0.beta3 lib/paraduct/variable_converter.rb
paraduct-1.0.0.beta2 lib/paraduct/variable_converter.rb
paraduct-1.0.0.beta1 lib/paraduct/variable_converter.rb
paraduct-0.0.3 lib/paraduct/variable_converter.rb
paraduct-0.0.3.beta2 lib/paraduct/variable_converter.rb