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
-
.encode(input, system_or_replacement = :unix) ⇒ Object
Create a copy of a string with all the string’s linebreaks replaced by linebreaks of a specific system or a given replacement.
-
.encoding?(input, *encodings) ⇒ true, false
Checks whether a string includes linebreaks of all the given encoding systems.
-
.encodings(input) ⇒ Set<Symbol>
Detects encoding systems of a string.
Instance Method Summary collapse
-
#linebreak_encode(system_or_replacement = :unix) ⇒ Object
Create a copy of a string with all the string’s linebreaks replaced by linebreaks of a specific system or a given replacement.
-
#linebreak_encoding?(*encodings) ⇒ true, false
Checks whether a string includes linebreaks of all the given encoding systems.
-
#linebreak_encodings ⇒ Set<Symbol>
Detects encoding systems of a string.
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.
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.
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.
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.
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.
129 130 131 |
# File 'lib/hosts/aef/linebreak/linebreak.rb', line 129 def linebreak_encoding?(*encodings) Aef::Linebreak.encoding?(self, encodings) end |