Class: Paraduct::VariableConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/paraduct/variable_converter.rb

Class Method Summary collapse

Class Method Details

.partial_match?(parent_hash, child_hash) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/paraduct/variable_converter.rb', line 34

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

.product(variables) ⇒ Array<Hash{String => String}>

Parameters:

  • variables (Hash{String => Array<String>})

    Key: variable name, Value: values_pattern which you want to product

Returns:

  • (Array<Hash{String => String}>)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/paraduct/variable_converter.rb', line 5

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

.reject(product_variables, exclude_variables) ⇒ Object



27
28
29
30
31
32
# File 'lib/paraduct/variable_converter.rb', line 27

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