Sha256: af3e464f98bf990743c5a8d8fd8a0834dd69bd29eaf6cde1d0cad3ae91e06e36

Contents?: true

Size: 1.35 KB

Versions: 4

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

require 'json'
require 'yaml'
require 'awesome_print'

module Rika
  # This module manages the formatters used to format the output of the Rika command line application.
  class Formatters
    AWESOME_PRINT_FORMATTER = ->(object) { object.ai }
    INSPECT_FORMATTER       = ->(object) { object.inspect }
    JSON_FORMATTER          = ->(object) { object.to_json }
    PRETTY_JSON_FORMATTER   = ->(object) { JSON.pretty_generate(object) }
    TO_S_FORMATTER          = ->(object) { object.to_s }
    YAML_FORMATTER          = ->(object) { object.to_yaml }

    # A hash of formatters, keyed by the format character.
    # The value is a lambda that takes the object to be formatted as a parameter.
    # @return [Hash] the hash of formatters
    FORMATTER_LOOKUP_TABLE = {
      'a' => AWESOME_PRINT_FORMATTER,
      'i' => INSPECT_FORMATTER,
      'j' => JSON_FORMATTER,
      'J' => PRETTY_JSON_FORMATTER,
      't' => TO_S_FORMATTER,
      'y' => YAML_FORMATTER
    }.freeze

    VALID_OPTION_CHARS = FORMATTER_LOOKUP_TABLE.keys

    # Gets the formatter lambda for the given option character.
    # @param [String] option_char the option character
    # @return [Lambda] the formatter lambda
    # @raise [KeyError] if any option character is invalid
    def self.get(option_char)
      FORMATTER_LOOKUP_TABLE.fetch(option_char)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rika-2.0.3-java lib/rika/formatters.rb
rika-2.0.2-java lib/rika/formatters.rb
rika-2.0.1-java lib/rika/formatters.rb
rika-2.0.0-java lib/rika/formatters.rb