Skip to content

Commit

Permalink
PathPrefix: #join and #relative_join to return a new instance of that…
Browse files Browse the repository at this point in the history
… class. Made #relative_join to be aware of the previous occurrences of separator
  • Loading branch information
jodosha committed Nov 7, 2014
1 parent df7380b commit 038987b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
62 changes: 28 additions & 34 deletions lib/lotus/utils/path_prefix.rb
@@ -1,9 +1,17 @@
require 'lotus/utils/string'

module Lotus
module Utils
# Prefixed string
#
# @since 0.1.0
class PathPrefix
class PathPrefix < Lotus::Utils::String
# Path separator
#
# @since x.x.x
# @api private
DEFAULT_SEPARATOR = '/'.freeze

# Initialize the path prefix
#
# @param string [::String] the prefix value
Expand All @@ -12,9 +20,11 @@ class PathPrefix
# @return [PathPrefix] self
#
# @since 0.1.0
def initialize(string = nil, separator = '/')
#
# @see Lotus::Utils::PathPrefix::DEFAULT_SEPARATOR
def initialize(string = nil, separator = DEFAULT_SEPARATOR)
super(string)
@separator = separator
@string = string.to_s
end

# Joins self with the given token.
Expand Down Expand Up @@ -68,53 +78,37 @@ def join(*strings)
# path_prefix.relative_join 'new', '_' # => 'posts_new'
def relative_join(strings, separator = @separator)
raise TypeError if separator.nil?
relativize [@string, strings].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
self.class.new(
relativize(
[@string.gsub(@separator, separator), strings].join(separator),
separator
),
@separator
)
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
# @since 0.1.0
# @api private
attr_reader :separator

# @since 0.1.0
# @api private
def absolutize(string)
string.tap do |s|
s.prepend(separator) unless absolute?(s)
end
end

# @since 0.1.0
# @api private
def absolute?(string)
string.start_with?(separator)
end

# @since 0.1.0
# @api private
def relativize(string, separator = @separator)
string.
gsub(%r{(?<!:)#{ separator * 2 }}, separator).
Expand Down
17 changes: 17 additions & 0 deletions test/path_prefix_test.rb
Expand Up @@ -13,6 +13,12 @@
end

describe '#join' do
it 'returns a PathPrefix' do
prefix = Lotus::Utils::PathPrefix.new('orders', '?').join('new')
prefix.must_be_kind_of(Lotus::Utils::PathPrefix)
prefix.__send__(:separator).must_equal('?')
end

it 'joins a string' do
prefix = Lotus::Utils::PathPrefix.new('fruits')
prefix.join('peaches').must_equal '/fruits/peaches'
Expand Down Expand Up @@ -46,6 +52,12 @@
end

describe '#relative_join' do
it 'returns a PathPrefix' do
prefix = Lotus::Utils::PathPrefix.new('orders', '&').relative_join('new')
prefix.must_be_kind_of(Lotus::Utils::PathPrefix)
prefix.__send__(:separator).must_equal('&')
end

it 'joins a string without prefixing with separator' do
prefix = Lotus::Utils::PathPrefix.new('fruits')
prefix.relative_join('peaches').must_equal 'fruits/peaches'
Expand Down Expand Up @@ -76,6 +88,11 @@
prefix.relative_join('_cherries', '_').must_equal 'fruits_cherries'
end

it 'changes all the occurences of the current separator with the given one' do
prefix = Lotus::Utils::PathPrefix.new('?fruits', '?')
prefix.relative_join('cherries', '_').must_equal 'fruits_cherries'
end

it 'raises error if the given separator is nil' do
prefix = Lotus::Utils::PathPrefix.new('fruits')
-> { prefix.relative_join('_cherries', nil) }.must_raise TypeError
Expand Down

0 comments on commit 038987b

Please sign in to comment.