Class: Vedeu::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/output/text.rb

Overview

Present a string (or object responding to ‘to_s`).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = '', options = {}) ⇒ Text

Returns a new instance of Text.

Parameters:

  • value (String) (defaults to: '')
  • options (Hash) (defaults to: {})

Options Hash (options):

  • width (Integer)
  • anchor (Symbol)

    See #anchor

  • pad (String)


20
21
22
23
# File 'lib/vedeu/output/text.rb', line 20

def initialize(value = '', options = {})
  @value   = value
  @options = defaults.merge!(options)
end

Instance Attribute Details

#optionsObject (readonly, private)

Returns the value of attribute options.



44
45
46
# File 'lib/vedeu/output/text.rb', line 44

def options
  @options
end

#valueObject (readonly, private)

Returns the value of attribute value.



44
45
46
# File 'lib/vedeu/output/text.rb', line 44

def value
  @value
end

Class Method Details

.with(value = '', options = {}) ⇒ Object

See Also:



8
9
10
# File 'lib/vedeu/output/text.rb', line 8

def self.with(value = '', options = {})
  new(value, options).aligned
end

Instance Method Details

#alignedString

Aligns the value.

Returns:

  • (String)


28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vedeu/output/text.rb', line 28

def aligned
  return string unless width

  return truncated if truncate?

  case anchor
  when :align, :left, :text then left
  when :centre, :center     then centre
  when :right               then right
  else
    left
  end
end

#anchorSymbol (private)

Returns One of :align, :centre, :center, :left, :right, :text.

Returns:

  • (Symbol)

    One of :align, :centre, :center, :left, :right, :text



47
48
49
# File 'lib/vedeu/output/text.rb', line 47

def anchor
  options[:anchor]
end

#centreString (private)

Returns:

  • (String)


52
53
54
# File 'lib/vedeu/output/text.rb', line 52

def centre
  string.center(width, pad)
end

#defaultsHash (private)

The default values for a new instance of this class.

Returns:

  • (Hash)


59
60
61
62
63
64
65
# File 'lib/vedeu/output/text.rb', line 59

def defaults
  {
    anchor: :left,
    pad:    ' ',
    width:  nil,
  }
end

#leftString (private)

Returns:

  • (String)


68
69
70
# File 'lib/vedeu/output/text.rb', line 68

def left
  string.ljust(width, pad)
end

#padString (private)

Returns:

  • (String)


73
74
75
# File 'lib/vedeu/output/text.rb', line 73

def pad
  options[:pad]
end

#rightString (private)

Returns:

  • (String)


78
79
80
# File 'lib/vedeu/output/text.rb', line 78

def right
  string.rjust(width, pad)
end

#stringString (private)

Returns:

  • (String)


83
84
85
# File 'lib/vedeu/output/text.rb', line 83

def string
  value.to_s
end

#truncate?Boolean (private)

Returns:

  • (Boolean)


88
89
90
# File 'lib/vedeu/output/text.rb', line 88

def truncate?
  string.size > width
end

#truncatedString (private)

Returns:

  • (String)


93
94
95
# File 'lib/vedeu/output/text.rb', line 93

def truncated
  string.slice(0, width)
end

#widthFixnum (private)

Returns:



98
99
100
# File 'lib/vedeu/output/text.rb', line 98

def width
  options[:width]
end