lib/lotus/utils/path_prefix.rb in lotus-utils-0.2.0 vs lib/lotus/utils/path_prefix.rb in lotus-utils-0.3.0
- old
+ new
@@ -1,26 +1,26 @@
module Lotus
module Utils
# Prefixed string
#
# @since 0.1.0
- class PathPrefix < ::String
+ class PathPrefix
# 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
def initialize(string = nil, separator = '/')
@separator = separator
- super(string.to_s)
+ @string = string.to_s
end
# Joins self with the given token.
- # It cleanups the all the `separator` repetitions.
+ # It cleans up all the `separator` repetitions.
#
# @param string [::String] the token we want to join
#
# @return [::String] the joined string
#
@@ -39,35 +39,69 @@
def join(string)
absolutize relative_join(string)
end
# Joins self with the given token, without prefixing it with `separator`.
- # It cleanups the all the `separator` repetitions.
+ # It cleans up all the `separator` repetitions.
#
# @param string [::String] the token we want to join
# @param separator [::String] the separator used between tokens
#
# @return [::String] the joined string
#
+ # @raise [TypeError] if one of the argument can't be treated as a
+ # string
+ #
# @since 0.1.0
#
# @example
# require 'lotus/utils/path_prefix'
#
# path_prefix = Lotus::Utils::PathPrefix.new 'posts'
# path_prefix.relative_join 'new' # => 'posts/new'
# path_prefix.relative_join 'new', '_' # => 'posts_new'
def relative_join(string, separator = @separator)
- separator = separator || @separator
- relativize [self, string].join(separator), separator
+ raise TypeError if separator.nil?
+ relativize [@string, string].join(separator), separator
end
+ # Returns the hash of the internal string
+ #
+ # @return [Fixnum]
+ #
+ # @since 0.3.0
+ def hash
+ @string.hash
+ end
+
+ # Returns a string representation
+ #
+ # @return [String]
+ #
+ # @since 0.3.0
+ def to_s
+ @string
+ end
+
+ alias_method :to_str, :to_s
+
+ # Equality
+ #
+ # @return [TrueClass,FalseClass]
+ #
+ # @since 0.3.0
+ def ==(other)
+ to_s == other
+ end
+
+ alias_method :eql?, :==
+
private
attr_reader :separator
def absolutize(string)
string.tap do |s|
- s.insert(0, separator) unless absolute?(s)
+ s.prepend(separator) unless absolute?(s)
end
end
def absolute?(string)
string.start_with?(separator)