Class: Hanami::Utils::PathPrefix

Inherits:
String
  • Object
show all
Defined in:
lib/hanami/utils/path_prefix.rb

Overview

Prefixed string

Since:

  • 0.1.0

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

Since:

  • 0.3.1

"/"

Constants inherited from String

String::CAPITALIZE_SEPARATOR, String::CLASSIFY_SEPARATOR, String::CLASSIFY_WORD_SEPARATOR, String::DASHERIZE_SEPARATOR, String::EMPTY_STRING, String::NAMESPACE_SEPARATOR, String::TITLEIZE_SEPARATOR, String::TOKENIZE_REGEXP, String::TOKENIZE_SEPARATOR, String::UNDERSCORE_DIVISION_TARGET, String::UNDERSCORE_SEPARATOR

Instance Method Summary collapse

Methods inherited from String

#==, bind, #capitalize, capitalize, #classify, classify, dasherize, #dasherize, demodulize, #demodulize, #gsub, #hash, #method_missing, #namespace, namespace, pluralize, #pluralize, #respond_to_missing?, #rsub, rsub, #scan, #singularize, singularize, #split, #titleize, titleize, #to_s, #tokenize, transform, underscore, #underscore

Constructor Details

#initialize(string = nil, separator = DEFAULT_SEPARATOR) ⇒ PathPrefix

Initialize the path prefix

Parameters:

  • string (::String) (defaults to: nil)

    the prefix value

  • separator (::String) (defaults to: DEFAULT_SEPARATOR)

    the separator used between tokens

See Also:

Since:

  • 0.1.0


28
29
30
31
# File 'lib/hanami/utils/path_prefix.rb', line 28

def initialize(string = nil, separator = DEFAULT_SEPARATOR)
  super(string)
  @separator = separator
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hanami::Utils::String

Instance Method Details

#join(*strings) ⇒ Hanami::Utils::PathPrefix

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

Examples:

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"

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"

Parameters:

  • strings (::String)

    the token(s) we want to join

Returns:

Since:

  • 0.1.0


59
60
61
# File 'lib/hanami/utils/path_prefix.rb', line 59

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.

Examples:

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'

Parameters:

  • strings (::String)

    the tokens we want to join

  • separator (::String) (defaults to: @separator)

    the separator used between tokens

Returns:

Raises:

  • (TypeError)

    if one of the argument can't be treated as a string

Since:

  • 0.1.0


82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hanami/utils/path_prefix.rb', line 82

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