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 ⇒ Object
- .media_root=(path) ⇒ Object
- .rewrite(&rule) ⇒ Object
- .rewrite_rules ⇒ 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”)
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/media-path.rb', line 72 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
61 62 63 |
# File 'lib/media-path.rb', line 61 def absolute @absolute end |
#media_root ⇒ Object
67 68 69 |
# File 'lib/media-path.rb', line 67 def media_root @media_root end |
Class Method Details
.basename_without_extension(file) ⇒ Object
263 264 265 |
# File 'lib/media-path.rb', line 263 def self.basename_without_extension(file) return File.basename(file.sub(/\.\w+$/, '')) end |
.change_extension(file, extension) ⇒ Object
268 269 270 |
# File 'lib/media-path.rb', line 268 def self.change_extension(file, extension) return file.sub(/\.\w+$/, ".#{extension}") end |
.check_directory_path(path) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/media-path.rb', line 15 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
10 11 12 |
# File 'lib/media-path.rb', line 10 def self.first_directory(*choices) choices.find { |file| File.directory?(File.(file)) } end |
.first_file(*choices) ⇒ Object
5 6 7 |
# File 'lib/media-path.rb', line 5 def self.first_file(*choices) choices.find { |file| File.file?(File.(file)) } end |
.media_root ⇒ Object
24 25 26 |
# File 'lib/media-path.rb', line 24 def self.media_root @media_root end |
.media_root=(path) ⇒ Object
28 29 30 |
# File 'lib/media-path.rb', line 28 def self.media_root=(path) @media_root = self.check_directory_path(path) end |
.rewrite(&rule) ⇒ Object
Note:
It make problems in case of reloading
55 56 57 58 |
# File 'lib/media-path.rb', line 55 def self.rewrite(&rule) # @@rewrite_rules.push(&rule) # WTF? @rewrite_rules += [rule] end |
.rewrite_rules ⇒ Object
43 44 45 |
# File 'lib/media-path.rb', line 43 def self.rewrite_rules @rewrite_rules ||= Array.new end |
.rewrite_rules=(rules) ⇒ Object
47 48 49 |
# File 'lib/media-path.rb', line 47 def self.rewrite_rules=(rules) @rewrite_rules = rules end |
.root=(path) ⇒ Object
38 39 40 |
# File 'lib/media-path.rb', line 38 def self.root=(path) @root = self.check_directory_path(path) end |
.without_extension(file) ⇒ Object
258 259 260 |
# File 'lib/media-path.rb', line 258 def self.without_extension(file) return file.sub(/\.\w+$/, '') end |
Instance Method Details
#+(segment) ⇒ Object
144 145 146 147 148 |
# File 'lib/media-path.rb', line 144 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?
130 131 132 133 |
# File 'lib/media-path.rb', line 130 def ==(another) raise TypeError unless another.is_a?(self.class) @absolute == another.absolute end |
#append(&block) ⇒ Object
186 187 188 |
# File 'lib/media-path.rb', line 186 def append(&block) File.open(@absolute, "a", &block) end |
#basename ⇒ Object
167 168 169 |
# File 'lib/media-path.rb', line 167 def basename File.basename(@absolute) end |
#empty? ⇒ Boolean
247 248 249 |
# File 'lib/media-path.rb', line 247 def empty? (self.directory? and self.children.empty?) || File.zero?() end |
#entries ⇒ Object
155 156 157 158 159 |
# File 'lib/media-path.rb', line 155 def entries Dir["#@absolute/*"].map do |path| self.class.new(path) end end |
#exist? ⇒ Boolean
232 233 234 |
# File 'lib/media-path.rb', line 232 def exist? File.exist?(@absolute) end |
#extension(file) ⇒ Object
MediaPathname(“x.html.erb”).extname
> “.erb”
198 199 200 201 |
# File 'lib/media-path.rb', line 198 def extension(file) self.to_s[/\.\w+?$/] # html.erb. # return @path.last.sub(/\.(\w+)$/, '\1') # dalsi moznost end |
#hidden? ⇒ Boolean
242 243 244 |
# File 'lib/media-path.rb', line 242 def hidden? self.basename.to_s.match(/^\./).to_bool end |
#inspect ⇒ Object
162 163 164 |
# File 'lib/media-path.rb', line 162 def inspect %["file://#@absolute"] end |
#join(*segments) ⇒ Object
137 138 139 140 141 |
# File 'lib/media-path.rb', line 137 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
237 238 239 |
# File 'lib/media-path.rb', line 237 def load Kernel.load(@absolute) end |
#make_executable ⇒ Object
alias_method :childrens, :children
215 216 217 |
# File 'lib/media-path.rb', line 215 def make_executable File.chmod(0755, @filename) end |
#make_unexecutable ⇒ Object
220 221 222 |
# File 'lib/media-path.rb', line 220 def make_unexecutable File.chmod(0644, @filename) end |
#md5(file) ⇒ Object
252 253 254 255 |
# File 'lib/media-path.rb', line 252 def md5(file) require 'digest/md5' Digest::MD5.hexdigest(File.read(file)) end |
#parent ⇒ Object
116 117 118 119 |
# File 'lib/media-path.rb', line 116 def parent parent = File.(File.join(@absolute, "..")) MediaPath.new(parent) end |
#read(&block) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/media-path.rb', line 177 def read(&block) if block_given? File.open(@absolute, "r", &block) else File.read(@absolute) end end |
#relative ⇒ Object
108 109 110 111 112 113 |
# File 'lib/media-path.rb', line 108 def relative path = @absolute.dup path[self.root] = String.new path.sub!(%r[^/], "") return path end |
#rewrite(&block) ⇒ Object
191 192 193 |
# File 'lib/media-path.rb', line 191 def rewrite(&block) File.open(@absolute, "w+", &block) end |
#run(command) ⇒ Object
225 226 227 228 229 |
# File 'lib/media-path.rb', line 225 def run(command) Dir.chdir(self) do return %x(#{command}) end end |
#to_a ⇒ Object
204 205 206 |
# File 'lib/media-path.rb', line 204 def to_a return self.to_s.split("/") end |
#url ⇒ Object
122 123 124 125 126 127 |
# File 'lib/media-path.rb', line 122 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
209 210 211 |
# File 'lib/media-path.rb', line 209 def without_extension return self.to_s.sub(/#{extension}$/, '') end |
#write(&block) ⇒ Object
172 173 174 |
# File 'lib/media-path.rb', line 172 def write(&block) File.open(@absolute, "w", &block) end |