Class: FSPath
- Inherits:
-
Pathname
- Object
- Pathname
- FSPath
- Defined in:
- lib/fspath.rb,
lib/fspath/xattr.rb,
lib/fspath/darwin.rb
Defined Under Namespace
Classes: Tempfile
Constant Summary collapse
- FINDER_LABEL_COLORS =
[:none, :orange, :red, :yellow, :blue, :purple, :green, :gray].freeze
- FINDER_LABEL_COLOR_ALIASES =
{:grey => :gray}.freeze
Class Method Summary collapse
-
.common_dir(*paths) ⇒ Object
Returns common dir for paths.
-
.temp_dir(*args) ⇒ Object
Returns or yields FSPath with temp directory created by Dir.mktmpdir.
-
.temp_file(*args, &block) ⇒ Object
Returns or yields temp file created by Tempfile.new with path returning FSPath.
-
.temp_file_path(*args) ⇒ Object
Returns or yields path as FSPath of temp file created by Tempfile.new.
-
.~(name = nil) ⇒ Object
Return current user home path if called without argument.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Fixing Pathname.+.
-
#/(other) ⇒ Object
Join paths using File.join.
-
#append(data) ⇒ Object
Append data to file.
-
#ascend(&block) ⇒ Object
Iterates over and yields each element in the given path in ascending order.
-
#descend(&block) ⇒ Object
Iterates over and yields each element in the given path in descending order.
-
#escape_glob ⇒ Object
Escape characters in glob pattern.
-
#finder_label ⇒ Object
Get finder label (one of :none, :orange, :red, :yellow, :blue, :purple, :green and :gray).
-
#finder_label=(color) ⇒ Object
Set finder label (:grey is same as :gray, nil or false as :none).
-
#glob(*args, &block) ⇒ Object
Expand glob.
-
#lxattr ⇒ Object
Xattr instance for path which doesn’t follow symlinks.
-
#mac_alias ⇒ Object
MacTypes::Alias for path.
-
#mac_file_url ⇒ Object
MacTypes::FileURL for path.
-
#mac_finder_alias ⇒ Object
Finder item for path through mac_alias.
-
#mac_finder_file_url ⇒ Object
Finder item for path through mac_alias.
-
#move_to_trash ⇒ Object
Move to trash using finder.
-
#parts(&block) ⇒ Object
Returns path parts.
-
#relative_path_from(other) ⇒ Object
Fixing Pathname.relative_path_from.
-
#spotlight_comment ⇒ Object
Get spotlight comment.
-
#spotlight_comment=(comment) ⇒ Object
Set spotlight comment.
-
#write(data) ⇒ Object
Write data to file.
-
#xattr ⇒ Object
Xattr instance for path.
Class Method Details
.common_dir(*paths) ⇒ Object
Returns common dir for paths
20 21 22 23 24 |
# File 'lib/fspath.rb', line 20 def common_dir(*paths) paths.map do |path| new(path).dirname.ascend end.inject(:&).first end |
.temp_dir(*args) ⇒ Object
Returns or yields FSPath with temp directory created by Dir.mktmpdir
44 45 46 47 48 49 50 51 52 |
# File 'lib/fspath.rb', line 44 def temp_dir(*args) if block_given? Dir.mktmpdir(*args) do |dir| yield new(dir) end else new(Dir.mktmpdir(*args)) end end |
.temp_file(*args, &block) ⇒ Object
Returns or yields temp file created by Tempfile.new with path returning FSPath
27 28 29 30 |
# File 'lib/fspath.rb', line 27 def temp_file(*args, &block) args = %w[f] if args.empty? Tempfile.open(*args, &block) end |
.temp_file_path(*args) ⇒ Object
Returns or yields path as FSPath of temp file created by Tempfile.new
33 34 35 36 37 38 39 40 41 |
# File 'lib/fspath.rb', line 33 def temp_file_path(*args) if block_given? temp_file(*args) do |file| yield file.path end else temp_file(*args).path end end |
.~(name = nil) ⇒ Object
Return current user home path if called without argument. If called with argument return specified user home path.
15 16 17 |
# File 'lib/fspath.rb', line 15 def ~(name = nil) new(File.("~#{name}")) end |
Instance Method Details
#+(other) ⇒ Object
Fixing Pathname.+
62 63 64 |
# File 'lib/fspath.rb', line 62 def +(other) self.class.new(plus(@path, other.to_s)) end |
#/(other) ⇒ Object
Join paths using File.join
56 57 58 |
# File 'lib/fspath.rb', line 56 def /(other) self.class.new(File.join(@path, other.to_s)) end |
#append(data) ⇒ Object
Append data to file
80 81 82 83 84 |
# File 'lib/fspath.rb', line 80 def append(data) open('ab') do |f| f.write(data) end end |
#ascend(&block) ⇒ Object
Iterates over and yields each element in the given path in ascending order
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fspath.rb', line 100 def ascend(&block) ascendants = [] path = @path ascendants << self while r = chop_basename(path) path, name = r break if path.empty? ascendants << self.class.new(del_trailing_separator(path)) end if block ascendants.each(&block) end ascendants end |
#descend(&block) ⇒ Object
Iterates over and yields each element in the given path in descending order
116 117 118 119 120 121 122 |
# File 'lib/fspath.rb', line 116 def descend(&block) descendants = ascend.reverse if block descendants.each(&block) end descendants end |
#escape_glob ⇒ Object
Escape characters in glob pattern
87 88 89 |
# File 'lib/fspath.rb', line 87 def escape_glob self.class.new(@path.gsub(/([\*\?\[\]\{\}])/, '\\\\\1')) end |
#finder_label ⇒ Object
Get finder label (one of :none, :orange, :red, :yellow, :blue, :purple, :green and :gray)
13 14 15 |
# File 'lib/fspath/darwin.rb', line 13 def finder_label FINDER_LABEL_COLORS[mac_finder_alias.label_index.get] end |
#finder_label=(color) ⇒ Object
Set finder label (:grey is same as :gray, nil or false as :none)
17 18 19 20 21 22 |
# File 'lib/fspath/darwin.rb', line 17 def finder_label=(color) color = FINDER_LABEL_COLOR_ALIASES[color] || color || :none index = FINDER_LABEL_COLORS.index(color) raise "Unknown label #{color.inspect}" unless index mac_finder_alias.label_index.set(index) end |
#glob(*args, &block) ⇒ Object
Expand glob
92 93 94 95 96 97 |
# File 'lib/fspath.rb', line 92 def glob(*args, &block) flags = args.last.is_a?(Fixnum) ? args.pop : nil args = [File.join(self, *args)] args << flags if flags self.class.glob(*args, &block) end |
#lxattr ⇒ Object
Xattr instance for path which doesn’t follow symlinks
21 22 23 |
# File 'lib/fspath/xattr.rb', line 21 def lxattr Xattr.new(@path, false) end |
#mac_alias ⇒ Object
MacTypes::Alias for path
35 36 37 |
# File 'lib/fspath/darwin.rb', line 35 def mac_alias MacTypes::Alias.path(@path) end |
#mac_file_url ⇒ Object
MacTypes::FileURL for path
40 41 42 |
# File 'lib/fspath/darwin.rb', line 40 def mac_file_url MacTypes::FileURL.path(@path) end |
#mac_finder_alias ⇒ Object
Finder item for path through mac_alias
45 46 47 |
# File 'lib/fspath/darwin.rb', line 45 def mac_finder_alias Appscript.app('Finder').items[mac_alias] end |
#mac_finder_file_url ⇒ Object
Finder item for path through mac_alias
50 51 52 |
# File 'lib/fspath/darwin.rb', line 50 def mac_finder_file_url Appscript.app('Finder').items[mac_file_url] end |
#move_to_trash ⇒ Object
Move to trash using finder
6 7 8 |
# File 'lib/fspath/darwin.rb', line 6 def move_to_trash mac_finder_alias.delete end |
#parts(&block) ⇒ Object
Returns path parts
125 126 127 |
# File 'lib/fspath.rb', line 125 def parts(&block) split_names(@path).flatten end |
#relative_path_from(other) ⇒ Object
Fixing Pathname.relative_path_from
68 69 70 |
# File 'lib/fspath.rb', line 68 def relative_path_from(other) self.class.new(super(self.class.new(other))) end |
#spotlight_comment ⇒ Object
Get spotlight comment
25 26 27 |
# File 'lib/fspath/darwin.rb', line 25 def spotlight_comment mac_finder_alias.comment.get end |
#spotlight_comment=(comment) ⇒ Object
Set spotlight comment
30 31 32 |
# File 'lib/fspath/darwin.rb', line 30 def spotlight_comment=(comment) mac_finder_alias.comment.set(comment.to_s) end |
#write(data) ⇒ Object
Write data to file
73 74 75 76 77 |
# File 'lib/fspath.rb', line 73 def write(data) open('wb') do |f| f.write(data) end end |
#xattr ⇒ Object
Xattr instance for path
16 17 18 |
# File 'lib/fspath/xattr.rb', line 16 def xattr Xattr.new(@path) end |