Class: Strings::Truncation::Configuration Private

Inherits:
Object
  • Object
show all
Defined in:
lib/strings/truncation/configuration.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.

A configuration object used by a Strings::Truncation instance

Constant Summary collapse

DEFAULT_LENGTH =

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

30
DEFAULT_OMISSION =

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

""
DEFAULT_POSITION =

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

0

Instance Method Summary collapse

Constructor Details

#initialize(length: DEFAULT_LENGTH, omission: DEFAULT_OMISSION, position: DEFAULT_POSITION, separator: nil) ⇒ Configuration

Create a configuration



18
19
20
21
22
23
24
# File 'lib/strings/truncation/configuration.rb', line 18

def initialize(length: DEFAULT_LENGTH, omission: DEFAULT_OMISSION,
               position: DEFAULT_POSITION, separator: nil)
  @length = length
  @omission = omission
  @position = position
  @separator = separator
end

Instance Method Details

#length(number = (not_set = true)) ⇒ Object

The maximum length to truncate to

Examples:

strings = Strings::Truncation.new

strings.configure do |config|
  config.length 40
end

Parameters:

  • number (Integer) (defaults to: (not_set = true))


48
49
50
51
52
53
54
# File 'lib/strings/truncation/configuration.rb', line 48

def length(number = (not_set = true))
  if not_set
    @length
  else
    @length = number
  end
end

#omission(string = (not_set = true)) ⇒ Object

The string to denote omitted content

Examples:

strings = Strings::Truncation.new

strings.configure do |config|
  config.omission "..."
end

Parameters:

  • string (String) (defaults to: (not_set = true))


68
69
70
71
72
73
74
# File 'lib/strings/truncation/configuration.rb', line 68

def omission(string = (not_set = true))
  if not_set
    @omission
  else
    @omission = string
  end
end

#position(position = (not_set = true)) ⇒ Object

The position of the omission within the string

Examples:

strings = Strings::Truncation.new

strings.configure do |config|
  config.position :start
end

Parameters:

  • position (Symbol) (defaults to: (not_set = true))

    the position out of :start, :middle or :end



90
91
92
93
94
95
96
# File 'lib/strings/truncation/configuration.rb', line 90

def position(position = (not_set = true))
  if not_set
    @position
  else
    @position = position
  end
end

#separator(separator = (not_set = true)) ⇒ Object

The separator to break the characters into words

Examples:

strings = Strings::Truncation.new

strings.configure do |config|
  config.separator /[, ]/
end

Parameters:

  • separator (String|Regexp) (defaults to: (not_set = true))


110
111
112
113
114
115
116
# File 'lib/strings/truncation/configuration.rb', line 110

def separator(separator = (not_set = true))
  if not_set
    @separator
  else
    @separator = separator
  end
end

#update(length: nil, omission: nil, position: nil, separator: nil) ⇒ Object

Update current configuration



29
30
31
32
33
34
# File 'lib/strings/truncation/configuration.rb', line 29

def update(length: nil, omission: nil, position: nil, separator: nil)
  @length = length if length
  @omission = omission if omission
  @position = position if position
  @separator = separator if separator
end