class Hanami::Utils::PathPrefix

Prefixed string

@since 0.1.0

Constants

DEFAULT_SEPARATOR

Path separator

@since 0.3.1 @api private

Attributes

separator[R]

@since 0.1.0 @api private

Public Class Methods

new(string = nil, separator = DEFAULT_SEPARATOR) click to toggle source

Initialize the path prefix

@param string [::String] the prefix value @param separator [::String] the separator used between tokens

@return [PathPrefix] self

@since 0.1.0

@see Hanami::Utils::PathPrefix::DEFAULT_SEPARATOR

# File lib/hanami/utils/path_prefix.rb, line 27
def initialize(string = nil, separator = DEFAULT_SEPARATOR)
  @string = string.to_s
  @separator = separator
end

Public Instance Methods

==(other) click to toggle source

Equality

@return [TrueClass,FalseClass]

@since 0.3.0

# File lib/hanami/utils/path_prefix.rb, line 120
def ==(other)
  to_s == other
end
Also aliased as: eql?
eql?(other)
Alias for: ==
hash() click to toggle source

Returns the hash of the internal string

@return [Integer]

@since 0.3.0

# File lib/hanami/utils/path_prefix.rb, line 100
def hash
  @string.hash
end
join(*strings) click to toggle source

Joins self with the given token. It cleans up all the ‘separator` repetitions.

@param strings [::String] the token(s) we want to join

@return [Hanami::Utils::PathPrefix] the joined string

@since 0.1.0

@example Single string

require 'hanami/utils/path_prefix'

path_prefix = Hanami::Utils::PathPrefix.new('/posts')
path_prefix.join('new').to_s  # => "/posts/new"
path_prefix.join('/new').to_s # => "/posts/new"

path_prefix = Hanami::Utils::PathPrefix.new('posts')
path_prefix.join('new').to_s  # => "/posts/new"
path_prefix.join('/new').to_s # => "/posts/new"

@example Multiple strings

require 'hanami/utils/path_prefix'

path_prefix = Hanami::Utils::PathPrefix.new('myapp')
path_prefix.join('/assets', 'application.js').to_s
  # => "/myapp/assets/application.js"
# File lib/hanami/utils/path_prefix.rb, line 58
def join(*strings)
  relative_join(strings).absolute!
end
relative_join(strings, separator = @separator) click to toggle source

Joins self with the given token, without prefixing it with ‘separator`. It cleans up all the `separator` repetitions.

@param strings [::String] the tokens we want to join @param separator [::String] the separator used between tokens

@return [Hanami::Utils::PathPrefix] the joined string

@raise [TypeError] if one of the argument can’t be treated as a

string

@since 0.1.0

@example

require 'hanami/utils/path_prefix'

path_prefix = Hanami::Utils::PathPrefix.new 'posts'
path_prefix.relative_join('new').to_s      # => 'posts/new'
path_prefix.relative_join('new', '_').to_s # => 'posts_new'
# 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() click to toggle source

Returns a string representation

@return [::String]

@since 0.3.0

# File lib/hanami/utils/path_prefix.rb, line 109
def to_s
  @string
end
Also aliased as: to_str
to_str()
Alias for: to_s

Protected Instance Methods

absolute!() click to toggle source

Modifies the path prefix to have a prepended separator.

@return [self]

@since 0.3.1 @api private

@see absolute

# File lib/hanami/utils/path_prefix.rb, line 136
def absolute!
  @string.prepend(separator) unless absolute?

  self
end
absolute?() click to toggle source

Returns whether the path prefix starts with its separator.

@return [TrueClass,FalseClass]

@since 0.3.1 @api private

@example

require 'hanami/utils/path_prefix'

Hanami::Utils::PathPrefix.new('/posts').absolute? #=> true
Hanami::Utils::PathPrefix.new('posts').absolute?  #=> false
# File lib/hanami/utils/path_prefix.rb, line 154
def absolute?
  @string.start_with?(separator)
end
relative!() click to toggle source

Modifies the path prefix to remove the leading separator.

@return [self]

@since 0.3.1 @api private

@see relative

# File lib/hanami/utils/path_prefix.rb, line 166
def relative!
  @string.gsub!(/(?<!:)#{separator * 2}/, separator)
  @string[/\A#{separator}|^/] = ""

  self
end