Sha256: 1559f536c59fbc125d05f3a62628aabfae5db49da99ba2eee02c62e773705981

Contents?: true

Size: 845 Bytes

Versions: 11

Compression:

Stored size: 845 Bytes

Contents

# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module UnitMeasurements
  module Formatter
    # The default format for formatting measurements.
    DEFAULT_FORMAT = "%.2<quantity>f %<unit>s".freeze

    # Formats measurement to certain formatted string specified by +format+.
    # If +format+ is not specified, it uses +DEFAULT_FORMAT+ for
    # formatting the measurement
    #
    # @example
    #   UnitMeasurements::Weight.parse("2 kg").to("st").format
    #   => "0.31 st"
    #   UnitMeasurements::Weight.parse("2 kg").to("st").format("%.4<quantity>f %<unit>s")
    #   => "0.3149 st"
    #
    # @param [String] format
    #
    # @return [String]
    def format(format = nil)
      kwargs = {quantity: quantity, unit: unit.to_s}

      (format || DEFAULT_FORMAT) % kwargs
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
unit_measurements-4.9.0 lib/unit_measurements/formatter.rb
unit_measurements-4.8.0 lib/unit_measurements/formatter.rb
unit_measurements-4.7.0 lib/unit_measurements/formatter.rb
unit_measurements-4.6.0 lib/unit_measurements/formatter.rb
unit_measurements-4.5.0 lib/unit_measurements/formatter.rb
unit_measurements-4.4.0 lib/unit_measurements/formatter.rb
unit_measurements-4.3.0 lib/unit_measurements/formatter.rb
unit_measurements-4.2.0 lib/unit_measurements/formatter.rb
unit_measurements-4.1.0 lib/unit_measurements/formatter.rb
unit_measurements-4.0.0 lib/unit_measurements/formatter.rb
unit_measurements-2.6.1 lib/unit_measurements/formatter.rb