Class: Pathstring

Inherits:
String
  • Object
show all
Extended by:
Pedlar
Defined in:
lib/pathstring.rb,
lib/pathstring/version.rb

Overview

we want a string, a little bit more intelligent though, so…

Direct Known Subclasses

PathstringRoot

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, relative_path = nil) ⇒ Pathstring

Returns a new instance of Pathstring.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pathstring.rb', line 40

def initialize(path, relative_path=nil)
  stringified = path.to_s
  # first arg to String
  super stringified

  # set relative origin, with '' as default
  # to allow setting absolute path in any case
  relative_root_with relative_path || ''
  absolute_with stringified

  # if path argument is not absolute, then it's relative...
  relative_with stringified if absolute != stringified
  # if path argument is not set yet and we're given a relative_path argument
  relative_with @absolute.relative_path_from(@relative_root) if !@relative && relative_path

  # Pathstring specific methods definitions
  pathstring_specifics
end

Instance Attribute Details

#content=(value) ⇒ Object (writeonly)

only writer, getter is implicitly defined within the read method



32
33
34
# File 'lib/pathstring.rb', line 32

def content=(value)
  @content = value
end

Class Method Details

.join(*joins) ⇒ Object

one utility class method, allows to instantiate a Pathstring with a path elements list



36
37
38
# File 'lib/pathstring.rb', line 36

def self.join(*joins)
  new File.join(*joins)
end

Instance Method Details

#readObject Also known as: content

read through a mere delegation to pathname fill up content attribute in the process



79
80
81
# File 'lib/pathstring.rb', line 79

def read
  @content = @absolute.read if exist?
end

#rename(new_name) ⇒ Object

rename not only string value, but resets the internal pathname to return apt values to basename or dirname for instance



85
86
87
88
89
# File 'lib/pathstring.rb', line 85

def rename(new_name)
  relative_with new_name.sub(@relative_root.to_s, '')
  absolute_with @relative
  replace new_name
end

#save(content = nil) ⇒ Object

save file content, if we have a content and if the dirname path exists



92
93
94
95
# File 'lib/pathstring.rb', line 92

def save(content=nil)
  @content = content if content
  open('w') { |f| f.write @content } if dirname.exist? && !@content.nil?
end

#save!(content = nil) ⇒ Object

save file content if we have a content, but forces the dirname creation if it doesn’t exist



99
100
101
102
# File 'lib/pathstring.rb', line 99

def save!(content=nil)
  FileUtils.mkdir_p dirname
  save content || read
end

#with_relative_root(*root) ⇒ Object

(re)set the relative origin set the relative facade in the process



61
62
63
64
65
66
67
68
# File 'lib/pathstring.rb', line 61

def with_relative_root(*root)
  # Tap because i like tap
  # No, tap because i want this to be chainable with `new` for example
  tap do |p|
    relative_root_with File.join(root)
    relative_with @absolute.relative_path_from(@relative_root)
  end
end