Class Tap::Root
In: lib/tap/root.rb
Parent: Object

Methods

chdir   empty?   ex   exchange   expand   glob   mkdir   new   open   parent   path   prepare   relative?   relative_path   root   rp   sub   to_s   tr   translate   trivial?   type  

Constants

WIN_ROOT_PATTERN = /\A[A-z]:\//   Regexp to match a windows-style root path.

Public Class methods

Empty returns true when dir is an existing directory that has no files.

[Source]

    # File lib/tap/root.rb, line 27
27:       def empty?(dir)
28:         File.directory?(dir) && (Dir.entries(dir) - ['.', '..']).empty?
29:       end

[Source]

    # File lib/tap/root.rb, line 35
35:     def initialize(path=Dir.pwd, dir=Dir.pwd)
36:       @path_root = File.expand_path(path.to_s, dir.to_s)
37:     end

Trivial indicates when a path does not have content to load. Returns true if the file at path is empty, non-existant, a directory, or nil.

[Source]

    # File lib/tap/root.rb, line 22
22:       def trivial?(path)
23:         path == nil || !File.file?(path) || File.size(path) == 0
24:       end

The path root type indicating windows, *nix, or some unknown style of paths (:win, :nix, :unknown).

[Source]

    # File lib/tap/root.rb, line 8
 8:       def type
 9:         @path_root_type ||= begin
10:           pwd = File.expand_path('.')
11:           
12:           case
13:           when pwd =~ WIN_ROOT_PATTERN then :win 
14:           when pwd[0] == ?/ then :nix
15:           else :unknown
16:           end
17:         end
18:       end

Public Instance methods

Changes to the specified directory using Dir.chdir, keeping the same block semantics as that method. The directory will be created if necessary and mkdir is specified. Raises an error for non-existant directories, as well as non-directory inputs.

[Source]

     # File lib/tap/root.rb, line 131
131:     def chdir(dir, mkdir=false, &block)
132:       dir = expand(dir)
133:     
134:       unless File.directory?(dir)
135:         if !File.exists?(dir) && mkdir
136:           FileUtils.mkdir_p(dir)
137:         else
138:           raise ArgumentError, "not a directory: #{dir}"
139:         end
140:       end
141:     
142:       Dir.chdir(dir, &block)
143:     end
ex(path, extname)

Alias for exchange

Returns the expanded path, exchanging the extension with extname. Extname may optionally omit the leading period.

  root = Root.new("/root")
  root.exchange('path/to/file.txt', '.html')  # => '/root/path/to/file.html'
  root.exchange('path/to/file.txt', 'rb')     # => '/root/path/to/file.rb'

[Source]

    # File lib/tap/root.rb, line 96
96:     def exchange(path, extname)
97:       path = expand(path)
98:       "#{path.chomp(File.extname(path))}#{extname[0] == ?. ? '' : '.'}#{extname}"
99:     end

Stringifies and expands the path relative to self. Paths are turned into strings using to_s.

[Source]

    # File lib/tap/root.rb, line 41
41:     def expand(path)
42:       File.expand_path(path.to_s, @path_root)
43:     end

Globs for unique paths matching the input patterns expanded relative to self. If no patterns are specified, glob returns all paths matching ’./**/*’.

[Source]

     # File lib/tap/root.rb, line 121
121:     def glob(*patterns)
122:       patterns << "**/*" if patterns.empty?
123:       patterns.collect! {|pattern| expand(pattern) }
124:       Dir[*patterns].uniq
125:     end

Makes the specified directory and parent directories (as required).

[Source]

     # File lib/tap/root.rb, line 146
146:     def mkdir(*path)
147:       path = self.path(*path)
148:       FileUtils.mkdir_p(path) unless File.directory?(path)
149:       path
150:     end

Opens the path in the specified mode, using the same semantics as File.open.

[Source]

     # File lib/tap/root.rb, line 154
154:     def open(path, mode='r', &block)
155:       path = expand(path)
156:       File.open(path, mode, &block)
157:     end

Returns a new Root for the parent directory for self.

[Source]

    # File lib/tap/root.rb, line 85
85:     def parent
86:       root File.dirname(@path_root)
87:     end

Joins and expands the path segments relative to self. Segments are turned to strings using to_s.

[Source]

    # File lib/tap/root.rb, line 47
47:     def path(*segments)
48:       segments.collect! {|seg| seg.to_s }
49:       expand(File.join(*segments))
50:     end

Prepares a file at the path by making paths‘s parent directory. The file is opened in the mode and passed to the block, if given. The mode is ignored if no block is given.

Returns path.

[Source]

     # File lib/tap/root.rb, line 164
164:     def prepare(*path)
165:       path = self.path(*path)
166:       dirname = File.dirname(path)
167:       FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
168:       File.open(path, 'w') {|io| yield(io) } if block_given?
169:       path
170:     end

Returns true if the expanded path is relative to self.

[Source]

    # File lib/tap/root.rb, line 53
53:     def relative?(path)
54:       expand(path).rindex(@path_root, 0) == 0
55:     end

Returns the part of the expanded path relative to self, or nil if the path is not relative to self.

[Source]

    # File lib/tap/root.rb, line 59
59:     def relative_path(path)
60:       path = expand(path)
61:       return nil unless relative?(path)
62:   
63:       # if path_root_length > path.length then the first arg
64:       # returns nil, and an empty string is returned
65:       path[path_root_length, path.length - path_root_length] || ""
66:     end

Returns a new Root for the path, relative to self.

[Source]

    # File lib/tap/root.rb, line 70
70:     def root(path)
71:       Root.new(path, self)
72:     end
rp(path)

Alias for relative_path

Returns a new Root for the path, relative to self. Same as root but raises an error if the path is not relative to self.

[Source]

    # File lib/tap/root.rb, line 76
76:     def sub(path)
77:       sub = root(path)
78:       unless relative?(sub)
79:         raise ArgumentError, "not a sub path: #{sub} (#{self})"
80:       end
81:       sub
82:     end

Returns path.

[Source]

     # File lib/tap/root.rb, line 173
173:     def to_s
174:       @path_root
175:     end
tr(path, source, target)

Alias for translate

Generates a path translated from the source to the target. Raises an error if path is not relative to the source.

[Source]

     # File lib/tap/root.rb, line 104
104:     def translate(path, source, target)
105:       path = expand(path)
106:       source = root(source)
107:       target = root(target)
108:       
109:       rp = source.relative_path(path)
110:       if rp.nil?
111:         raise ArgumentError, "\n#{path}\nis not relative to:\n#{source}"
112:       end
113:       
114:       target.path(rp)
115:     end

[Validate]