Skip to content

Commit

Permalink
Improve and expose the relative_to_base method
Browse files Browse the repository at this point in the history
  • Loading branch information
Maher4Ever committed Apr 19, 2012
1 parent 7b56ce8 commit d9e5346
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/listen/directory_record.rb
Expand Up @@ -148,6 +148,17 @@ def fetch_changes(directories, options = {})
@changes
end

# Converts an absolute path to a path that's relative to the base directory.
#
# @param [String] path the path to convert
#
# @return [String] the relative path
#
def relative_to_base(path)
return nil unless path[@directory]
path.sub(%r{^#{@directory}#{File::SEPARATOR}?}, '')
end

private

# Detects modifications and removals recursively in a directory.
Expand Down Expand Up @@ -271,15 +282,5 @@ def insert_path(path)
def existing_path?(path)
@paths[File.dirname(path)][File.basename(path)] != nil
end

# Converts an absolute path to a path that's relative to the base directory.
#
# @param [String] path the path to convert
#
# @return [String] the relative path
#
def relative_to_base(path)
path.sub(%r(^#{@directory}/?/), '')
end
end
end
15 changes: 15 additions & 0 deletions spec/listen/directory_record_spec.rb
Expand Up @@ -55,6 +55,8 @@
end

describe '#ignored?' do
before { subject.stub(:relative_to_base) { |path| path } }

it 'tests paths relative to the base directory' do
subject.should_receive(:relative_to_base).with('file.txt')
subject.ignored?('file.txt')
Expand Down Expand Up @@ -85,6 +87,8 @@
end

describe '#filterd?' do
before { subject.stub(:relative_to_base) { |path| path } }

context 'when no filtering pattrens are set' do
it 'returns true for any path' do
subject.filtered?('file.txt').should be_true
Expand Down Expand Up @@ -180,6 +184,17 @@
end
end

describe '#relative_to_base' do
it 'removes the path of the base-directory from the passed path' do
path = 'dir/to/app/file.rb'
subject.relative_to_base(File.join(base_directory, path)).should eq path
end

it 'returns nil when the passed path is not inside the base-directory' do
subject.relative_to_base('/tmp/some_random_path').should be_nil
end
end

describe '#fetch_changes' do
context 'with single file changes' do
context 'when a file is created' do
Expand Down

0 comments on commit d9e5346

Please sign in to comment.