Class: Layouter::Leaf::Annotation

Inherits:
Base show all
Defined in:
lib/layouter/leaf/annotation.rb

Constant Summary collapse

ALMOST =
"~"
ELLIPSIS =
"\u2026"

Constants inherited from Base

Base::EPS, Base::INF

Instance Attribute Summary collapse

Attributes inherited from Base

#importance

Attributes inherited from Element

#calculated_height, #calculated_width

Instance Method Summary collapse

Methods inherited from Base

#layout

Methods inherited from Element

#layout?

Constructor Details

#initialize(content, importance: 1, trim: true) ⇒ Annotation

Returns a new instance of Annotation.



10
11
12
13
14
15
16
17
18
19
# File 'lib/layouter/leaf/annotation.rb', line 10

def initialize(content, importance: 1, trim: true)
  super(importance: importance)
  unless content.is_a?(Numeric) || content.is_a?(String)
    raise(ArgumentError, "Must be a number or strings") 
  end
  @content = content
  @trim = trim
  @min_width = @max_width = @content.to_s.length # TODO: make smarter.
  @min_height = @max_height = 1
end

Instance Attribute Details

#max_heightObject

Returns the value of attribute max_height.



8
9
10
# File 'lib/layouter/leaf/annotation.rb', line 8

def max_height
  @max_height
end

#min_heightObject

Returns the value of attribute min_height.



8
9
10
# File 'lib/layouter/leaf/annotation.rb', line 8

def min_height
  @min_height
end

Instance Method Details

#max_widthObject



32
33
34
# File 'lib/layouter/leaf/annotation.rb', line 32

def max_width
  @content.to_s.length
end

#min_widthObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/layouter/leaf/annotation.rb', line 21

def min_width
  if @trim && @content.is_a?(String)
    2
  elsif @trim && @content.is_a?(Numeric)
    dot = @content.to_s.index(".")
    dot ? dot + 1 : @content.to_s.length
  else
    @content.to_s.length
  end
end

#renderObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/layouter/leaf/annotation.rb', line 36

def render
  layout!
  if @calculated_width == @content.to_s.length
    @content.to_s
  elsif @content.is_a?(String)
    @content[0...(@calculated_width - 1)] + ELLIPSIS
  elsif @content.is_a?(Numeric)
    ALMOST + @content.to_s[0...(@calculated_width - 1)]
  end
end