Class: Hanami::Utils::PathPrefix
- Inherits:
-
Object
- Object
- Hanami::Utils::PathPrefix
- Defined in:
- lib/hanami/utils/path_prefix.rb
Overview
Prefixed string
Constant Summary collapse
- DEFAULT_SEPARATOR =
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.
Path separator
"/"
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
(also: #eql?)
Equality.
-
#hash ⇒ Integer
Returns the hash of the internal string.
-
#initialize(string = nil, separator = DEFAULT_SEPARATOR) ⇒ PathPrefix
constructor
Initialize the path prefix.
-
#join(*strings) ⇒ Hanami::Utils::PathPrefix
Joins self with the given token.
-
#relative_join(strings, separator = @separator) ⇒ Hanami::Utils::PathPrefix
Joins self with the given token, without prefixing it with
separator. -
#to_s ⇒ ::String
(also: #to_str)
Returns a string representation.
Constructor Details
#initialize(string = nil, separator = DEFAULT_SEPARATOR) ⇒ PathPrefix
Initialize the path prefix
27 28 29 30 |
# File 'lib/hanami/utils/path_prefix.rb', line 27 def initialize(string = nil, separator = DEFAULT_SEPARATOR) @string = string.to_s @separator = separator end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass Also known as: eql?
Equality
120 121 122 |
# File 'lib/hanami/utils/path_prefix.rb', line 120 def ==(other) to_s == other end |
#hash ⇒ Integer
Returns the hash of the internal string
100 101 102 |
# File 'lib/hanami/utils/path_prefix.rb', line 100 def hash @string.hash end |
#join(*strings) ⇒ Hanami::Utils::PathPrefix
Joins self with the given token. It cleans up all the separator repetitions.
58 59 60 |
# File 'lib/hanami/utils/path_prefix.rb', line 58 def join(*strings) relative_join(strings).absolute! end |
#relative_join(strings, separator = @separator) ⇒ Hanami::Utils::PathPrefix
Joins self with the given token, without prefixing it with separator. It cleans up all the separator repetitions.
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/hanami/utils/path_prefix.rb', line 81 def relative_join(strings, separator = @separator) raise TypeError if separator.nil? prefix = @string.gsub(@separator, separator) result = [prefix, strings] result.flatten! result.compact! result.reject! { |string| string == separator } self.class.new( result.join(separator), separator ).relative! end |
#to_s ⇒ ::String Also known as: to_str
Returns a string representation
109 110 111 |
# File 'lib/hanami/utils/path_prefix.rb', line 109 def to_s @string end |