Class: AWSCronParser::Describer::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_cron_parser/describer/template.rb

Overview

Simple template interpolator for CRON descriptions

Constant Summary collapse

TEMPLATES =
YAML.load_file(File.join(__dir__, 'template.en.yml')).freeze

Class Method Summary collapse

Class Method Details

.allObject

Get all templates (for debugging)



48
49
50
# File 'lib/aws_cron_parser/describer/template.rb', line 48

def all
  TEMPLATES
end

.get(path) ⇒ Object

Get template by path (dot notation) Example: get("time.every_n_minutes") => "Every step minutestep_plural"



24
25
26
# File 'lib/aws_cron_parser/describer/template.rb', line 24

def get(path)
  path.to_s.split('.').reduce(TEMPLATES) { |hash, key| hash&.[](key) }
end

.interpolate(template_path, **variables) ⇒ Object

Main interpolation method with auto-pluralization Example: interpolate("time.every_n_minutes", step: 5) Returns: "Every 5 minutes"



15
16
17
18
19
20
# File 'lib/aws_cron_parser/describer/template.rb', line 15

def interpolate(template_path, **variables)
  template = get(template_path)
  return nil if template.nil?

  render(template, auto_pluralize(template, variables))
end

.ordinal_word(number) ⇒ Object

Get ordinal word (first, second, third, etc.)



43
44
45
# File 'lib/aws_cron_parser/describer/template.rb', line 43

def ordinal_word(number)
  ORDINAL_WORDS[number] || "#{number}th"
end

.plural_for(count) ⇒ Object

Simple plural helper



38
39
40
# File 'lib/aws_cron_parser/describer/template.rb', line 38

def plural_for(count)
  count == 1 ? '' : 's'
end

.render(template, variables = {}) ⇒ Object

Render template string with variables (faster, direct replacement) Example: render("Every step minutes", step: 5) => "Every 5 minutes"



30
31
32
33
34
35
# File 'lib/aws_cron_parser/describer/template.rb', line 30

def render(template, variables = {})
  template.to_s.gsub(/\{(\w+)\}/) do
    variables[::Regexp.last_match(1).to_sym] || variables[::Regexp.last_match(1)] ||
      "{#{::Regexp.last_match(1)}}"
  end
end