Module: Aef::Linebreak

Defined in:
lib/hosts/aef/linebreak/linebreak.rb

Overview

Namespace for the linebreak library

Constant Summary collapse

BREAK_BY_SYSTEM =

Mapping table from symbol to actual linebreak sequence

{
    :unix => "\n",
    :windows => "\r\n",
    :mac => "\r"
}
SYSTEM_BY_BREAK =

Mapping table from actual linebreak sequence to symbol

BREAK_BY_SYSTEM.invert
BREAK_REGEXP =

Regular expression for linebreak detection and extraction

/(\r\n|[\r\n])/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.encode(input, system) ⇒ Object .encode(input, replacement) ⇒ Object

Create a copy of a string with all the string’s linebreaks replaced by linebreaks of a specific system or a given replacement.

Overloads:

  • .encode(input, system) ⇒ Object

    Parameters:

    • input (String)

      a String as conversion template

    • system (:unix, :windows, :mac)

      a target linebreak system

  • .encode(input, replacement) ⇒ Object

    Parameters:

    • input (String)

      a String as conversion template

    • replacement (String)

      a String to be the replacement for all linebreaks in the template



104
105
106
107
108
109
110
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 104

def self.encode(input, system_or_replacement = :unix)
  if input.respond_to?(:to_s) then input = input.to_s
  else raise ArgumentError, 'Input needs to be a string or must support to_s' end

  input.gsub(BREAK_REGEXP,
             BREAK_BY_SYSTEM[system_or_replacement] || system_or_replacement)
end

.encoding?(input, *encodings) ⇒ true, false

Checks whether a string includes linebreaks of all the given encoding systems.

Parameters:

  • input (String)

    a String to be analysed

  • encodings (Array<Symbol>)

    one or more encoding systems

Returns:

  • (true, false)

    true if all of the given linebreak systems are present in the given String



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 79

def self.encoding?(input, *encodings)
  systems = BREAK_BY_SYSTEM.keys

  encodings.flatten!
  encodings.each do |encoding|
    unless systems.include?(encoding)
      raise ArgumentError,
            %{Invalid encoding system. Available systems: #{systems.join(', ')}. Arguments are expected as symbols or an array of symbols.}
    end
  end

  Aef::Linebreak.encodings(input) == Set.new(encodings)
end

.encodings(input) ⇒ Set<Symbol>

Detects encoding systems of a string.

Parameters:

  • input (String)

    a String to be analysed

Returns:

  • (Set<Symbol>)

    the encoding systems present in the given String



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 59

def self.encodings(input)
  if input.respond_to?(:to_s) then input = input.to_s
  else raise ArgumentError, 'Input needs to be a string or must support to_s' end

  occurences = Set.new

  input.scan(BREAK_REGEXP).each do |linebreak|
    occurences << SYSTEM_BY_BREAK[linebreak.first]
  end

  occurences
end

Instance Method Details

#encode(system) ⇒ Object #encode(replacement) ⇒ Object

Create a copy of a string with all the string’s linebreaks replaced by linebreaks of a specific system or a given replacement.

This method is supposed to be used as a method of String.

Overloads:

  • #encode(system) ⇒ Object

    Parameters:

    • system (:unix, :windows, :mac)

      a target linebreak system

  • #encode(replacement) ⇒ Object

    Parameters:

    • replacement (String)

      a String to be the replacement for all linebreaks in the template



144
145
146
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 144

def linebreak_encode(system_or_replacement = :unix)
  Aef::Linebreak.encode(self, system_or_replacement)
end

#linebreak_encoding?(*encodings) ⇒ true, false

Checks whether a string includes linebreaks of all the given encoding systems.

This method is supposed to be used as a method of String.

Parameters:

  • encodings (Array<Symbol>)

    one or more encoding systems

Returns:

  • (true, false)

    true if all of the given linebreak systems are present in the given String



129
130
131
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 129

def linebreak_encoding?(*encodings)
  Aef::Linebreak.encoding?(self, encodings)
end

#linebreak_encodingsSet<Symbol>

Detects encoding systems of a string.

This method is supposed to be used as a method of String.

Returns:

  • (Set<Symbol>)

    the encoding systems present in the String



117
118
119
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 117

def linebreak_encodings
  Aef::Linebreak.encodings(self)
end