Class: CutePrint::Labeler Private

Inherits:
Object show all
Defined in:
lib/cute_print/labeler.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(format, width, label, value) ⇒ Labeler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Labeler.

API:

  • private



12
13
14
15
16
17
# File 'lib/cute_print/labeler.rb', line 12

def initialize(format, width, label, value)
  @format = format
  @width = width
  @label = label
  @value = value
end

Class Method Details

.label(format, width, label, value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



8
9
10
# File 'lib/cute_print/labeler.rb', line 8

def self.label(format, width, label, value)
  new(format, width, label, value).labeled
end

Instance Method Details

#labeledObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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