Class: Vedeu::DSL::Align Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Common
Defined in:
lib/vedeu/dsl/helpers/align.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.

Aligns a text value based on given options when building views.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#absent?, #array?, #boolean, #boolean?, #empty_value?, #escape?, #falsy?, #hash?, #line_model?, #numeric?, #positionable?, #present?, #snake_case, #stream_model?, #string?, #symbol?, #truthy?, #view_model?

Constructor Details

#initialize(value = '', options = {}) ⇒ Vedeu::DSL::Align

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.

Parameters:

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

Options Hash (options):

  • align (Symbol)
  • name (String|Symbol)
  • pad (String)
  • width (Fixnum)


29
30
31
32
# File 'lib/vedeu/dsl/helpers/align.rb', line 29

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

Instance Attribute Details

#optionsHash (readonly, protected)

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:

  • (Hash)


58
59
60
# File 'lib/vedeu/dsl/helpers/align.rb', line 58

def options
  @options
end

#valueString (readonly, protected)

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:

  • (String)


62
63
64
# File 'lib/vedeu/dsl/helpers/align.rb', line 62

def value
  @value
end

Instance Method Details

#alignmentVedeu::Coercers::Alignment (private)

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.



67
68
69
# File 'lib/vedeu/dsl/helpers/align.rb', line 67

def alignment
  @_alignment ||= Vedeu::Coercers::Alignment.coerce(options[:align])
end

#centreString (private) Also known as: center

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.

The value padded to width, centralized.

Returns:

  • (String)


74
75
76
# File 'lib/vedeu/dsl/helpers/align.rb', line 74

def centre
  value.center(width, options[:pad])
end

#defaultsHash<Symbol => void> (private)

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.

The default options/attributes for a new instance of this class.

Returns:

  • (Hash<Symbol => void>)


80
81
82
83
84
85
86
87
# File 'lib/vedeu/dsl/helpers/align.rb', line 80

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

#geometryVedeu::Geometries::Geometry (private)

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 the named geometry from the geometries repository.



90
91
92
# File 'lib/vedeu/dsl/helpers/align.rb', line 90

def geometry
  @_geometry ||= Vedeu.geometries.by_name(options[:name])
end

#leftString (private)

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.

The value padded to width, left justified. See #width.

Returns:

  • (String)


97
98
99
# File 'lib/vedeu/dsl/helpers/align.rb', line 97

def left
  value.ljust(width, options[:pad])
end

#rightString (private)

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.

The value padded to width, right justified.

Returns:

  • (String)


104
105
106
# File 'lib/vedeu/dsl/helpers/align.rb', line 104

def right
  value.rjust(width, options[:pad])
end

#textString

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:

  • (String)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vedeu/dsl/helpers/align.rb', line 35

def text
  if unaligned?
    value

  elsif left_aligned?
    left

  elsif centre_aligned?
    centre

  elsif right_aligned?
    right

  else
    value

  end
end

#widthFixnum (private)

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.

Return the width given in the options. If no :width option was set, but a :name option was set, then we can use the width of the interface to determine the correct width to use. There is a caveat in this; if the :align option was set to :left, but no :width provided, then we don’t want to use the interface’s width. The reason being, is that there may be another stream in this line which will not appear by default as it will be push right out of the visible area.

Return the width of the interface when a name is given, otherwise use the given width.

Returns:

  • (Fixnum)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/vedeu/dsl/helpers/align.rb', line 121

def width
  if present?(options[:width])
    options[:width]

  elsif present?(options[:name])
    if left_aligned?
      value.size

    else
      geometry.bordered_width

    end

  else
    value.size

  end
end