Module: Yummi::Formatters
- Defined in:
- lib/yummi/formatters.rb
Overview
A module with useful formatters
Constant Summary collapse
- BYTE_MODES =
Defines the modes to format a byte value
{ :iec => { :range => %w{B KiB MiB GiB TiB PiB EiB ZiB YiB}, :step => 1024 }, :si => { :range => %w{B KB MB GB TB PB EB ZB YB}, :step => 1000 } }
Class Method Summary collapse
-
.boolean(params = {}) ⇒ Object
A formatter for boolean values that uses ‘Yes’ or ‘No’ by default.
-
.byte(params = {}) ⇒ Object
Formats a byte value to ensure easily reading.
-
.numeric(params) ⇒ Object
A formatter for numeric values.
-
.percentage(precision = 3) ⇒ Object
A formatter for percent values.
-
.round(precision) ⇒ Object
A formatter to round float values.
-
.with(format) ⇒ Object
A formatter that uses the given format.
Class Method Details
.boolean(params = {}) ⇒ Object
A formatter for boolean values that uses ‘Yes’ or ‘No’ by default
Hash Args
:if_true => String to use when value is true :if_false => String to use when value is false
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/yummi/formatters.rb', line 52 def self.boolean params = {} Yummi::to_format do |ctx| value = ctx.value if value.to_s.downcase == 'true' (params[:if_true] or 'Yes') else (params[:if_false] or 'No') end end end |
.byte(params = {}) ⇒ Object
Formats a byte value to ensure easily reading
Hash Args
precision
-
How many decimal digits should be displayed. (Defaults to 1)
mode
-
Which mode should be used to display unit symbols. (Defaults to :iec)
See #BYTE_MODES
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/yummi/formatters.rb', line 142 def self.byte params = {} Yummi::to_format do |ctx| value = ctx.value value = value.to_i if value.is_a? String mode = (params[:mode] or :iec) range = BYTE_MODES[mode][:range] step = BYTE_MODES[mode][:step] params[:precision] ||= 1 result = value range.each_index do |i| minimun = (step.** i) result = "%.#{params[:precision]}f #{range[i]}" % (value.to_f / minimun) if value >= minimun end result end end |
.numeric(params) ⇒ Object
A formatter for numeric values
Hash Args
:negative => format to use when value is negative :zero => format to use when value is zero :positive => format to use when value is positive :any => format to use for any value without a specific format
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yummi/formatters.rb', line 89 def self.numeric params Yummi::to_format do |ctx| value = ctx.value negative = (params[:negative] or params[:any]) positive = (params[:positive] or params[:any]) zero = (params[:zero] or params[:any]) if negative and value < 0 negative % value.abs elsif positive and value > 0 positive % value elsif zero and value == 0 zero % value end end end |
.percentage(precision = 3) ⇒ Object
A formatter for percent values.
Parameters:
The precision to use (defaults to 3)
111 112 113 114 115 116 |
# File 'lib/yummi/formatters.rb', line 111 def self.percentage precision = 3 Yummi::to_format do |ctx| value = ctx.value "%.#{precision}f%%" % (value * 100) end end |