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…

Constant Summary collapse

VERSION =
"0.0.1"

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
# File 'lib/pathstring.rb', line 40

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

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

  # if path argument is not absolute, then it's relative...
  relative_with path if absolute != path
  # 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



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

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



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

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



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

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



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

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



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

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 root
    relative_with @absolute.relative_path_from(@relative_root)
  end
end