Module: TieredCategoryExpressions::Generator

Defined in:
lib/tiered_category_expressions/generator.rb

Class Method Summary collapse

Class Method Details

.call(category, strict: false) ⇒ Expression?

Generates a basic TCE that matches the given category. Returns nil if no valid TCE can be generated.

Examples:

TieredCategoryExpressions::Generator.call(["Non-food", "Baby", "Baby formula"])
# => TieredCategoryExpressions::Expression[Nonfood > Baby > Baby formula]

Parameters:

  • category (Array<String>)
  • strict (Boolean) (defaults to: false)

    If true is given then the resulting TCE will not match subcategories of the given category.

Returns:



18
19
20
21
22
23
24
25
26
# File 'lib/tiered_category_expressions/generator.rb', line 18

def call(category, strict: false)
  return if category.empty?

  tiers = category.map { |t| sanitize_name(t) or return nil }
  expression = tiers.join(">")
  expression << "." if strict

  TieredCategoryExpressions::TCE(expression)
end