Class: WarpDrive::Path

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

Overview

This class allows you to easily get the location of any file or path in a Warp Drive gem.

Example:

WarpDrive::Path.app.controllers.application_controller.rb
# => "/usr/local/lib/ruby/gems/1.8/gems/my_warp_drive_gem-1.2.3/lib/app/controllers/application_controller.rb"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, path) ⇒ Path

Returns a new instance of Path.



12
13
14
15
# File 'lib/warp_drive/path.rb', line 12

def initialize(parent, path)
  self.parent = parent
  self.path = path.to_s
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/warp_drive/path.rb', line 17

def method_missing(sym, *args)
  if sym == :rb || sym == :yml
    return self.to_s + ".#{sym}"
  else
    WarpDrive::Path.new(self, sym)
  end
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/warp_drive/path.rb', line 9

def parent
  @parent
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.method_missing(sym, *args) ⇒ Object



43
44
45
# File 'lib/warp_drive/path.rb', line 43

def method_missing(sym, *args)
  WarpDrive::Path.new(nil, sym)
end

Instance Method Details

#/(other) ⇒ Object



37
38
39
# File 'lib/warp_drive/path.rb', line 37

def /(other)
  File.expand_path(File.join(self.to_s, other.to_s))
end

#to_sObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/warp_drive/path.rb', line 25

def to_s
  paths = [self.path]
  par = self.parent
  until par.nil?
    paths << par.path
    par = par.parent
  end
  paths << File.join(WarpDrive::ROOT)
  path = File.expand_path(File.join(paths.reverse))
  return path
end