pathstring 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ class Pathstring < String
2
+ VERSION = "0.0.1"
3
+ end
data/lib/pathstring.rb ADDED
@@ -0,0 +1,129 @@
1
+ # encoding: utf-8
2
+
3
+ require 'pedlar'
4
+
5
+ # we want a string, a little bit more intelligent though, so...
6
+ class Pathstring < String
7
+
8
+ # delegations
9
+ extend Pedlar
10
+
11
+ # what we shamelessly steal from Pathname
12
+ def_delegator :@absolute, :to_s, :absolute
13
+ def_delegator :@absolute, :dirname, :absolute_dirname
14
+ # here with a failsafe thanks to Pedlar
15
+ safe_delegator :@relative, :to_s, :relative
16
+ safe_delegator :@relative, :dirname, :relative_dirname
17
+
18
+ # and even more
19
+ def_delegators :facade_delegate, :dirname, :absolute?, :relative?
20
+
21
+ # and that again
22
+ def_delegators :@absolute, :exist?, :file?, :basename, :extname, :join, :split,
23
+ :size, :stat, :children, :delete, :readlines, :open
24
+
25
+ # three interfaces
26
+ peddles Pathname
27
+ pathname_accessor :relative_root
28
+ pathname_writer :absolute
29
+ pathname_writer :relative
30
+
31
+ # only writer, getter is implicitly defined within the read method
32
+ attr_writer :content
33
+
34
+ # one utility class method, allows to instantiate a Pathstring with
35
+ # a path elements list
36
+ def self.join(*joins)
37
+ new File.join(*joins)
38
+ end
39
+
40
+ def initialize(path, relative_path=nil)
41
+ # first arg to String
42
+ super path
43
+
44
+ # set relative origin, with '' as default
45
+ # to allow setting absolute path in any case
46
+ relative_root_with relative_path || ''
47
+ absolute_with path
48
+
49
+ # if path argument is not absolute, then it's relative...
50
+ relative_with path if absolute != path
51
+ # if path argument is not set yet and we're given a relative_path argument
52
+ relative_with @absolute.relative_path_from(@relative_root) if !@relative && relative_path
53
+
54
+ # Pathstring specific methods definitions
55
+ pathstring_specifics
56
+ end
57
+
58
+ # (re)set the relative origin
59
+ # set the relative facade in the process
60
+ def with_relative_root(root)
61
+ # Tap because i like tap
62
+ # No, tap because i want this to be chainable with `new` for example
63
+ tap do |p|
64
+ relative_root_with root
65
+ relative_with @absolute.relative_path_from(@relative_root)
66
+ end
67
+ end
68
+
69
+ # definitions of relative! and absolute! that allow to swith facades
70
+ %w|absolute relative|.each do |face|
71
+ define_method "#{face}!".to_sym do
72
+ instance_variable_get("@#{face}") && replace(send(face))
73
+ end
74
+ end
75
+
76
+ # read through a mere delegation to pathname
77
+ # fill up content attribute in the process
78
+ def read
79
+ @content = @absolute.read if exist?
80
+ end
81
+
82
+ # rename not only string value, but resets the internal pathname
83
+ # to return apt values to basename or dirname for instance
84
+ def rename(new_name)
85
+ relative_with new_name.sub(@relative_root.to_s, '')
86
+ absolute_with @relative
87
+ replace new_name
88
+ end
89
+
90
+ # save file content, if we have a content and if the dirname path exists
91
+ def save(content=nil)
92
+ @content = content if content
93
+ open('w') { |f| f.write @content } if dirname.exist? && !@content.nil?
94
+ end
95
+
96
+ # save file content if we have a content,
97
+ # but forces the dirname creation if it doesn't exist
98
+ def save!(content=nil)
99
+ FileUtils.mkdir_p dirname
100
+ save content || read
101
+ end
102
+
103
+ # man ruby
104
+ alias :content :read
105
+
106
+ private
107
+
108
+ # fitting setter method for abolute resource
109
+ def absolute_setter(path)
110
+ relative_root.join(path).expand_path
111
+ end
112
+
113
+ # is the current facade absolute or relative ?
114
+ def facade_delegate
115
+ absolute == self ? @absolute : @relative
116
+ end
117
+
118
+ # Pathstring specific methode definitions
119
+ def pathstring_specifics
120
+ # anything that's called *basename* or *dirname* will have
121
+ # its basestring or dirstring couterpart
122
+ methods.grep(/basename|dirname/).each do |method|
123
+ define_singleton_method method.to_s.sub('name', 'string').to_sym do
124
+ (pathname = send method) && pathname.to_s
125
+ end
126
+ end
127
+ end
128
+
129
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pathstring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - lacravate
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pedlar
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A Pathname / String midway interface, to files and directories
47
+ email:
48
+ - lacravate@lacravate.fr
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - lib/pathstring.rb
54
+ - lib/pathstring/version.rb
55
+ homepage: https://github.com/lacravate/pathstring
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project: ! '[none]'
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A Pathname / String midway interface, to files and directories
79
+ test_files: []