Sha256: d15ea291a4cc52a7b9bbeabd02be62cbd34c8c94bcddc868d6271cda9df2eca9
Contents?: true
Size: 1.36 KB
Versions: 2
Compression:
Stored size: 1.36 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.each_with_object([]) do |variables, rejected_product_variables| rejected_product_variables << variables unless exclude_variables.any? { |exclude| partial_match?(variables, exclude) } 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
paraduct-1.0.1 | lib/paraduct/variable_converter.rb |
paraduct-1.0.0 | lib/paraduct/variable_converter.rb |