Class: Polyamory::RootedPathname

Inherits:
Pathname
  • Object
show all
Defined in:
lib/polyamory/rooted_pathname.rb

Overview

A kind of Pathname that keeps a reference to a root directory and is able to return a relativized pathname from that particular root.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, root_path = nil) ⇒ RootedPathname

Returns a new instance of RootedPathname.



17
18
19
20
# File 'lib/polyamory/rooted_pathname.rb', line 17

def initialize(path, root_path = nil)
  super(path)
  self.root = root_path
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



7
8
9
# File 'lib/polyamory/rooted_pathname.rb', line 7

def root
  @root
end

Class Method Details

.glob(patterns, root) ⇒ Object

Find pathnames matching the glob pattern and assign to them a root



10
11
12
13
14
15
# File 'lib/polyamory/rooted_pathname.rb', line 10

def self.glob(patterns, root)
  patterns = Array(patterns)
  Dir[*patterns].map do |path|
    self.new(path, root)
  end
end

Instance Method Details

#+(other) ⇒ Object

Add to the current path; the result has the current path as root



45
46
47
48
49
# File 'lib/polyamory/rooted_pathname.rb', line 45

def +(other)
  result = self.class.new(plus(@path, other.to_s))
  result.root ||= self
  result
end

#=~(pattern) ⇒ Object

Perform a regex match only on the relative portion of the path



40
41
42
# File 'lib/polyamory/rooted_pathname.rb', line 40

def =~(pattern)
  relative.to_s =~ pattern
end

#in_dir?(dir) ⇒ Boolean

Check if current path is contained in directory

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/polyamory/rooted_pathname.rb', line 34

def in_dir? dir
  self == dir or
    self.to_s.index(File.join(dir, '')) == 0
end

#relativeObject

Return the relative portion of the path from root



28
29
30
31
# File 'lib/polyamory/rooted_pathname.rb', line 28

def relative
  return self if relative?
  @relativized ||= relative_path_from root
end