Module: Karafka::Web::Ui::Helpers::FormattingHelper
- Included in:
- Base
- Defined in:
- lib/karafka/web/ui/helpers/formatting_helper.rb
Overview
Generic value, number and text formatting helpers used across the Web UI
Instance Method Summary collapse
-
#format_memory(mem_kb) ⇒ String
Formatted memory usage.
-
#number_with_delimiter(number, delimiter = ",") ⇒ String
Converts number to a more friendly delimiter based version.
-
#object_value_to_s(object) ⇒ String
Converts object into a string and for objects that would anyhow return their stringified instance value, it replaces it with the class name instead.
-
#truncate(string, length: 50, omission: "...", strategy: :default) ⇒ String
Truncates given text if it is too long and wraps it with a title with full text.
Instance Method Details
#format_memory(mem_kb) ⇒ String
Returns formatted memory usage.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 21 def format_memory(mem_kb) return "0" if !mem_kb || mem_kb.zero? if mem_kb < 10_240 "#{number_with_delimiter(mem_kb.round(4))} KB" elsif mem_kb < 1_000_000 "#{number_with_delimiter((mem_kb / 1024.0).to_i)} MB" else "#{number_with_delimiter((mem_kb / (1024.0 * 1024.0)).round(1))} GB" end end |
#number_with_delimiter(number, delimiter = ",") ⇒ String
Converts number to a more friendly delimiter based version
37 38 39 40 41 42 43 |
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 37 def number_with_delimiter(number, delimiter = ",") return "" unless number parts = number.to_s.to_str.split(".") parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") parts.join(".") end |
#object_value_to_s(object) ⇒ String
Converts object into a string and for objects that would anyhow return their stringified instance value, it replaces it with the class name instead. Useful for deserializers, etc presentation.
15 16 17 |
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 15 def object_value_to_s(object) object.to_s.include?("#<") ? object.class.to_s : object.to_s end |
#truncate(string, length: 50, omission: "...", strategy: :default) ⇒ String
Truncates given text if it is too long and wraps it with a title with full text. Can use a middle-based strategy that keeps beginning and ending of a string instead of keeping just the beginning.
The :middle strategy is useful when we have strings such as really long process names
that have important beginning and end but middle can be removed without risk of not
allowing user to recognize the content.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/karafka/web/ui/helpers/formatting_helper.rb', line 58 def truncate(string, length: 50, omission: "...", strategy: :default) return string if string.length <= length case strategy when :default truncated = string[0...(length - omission.length)] + omission when :middle part_length = (length - omission.length) / 2 truncated = string[0...part_length] + omission + string[-part_length..] else raise Karafka::Errors::UnsupportedCaseError, "Unknown strategy: #{strategy}" end %(<span title="#{string}">#{truncated}</span>) end |