Module: Kaal::CronHumanizer

Defined in:
lib/kaal/utils/cron_humanizer.rb

Overview

Human-friendly cron phrase generation with i18n support.

Constant Summary collapse

I18N_LOAD_MUTEX =
Mutex.new
MACRO_PHRASES =
{
  '@yearly' => 'phrases.yearly',
  '@monthly' => 'phrases.monthly',
  '@weekly' => 'phrases.weekly',
  '@daily' => 'phrases.daily',
  '@hourly' => 'phrases.hourly'
}.freeze

Class Method Summary collapse

Class Method Details

.to_human(expression, locale: nil) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/kaal/utils/cron_humanizer.rb', line 26

def to_human(expression, locale: nil)
  ensure_i18n_loaded!

  normalized = CronUtils.safe_normalize_expression(expression)
  raise ArgumentError, CronUtils.invalid_expression_error_message('') unless normalized
  raise ArgumentError, CronUtils.invalid_expression_error_message(normalized) if normalized.empty?

  resolved_locale = locale || I18n.locale
  I18n.with_locale(resolved_locale) do
    humanized = humanize_expression(normalized)
    return humanized unless humanized.to_s.strip.empty?

    translate_phrase('phrases.cron_expression', expression: normalized)
  end
end