Class: MediaPath

Inherits:
Object
  • Object
show all
Defined in:
lib/media-path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MediaPath

MediaPath.new(“public/uploads”) MediaPath.new(“#Merb.root/public/uploads”)

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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.expand_path(path)
  else
    @absolute = File.expand_path(File.join(self.class.root, path))
  end
  raise Errno::ENOENT, "File does not exist: '#{@absolute}'" unless File.exist?(@absolute)
end

Instance Attribute Details

#absoluteObject (readonly) Also known as: path, to_s

Since:

  • 0.0.1



61
62
63
# File 'lib/media-path.rb', line 61

def absolute
  @absolute
end

#media_rootObject

Since:

  • 0.0.1



67
68
69
# File 'lib/media-path.rb', line 67

def media_root
  @media_root
end

#rootObject

Since:

  • 0.0.1



67
68
69
# File 'lib/media-path.rb', line 67

def root
  @root
end

Class Method Details

.basename_without_extension(file) ⇒ Object

Since:

  • 0.0.1



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

Since:

  • 0.0.1



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

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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.expand_path(path)
  raise ArgumentError, "MediaPath '#{path}' doesn't exist" unless File.directory?(path)
  return path
end

.first_directory(*choices) ⇒ Object

Since:

  • 0.0.1



10
11
12
# File 'lib/media-path.rb', line 10

def self.first_directory(*choices)
  choices.find { |file| File.directory?(File.expand_path(file)) }
end

.first_file(*choices) ⇒ Object

Since:

  • 0.0.1



5
6
7
# File 'lib/media-path.rb', line 5

def self.first_file(*choices)
  choices.find { |file| File.file?(File.expand_path(file)) }
end

.media_rootObject

Since:

  • 0.0.1



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

Since:

  • 0.0.1



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_rulesObject

Since:

  • 0.0.1



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

.rootObject

Since:

  • 0.0.1



33
34
35
# File 'lib/media-path.rb', line 33

def self.root
  @root ||= Dir.pwd
end

.root=(path) ⇒ Object

Since:

  • 0.0.1



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

Since:

  • 0.0.1



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

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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?

Raises:

  • (TypeError)

Since:

  • 0.0.1



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

Since:

  • 0.0.1



186
187
188
# File 'lib/media-path.rb', line 186

def append(&block)
  File.open(@absolute, "a", &block)
end

#basenameObject

Since:

  • 0.0.1



167
168
169
# File 'lib/media-path.rb', line 167

def basename
  File.basename(@absolute)
end

#empty?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.1



247
248
249
# File 'lib/media-path.rb', line 247

def empty?
  (self.directory? and self.children.empty?) || File.zero?()
end

#entriesObject

Since:

  • 0.0.1



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

Returns:

  • (Boolean)

Since:

  • 0.0.1



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”

Since:

  • 0.0.1



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

Returns:

  • (Boolean)

Since:

  • 0.0.1



242
243
244
# File 'lib/media-path.rb', line 242

def hidden?
  self.basename.to_s.match(/^\./).to_bool
end

#inspectObject

Since:

  • 0.0.1



162
163
164
# File 'lib/media-path.rb', line 162

def inspect
  %["file://#@absolute"]
end

#join(*segments) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 0.0.1



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

#loadObject

Since:

  • 0.0.1



237
238
239
# File 'lib/media-path.rb', line 237

def load
  Kernel.load(@absolute)
end

#make_executableObject

alias_method :childrens, :children

Since:

  • 0.0.1



215
216
217
# File 'lib/media-path.rb', line 215

def make_executable
  File.chmod(0755, @filename)
end

#make_unexecutableObject

Since:

  • 0.0.1



220
221
222
# File 'lib/media-path.rb', line 220

def make_unexecutable
  File.chmod(0644, @filename)
end

#md5(file) ⇒ Object

Since:

  • 0.0.1



252
253
254
255
# File 'lib/media-path.rb', line 252

def md5(file)
  require 'digest/md5'
  Digest::MD5.hexdigest(File.read(file))
end

#parentObject

Since:

  • 0.0.1



116
117
118
119
# File 'lib/media-path.rb', line 116

def parent
  parent = File.expand_path(File.join(@absolute, ".."))
  MediaPath.new(parent)
end

#read(&block) ⇒ Object

Since:

  • 0.0.1



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

#relativeObject

Since:

  • 0.0.1



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

Since:

  • 0.0.1



191
192
193
# File 'lib/media-path.rb', line 191

def rewrite(&block)
  File.open(@absolute, "w+", &block)
end

#run(command) ⇒ Object

Since:

  • 0.0.1



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_aObject

Since:

  • 0.0.1



204
205
206
# File 'lib/media-path.rb', line 204

def to_a
  return self.to_s.split("/")
end

#urlObject

Since:

  • 0.0.1



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_extensionObject

Since:

  • 0.0.1



209
210
211
# File 'lib/media-path.rb', line 209

def without_extension
  return self.to_s.sub(/#{extension}$/, '')
end

#write(&block) ⇒ Object

Since:

  • 0.0.1



172
173
174
# File 'lib/media-path.rb', line 172

def write(&block)
  File.open(@absolute, "w", &block)
end