Class: MediaPath
- Inherits:
-
Object
- Object
- MediaPath
- Defined in:
- lib/media-path.rb
Instance Attribute Summary collapse
- #absolute ⇒ Object (also: #path, #to_s) readonly
- #media_root ⇒ Object
- #root ⇒ Object
Class Method Summary collapse
- .basename_without_extension(file) ⇒ Object
- .change_extension(file, extension) ⇒ Object
- .check_directory_path(path) ⇒ Object
- .first_directory(*choices) ⇒ Object
- .first_file(*choices) ⇒ Object
- .media_root=(path) ⇒ Object
- .rewrite(&rule) ⇒ Object
- .rewrite_rules=(rules) ⇒ Object
- .root ⇒ Object
- .root=(path) ⇒ Object
- .without_extension(file) ⇒ Object
Instance Method Summary collapse
- #+(segment) ⇒ Object
- #==(another) ⇒ Object (also: #eql?)
- #append(&block) ⇒ Object
- #basename ⇒ Object
- #empty? ⇒ Boolean
- #entries ⇒ Object
- #exist? ⇒ Boolean
-
#extension(file) ⇒ Object
MediaPathname(“x.html.erb”).extname => “.erb”.
- #hidden? ⇒ Boolean
-
#initialize(path) ⇒ MediaPath
constructor
MediaPath.new(“public/uploads”) MediaPath.new(“#Merb.root/public/uploads”).
- #inspect ⇒ Object
- #join(*segments) ⇒ Object
- #load ⇒ Object
-
#make_executable ⇒ Object
alias_method :childrens, :children.
- #make_unexecutable ⇒ Object
- #md5(file) ⇒ Object
- #parent ⇒ Object
- #read(&block) ⇒ Object
- #relative ⇒ Object
- #rewrite(&block) ⇒ Object
- #run(command) ⇒ Object
- #to_a ⇒ Object
- #url ⇒ Object
- #without_extension ⇒ Object
- #write(&block) ⇒ Object
Constructor Details
#initialize(path) ⇒ MediaPath
MediaPath.new(“public/uploads”) MediaPath.new(“#Merb.root/public/uploads”)
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/media-path.rb', line 70 def initialize(path) self.root = self.class.root raise ArgumentError.new("Argument for creating new MediaPath must be string") unless path.is_a?(String) raise ArgumentError.new("MediaPath can't be created from empty string") if path.empty? path.chomp!("/") unless path == "/" # no trailing / if path.match(%r{^/}) || path.match(%r[^[A-Z]//]) # / or C:// @absolute = File.(path) else @absolute = File.(File.join(self.class.root, path)) end raise Errno::ENOENT, "File does not exist: '#{@absolute}'" unless File.exist?(@absolute) end |
Instance Attribute Details
#absolute ⇒ Object (readonly) Also known as: path, to_s
59 60 61 |
# File 'lib/media-path.rb', line 59 def absolute @absolute end |
#media_root ⇒ Object
65 66 67 |
# File 'lib/media-path.rb', line 65 def media_root @media_root end |
Class Method Details
.basename_without_extension(file) ⇒ Object
261 262 263 |
# File 'lib/media-path.rb', line 261 def self.basename_without_extension(file) return File.basename(file.sub(/\.\w+$/, '')) end |
.change_extension(file, extension) ⇒ Object
266 267 268 |
# File 'lib/media-path.rb', line 266 def self.change_extension(file, extension) return file.sub(/\.\w+$/, ".#{extension}") end |
.check_directory_path(path) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/media-path.rb', line 19 def self.check_directory_path(path) return if path.nil? # because of exceptions raise ArgumentError.new("MediaPath can't be created from empty string") if path.empty? path = File.(path) raise ArgumentError, "MediaPath '#{path}' doesn't exist" unless File.directory?(path) return path end |
.first_directory(*choices) ⇒ Object
14 15 16 |
# File 'lib/media-path.rb', line 14 def self.first_directory(*choices) choices.find { |file| File.directory?(File.(file)) } end |
.first_file(*choices) ⇒ Object
9 10 11 |
# File 'lib/media-path.rb', line 9 def self.first_file(*choices) choices.find { |file| File.file?(File.(file)) } end |
.media_root=(path) ⇒ Object
29 30 31 |
# File 'lib/media-path.rb', line 29 def self.media_root=(path) @@media_root = self.check_directory_path(path) end |
.rewrite(&rule) ⇒ Object
Note:
It make problems in case of reloading
53 54 55 56 |
# File 'lib/media-path.rb', line 53 def self.rewrite(&rule) # @@rewrite_rules.push(&rule) # WTF? @@rewrite_rules += [rule] end |
.rewrite_rules=(rules) ⇒ Object
45 46 47 |
# File 'lib/media-path.rb', line 45 def self.rewrite_rules=(rules) @@rewrite_rules = rules end |
.root=(path) ⇒ Object
39 40 41 |
# File 'lib/media-path.rb', line 39 def self.root=(path) @@root = self.check_directory_path(path) end |
.without_extension(file) ⇒ Object
256 257 258 |
# File 'lib/media-path.rb', line 256 def self.without_extension(file) return file.sub(/\.\w+$/, '') end |
Instance Method Details
#+(segment) ⇒ Object
142 143 144 145 146 |
# File 'lib/media-path.rb', line 142 def +(segment) raise ArgumentError unless segment.is_a?(String) raise ArgumentError if segment.match(%r{^/}) self.class.new("#@absolute/#{segment}") end |
#==(another) ⇒ Object Also known as: eql?
128 129 130 131 |
# File 'lib/media-path.rb', line 128 def ==(another) raise TypeError unless another.is_a?(self.class) @absolute == another.absolute end |
#append(&block) ⇒ Object
184 185 186 |
# File 'lib/media-path.rb', line 184 def append(&block) File.open(@absolute, "a", &block) end |
#basename ⇒ Object
165 166 167 |
# File 'lib/media-path.rb', line 165 def basename File.basename(@absolute) end |
#empty? ⇒ Boolean
245 246 247 |
# File 'lib/media-path.rb', line 245 def empty? (self.directory? and self.children.empty?) || File.zero?() end |
#entries ⇒ Object
153 154 155 156 157 |
# File 'lib/media-path.rb', line 153 def entries Dir["#@absolute/*"].map do |path| self.class.new(path) end end |
#exist? ⇒ Boolean
230 231 232 |
# File 'lib/media-path.rb', line 230 def exist? File.exist?(@absolute) end |
#extension(file) ⇒ Object
MediaPathname(“x.html.erb”).extname
> “.erb”
196 197 198 199 |
# File 'lib/media-path.rb', line 196 def extension(file) self.to_s[/\.\w+?$/] # html.erb. # return @path.last.sub(/\.(\w+)$/, '\1') # dalsi moznost end |
#hidden? ⇒ Boolean
240 241 242 |
# File 'lib/media-path.rb', line 240 def hidden? self.basename.to_s.match(/^\./).to_bool end |
#inspect ⇒ Object
160 161 162 |
# File 'lib/media-path.rb', line 160 def inspect %["file://#@absolute"] end |
#join(*segments) ⇒ Object
135 136 137 138 139 |
# File 'lib/media-path.rb', line 135 def join(*segments) raise ArgumentError if segments.any? { |segment| not segment.is_a?(String) } raise ArgumentError if segments.any? { |segment| segment.match(%r{^/}) } self.class.new(File.join(@absolute, *segments)) end |
#load ⇒ Object
235 236 237 |
# File 'lib/media-path.rb', line 235 def load Kernel.load(@absolute) end |
#make_executable ⇒ Object
alias_method :childrens, :children
213 214 215 |
# File 'lib/media-path.rb', line 213 def make_executable File.chmod(0755, @filename) end |
#make_unexecutable ⇒ Object
218 219 220 |
# File 'lib/media-path.rb', line 218 def make_unexecutable File.chmod(0644, @filename) end |
#md5(file) ⇒ Object
250 251 252 253 |
# File 'lib/media-path.rb', line 250 def md5(file) require 'digest/md5' Digest::MD5.hexdigest(File.read(file)) end |
#parent ⇒ Object
114 115 116 117 |
# File 'lib/media-path.rb', line 114 def parent parent = File.(File.join(@absolute, "..")) MediaPath.new(parent) end |
#read(&block) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/media-path.rb', line 175 def read(&block) if block_given? File.open(@absolute, "r", &block) else File.read(@absolute) end end |
#relative ⇒ Object
106 107 108 109 110 111 |
# File 'lib/media-path.rb', line 106 def relative path = @absolute.dup path[self.root] = String.new path.sub!(%r[^/], "") return path end |
#rewrite(&block) ⇒ Object
189 190 191 |
# File 'lib/media-path.rb', line 189 def rewrite(&block) File.open(@absolute, "w+", &block) end |
#run(command) ⇒ Object
223 224 225 226 227 |
# File 'lib/media-path.rb', line 223 def run(command) Dir.chdir(self) do return %x(#{command}) end end |
#to_a ⇒ Object
202 203 204 |
# File 'lib/media-path.rb', line 202 def to_a return self.to_s.split("/") end |
#url ⇒ Object
120 121 122 123 124 125 |
# File 'lib/media-path.rb', line 120 def url url = @absolute.dup url[self.media_root] = String.new rules = self.class.rewrite_rules rules.empty? ? url : rules.map { |rule| url = rule.call(url) }.last end |
#without_extension ⇒ Object
207 208 209 |
# File 'lib/media-path.rb', line 207 def without_extension return self.to_s.sub(/#{extension}$/, '') end |
#write(&block) ⇒ Object
170 171 172 |
# File 'lib/media-path.rb', line 170 def write(&block) File.open(@absolute, "w", &block) end |