Class: Shog::Path

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, outoftree, absolute = false) ⇒ Path

Returns a new instance of Path.



15
16
17
18
19
# File 'lib/path.rb', line 15

def initialize(path, outoftree, absolute = false)
  @path = path
  @outoftree = outoftree
  @absolute = absolute
end

Class Attribute Details

.pwdObject

Returns the value of attribute pwd.



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

def pwd
  @pwd
end

Instance Attribute Details

#absoluteObject (readonly)

Returns the value of attribute absolute.



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

def absolute
  @absolute
end

#outoftreeObject (readonly)

Returns the value of attribute outoftree.



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

def outoftree
  @outoftree
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.make(path, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/path.rb', line 25

def self.make(path, params = {})
  if path.is_a?(String)
    abolute = false
    if params.key?(:absolute) and params[:absolute]
      path = File.expand_path(path)
      absolute = true
    elsif not params.key?(:root) and not params[:root]
      path = File.join(Path.pwd, path)
    end
    path = Path.normalize(path)
    outoftree = if params.key?(:outoftree)
                  params[:outoftree]
                else
                  false # by default we assume input file
                end
    return Path.new(path, outoftree, absolute)
  elsif path.is_a?(Path)
    if params.key?(:outoftree) and params[:outoftree] != path.outoftree
      return Path.new(path.path, params[:outoftree])
    else
      return path
    end
  elsif path.is_a?(PathSet)
    return self.make(path.single_path, params)
  else
    raise "Cannot make path from #{path}"
  end
end

.normalize(path) ⇒ Object



21
22
23
# File 'lib/path.rb', line 21

def self.normalize(path)
  Pathname.new(path).cleanpath.to_s
end

Instance Method Details

#dirObject



54
55
56
57
# File 'lib/path.rb', line 54

def dir
  path = File.dirname(@path)
  Path.new(path, @outoftree)
end

#to_strObject



63
64
65
66
67
68
69
70
# File 'lib/path.rb', line 63

def to_str
  # as we build files by ninja from inside outoftree directory then outs should not have any prefixes, but inputs should be accessed by '../'
  if @outoftree or @absolute
    @path
  else
    File.join("..", @path)
  end
end

#with_suffix(suffix) ⇒ Object



59
60
61
# File 'lib/path.rb', line 59

def with_suffix(suffix)
  Path.new(Path.normalize(@path + suffix), @outoftree)
end