Class: Ace::B36ts::Commands::EncodeCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/b36ts/commands/encode_command.rb

Overview

Command to encode a timestamp to a compact ID

Examples:

Usage

EncodeCommand.execute("2025-01-06 12:30:00")
# => "i50jj3"

Class Method Summary collapse

Class Method Details

.execute(time_string, options = {}) ⇒ Integer

Execute the encode command

Parameters:

  • time_string (String)

    Time string to encode (various formats supported)

  • options (Hash) (defaults to: {})

    Command options

Options Hash (options):

  • :year_zero (Integer)

    Override year_zero config

  • :format (String)

    Output format

  • :quiet (Boolean)

    Suppress config summary output

Returns:

  • (Integer)

    Exit code (0 for success, 1 for error)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ace/b36ts/commands/encode_command.rb', line 25

def execute(time_string, options = {})
  time = parse_time(time_string)
  config = Molecules::ConfigResolver.resolve(options)

  display_config_summary("encode", config, options)

  # Validate mutually exclusive options
  if options[:split]
    if options[:format]
      raise ArgumentError, "--split and --format are mutually exclusive"
    end
    if options[:count]
      raise ArgumentError, "--count and --split are mutually exclusive"
    end

    levels = parse_split_levels(options[:split])
    output = Atoms::CompactIdEncoder.encode_split(
      time,
      levels: levels,
      year_zero: config[:year_zero],
      alphabet: config[:alphabet]
    )

    if options[:path_only]
      puts output[:path]
    elsif options[:json]
      puts JSON.generate(output.transform_keys(&:to_s))
    else
      display_split_output(levels, output)
    end
  else
    # Get format from options or config (default: :"2sec")
    # Normalize hyphens to underscores for CLI compatibility (e.g., high-8 -> high_8)
    format = options[:format]
    format = format.to_s.tr("-", "_").to_sym if format
    format ||= config[:default_format]&.to_sym || :"2sec"

    if options[:count]
      count = options[:count].to_i
      ids = Atoms::CompactIdEncoder.encode_sequence(
        time,
        count: count,
        format: format,
        year_zero: config[:year_zero],
        alphabet: config[:alphabet]
      )

      if options[:json]
        puts JSON.generate(ids)
      else
        ids.each { |id| puts id }
      end
    else
      compact_id = Atoms::CompactIdEncoder.encode_with_format(
        time,
        format: format,
        year_zero: config[:year_zero],
        alphabet: config[:alphabet]
      )

      puts compact_id
    end
  end
  0
rescue ArgumentError => e
  warn "Error: #{e.message}"
  raise
rescue => e
  warn "Error encoding timestamp: #{e.message}"
  warn e.backtrace.first(5).join("\n") if Ace::B36ts.debug?
  raise
end