Class: EpubForge::Utils::FilePath
- Defined in:
- lib/utils/file_path.rb
Class Method Summary collapse
-
.cwd(*args) ⇒ Object
args implicitly joined to cwd.
- .pwd(*args) ⇒ Object
Instance Method Summary collapse
- #append(content = nil, &block) ⇒ Object
- #basename_no_ext ⇒ Object
-
#empty? ⇒ Boolean
Not the same as zero?.
- #expand ⇒ Object
- #ext ⇒ Object
- #fwf_filepath ⇒ Object
-
#glob(*args) ⇒ Object
opts: :class => [self.class] The class of objects you want returned (String, FilePath, ClassLoader, etc.) Should probably be a subclass of FilePath or String.
- #grep(regex) ⇒ Object
- #gsub(*args) ⇒ Object
- #gsub!(*args) ⇒ Object
-
#initialize(*args) ⇒ FilePath
constructor
A new instance of FilePath.
- #join(*args, &block) ⇒ Object
- #relative_path_from(ancestor_dir) ⇒ Object
- #touch ⇒ Object
- #touch_dir ⇒ Object
- #up ⇒ Object
- #without_ext ⇒ Object
- #write(content = nil, &block) ⇒ Object
Constructor Details
#initialize(*args) ⇒ FilePath
Returns a new instance of FilePath.
4 5 6 |
# File 'lib/utils/file_path.rb', line 4 def initialize( *args ) super( File.join( *args ) ) end |
Class Method Details
.cwd(*args) ⇒ Object
args implicitly joined to cwd
9 10 11 |
# File 'lib/utils/file_path.rb', line 9 def self.cwd( *args ) Dir.pwd.fwf_filepath.join( *args ) end |
.pwd(*args) ⇒ Object
13 14 15 |
# File 'lib/utils/file_path.rb', line 13 def self.pwd( *args ) self.cwd( *args ) end |
Instance Method Details
#append(content = nil, &block) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/utils/file_path.rb', line 91 def append( content = nil, &block ) File.open( self, "a" ) do |f| f << content if content if block_given? yield f end end end |
#basename_no_ext ⇒ Object
120 121 122 |
# File 'lib/utils/file_path.rb', line 120 def basename_no_ext self.basename.to_s.split(".")[0..-2].join(".").fwf_filepath end |
#empty? ⇒ Boolean
Not the same as zero?
110 111 112 113 114 115 116 117 118 |
# File 'lib/utils/file_path.rb', line 110 def empty? raise Exceptions::FileDoesNotExist unless self.exist? if self.file? File.size( self ) == 0 elsif self.directory? self.glob( "**", "*" ).length == 0 end end |
#expand ⇒ Object
62 63 64 |
# File 'lib/utils/file_path.rb', line 62 def self.class.new( File.( self ) ) end |
#ext ⇒ Object
128 129 130 |
# File 'lib/utils/file_path.rb', line 128 def ext self.basename.to_s.split(".").last || "" end |
#fwf_filepath ⇒ Object
147 148 149 |
# File 'lib/utils/file_path.rb', line 147 def fwf_filepath self end |
#glob(*args) ⇒ Object
opts:
:class => [self.class] The class of objects you want returned (String, FilePath, ClassLoader, etc.)
Should probably be a subclass of FilePath or String. Class.init must accept a string
[representing a file path] as the sole argument.
:recurse => [false]
:ext => [] A single symbol, or a list containing strings/symbols representing file name extensions.
No leading periods kthxbai.
If opts not given, the user can still do it explicitly with arguments like .glob("**", "*.rb")
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/utils/file_path.rb', line 41 def glob( *args ) opts = args.last.is_a?(Hash) ? args.pop : {} recurser = opts[:recurse] ? "**" : nil extensions = case opts[:ext] when Symbol, String "*.#{opts[:ext]}" when Array extensions = opts[:ext].map(&:to_s).join(',') "*.{#{extensions}}" when NilClass nil end args += [recurser, extensions] args.compact! opts[:class] ||= self.class Dir.glob( self.join(*args) ).map{ |f| opts[:class].new(f) } end |
#grep(regex) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/utils/file_path.rb', line 100 def grep( regex ) return [] unless self.file? matching = [] self.each_line do |line| matching.push( line ) if line.match( regex ) end matching end |
#gsub(*args) ⇒ Object
138 139 140 |
# File 'lib/utils/file_path.rb', line 138 def gsub( *args ) self.to_s.gsub(*args).fwf_filepath end |
#gsub!(*args) ⇒ Object
142 143 144 145 |
# File 'lib/utils/file_path.rb', line 142 def gsub!( *args ) new_str = self.to_s.gsub(*args) self.instance_variable_set(:@path, new_str) end |
#join(*args, &block) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/utils/file_path.rb', line 17 def join( *args, &block ) if block_given? yield self.class.new( super(*args) ) else self.class.new( super(*args) ) end end |
#relative_path_from(ancestor_dir) ⇒ Object
132 133 134 135 136 |
# File 'lib/utils/file_path.rb', line 132 def relative_path_from( ancestor_dir ) depth = ancestor_dir.to_s.split(File::SEPARATOR).length relative_path = self.to_s.split(File::SEPARATOR) relative_path[(depth)..-1].join(File::SEPARATOR).fwf_filepath end |
#touch ⇒ Object
66 67 68 69 70 71 |
# File 'lib/utils/file_path.rb', line 66 def touch FileUtils.touch( self ) return true rescue Errno::EACCESS return false end |
#touch_dir ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/utils/file_path.rb', line 73 def touch_dir FileUtils.mkdir_p( self ) return true rescue Errno::EEXIST return true rescue Errno::EACCESS return false end |
#up ⇒ Object
27 28 29 |
# File 'lib/utils/file_path.rb', line 27 def up self.class.new( self.join("..") ). end |
#without_ext ⇒ Object
124 125 126 |
# File 'lib/utils/file_path.rb', line 124 def without_ext self.gsub(/\.#{self.ext}$/, '') end |
#write(content = nil, &block) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/utils/file_path.rb', line 82 def write( content = nil, &block ) File.open( self, "w" ) do |f| f << content if content if block_given? yield f end end end |