class CutePrint::Labeler
@api private
Public Class Methods
label(format, width, label, value)
click to toggle source
# File lib/cute_print/labeler.rb, line 8 def self.label(format, width, label, value) new(format, width, label, value).labeled end
new(format, width, label, value)
click to toggle source
# File lib/cute_print/labeler.rb, line 12 def initialize(format, width, label, value) @format = format @width = width @label = label @value = value end
Public Instance Methods
labeled()
click to toggle source
# File lib/cute_print/labeler.rb, line 19 def labeled # Optimization: Avoid the computation of "outlined", and the # elaborate comparison, in many cases. return outlined if outlined_fits_on_one_line? inlined_fits = lines_fit_horizontally?(inlined) outlined_fits = lines_fit_horizontally?(outlined) if inlined_fits && outlined_fits [inlined, outlined].min_by do |lines| [ lines.size, lines.equal?(outlined) ? 0 : 1, ] end elsif inlined_fits && !outlined_fits inlined elsif !inlined_fits && outlined_fits outlined else # neither fits [inlined, outlined].min_by do |lines| [ lines.map(&:size).min, lines.size, ] end end end
Private Instance Methods
inlined()
click to toggle source
# File lib/cute_print/labeler.rb, line 66 def inlined @inlined ||= InlineLabeler.label(@format, @width, @label, @value) end
line_fits_horizontally?(line)
click to toggle source
# File lib/cute_print/labeler.rb, line 54 def line_fits_horizontally?(line) line.chomp.size <= @width end
lines_fit_horizontally?(lines)
click to toggle source
# File lib/cute_print/labeler.rb, line 48 def lines_fit_horizontally?(lines) lines.all? do |line| line_fits_horizontally?(line) end end
outlined()
click to toggle source
# File lib/cute_print/labeler.rb, line 62 def outlined @outlined ||= OutlineLabeler.label(@format, @width, @label, @value) end
outlined_fits_on_one_line?()
click to toggle source
# File lib/cute_print/labeler.rb, line 58 def outlined_fits_on_one_line? outlined.size == 1 && line_fits_horizontally?(outlined.first) end