Module: Hackmac::Graph::Formatters
- Included in:
- Hackmac::Graph
- Defined in:
- lib/hackmac/graph/formatters.rb
Overview
A module that provides various formatting methods for converting numeric values into human-readable strings with appropriate units and display formats.
The Formatters module contains a collection of utility methods designed to transform raw numeric data into formatted strings that are more suitable for display purposes. These methods handle common formatting tasks such as converting byte measurements, frequency values, temperature readings, and percentages into clean, readable representations.
Each formatter method in this module is intended to be used with data visualization or reporting scenarios where presenting numerical information in an easily understandable format is important. The module also includes specialized functionality for deriving consistent color values based on input strings, which can be useful for maintaining visual coherence when displaying multiple data series.
Instance Method Summary collapse
-
#as_bytes(value) ⇒ String
The as_bytes method formats a numeric value into a human-readable byte representation.
-
#as_celsius(value) ⇒ String
The as_celsius method formats a temperature value with a degree symbol.
-
#as_default(value) ⇒ String
The as_default method converts a value to its string representation.
-
#as_hertz(value) ⇒ String
The as_hertz method formats a numeric value into a human-readable frequency representation.
-
#as_percent(value) ⇒ String
The as_percent method formats a numeric value as a percentage string.
-
#derive_color_from_string(string) ⇒ Integer
The derive_color_from_string method calculates a color value based on the input string by generating an MD5 hash and selecting from a predefined set of dark colors.
Instance Method Details
#as_bytes(value) ⇒ String
The as_bytes method formats a numeric value into a human-readable byte representation
This method takes a numeric input and converts it into a formatted string representing the value in bytes with appropriate binary prefixes (KB, MB, GB, etc.)
38 39 40 |
# File 'lib/hackmac/graph/formatters.rb', line 38 def as_bytes(value) Tins::Unit.format(value, prefix: :uc, format: '%.3f%U', unit: 'B') end |
#as_celsius(value) ⇒ String
The as_celsius method formats a temperature value with a degree symbol
This method takes a numeric temperature value and returns a string representation with the degree Celsius symbol appended to it
64 65 66 |
# File 'lib/hackmac/graph/formatters.rb', line 64 def as_celsius(value) "#{value}°" end |
#as_default(value) ⇒ String
The as_default method converts a value to its string representation
This method takes any input value and converts it to a string using the to_s method It serves as a fallback formatting option when no specific formatting is required
89 90 91 |
# File 'lib/hackmac/graph/formatters.rb', line 89 def as_default(value) value.to_s end |
#as_hertz(value) ⇒ String
The as_hertz method formats a numeric value into a human-readable frequency representation
This method takes a numeric input and converts it into a formatted string representing the value in hertz with appropriate metric prefixes (kHz, MHz, GHz, etc.)
52 53 54 |
# File 'lib/hackmac/graph/formatters.rb', line 52 def as_hertz(value) Tins::Unit.format(value, prefix: :uc, format: '%.3f%U', unit: 'Hz') end |
#as_percent(value) ⇒ String
The as_percent method formats a numeric value as a percentage string
This method takes a numeric input and returns a string representation with a percent sign appended to it, providing a simple way to format numeric values as percentages for display purposes
77 78 79 |
# File 'lib/hackmac/graph/formatters.rb', line 77 def as_percent(value) "#{value}%" end |
#derive_color_from_string(string) ⇒ Integer
The derive_color_from_string method calculates a color value based on the input string by generating an MD5 hash and selecting from a predefined set of dark colors
100 101 102 103 104 105 106 107 |
# File 'lib/hackmac/graph/formatters.rb', line 100 def derive_color_from_string(string) cs = (21..226).select { |d| Term::ANSIColor::Attribute[d].to_rgb_triple.to_hsl_triple. lightness < 40 } s = Digest::MD5.digest(string).unpack('Q*') cs[ s.first % cs.size ] end |