camino 0.0.0

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.
Files changed (2) hide show
  1. data/lib/camino.rb +100 -0
  2. metadata +62 -0
data/lib/camino.rb ADDED
@@ -0,0 +1,100 @@
1
+ class Path < String
2
+
3
+ def initialize(string = Path.root)
4
+ super(string)
5
+ end
6
+
7
+ def /(obj)
8
+ Path.new(File.join(self, obj.to_s))
9
+ end
10
+
11
+ def +(obj)
12
+ Path.new(self.to_s + obj.to_s)
13
+ end
14
+
15
+ def exist?
16
+ File.exists?(self)
17
+ end
18
+
19
+ alias exists? exist?
20
+
21
+ def directory?
22
+ File.directory?(self)
23
+ end
24
+
25
+ def file?
26
+ exist? && !directory?
27
+ end
28
+
29
+ def files(pattern = "*")
30
+ if directory?
31
+ Dir.glob(File.join(self, pattern)).collect{|p|Path.new(p)}
32
+ else
33
+ raise "Not a directory:(#{self})"
34
+ end
35
+ end
36
+
37
+ def read
38
+ if file?
39
+ File.open(self).read
40
+ else
41
+ raise "Not a file:(#{self})"
42
+ end
43
+ end
44
+
45
+ def directory
46
+ File.dirname(self)
47
+ end
48
+
49
+ def file
50
+ File.basename(self)
51
+ end
52
+
53
+ def extension
54
+ File.extname(self)[1..-1]
55
+ end
56
+
57
+ def filename
58
+ File.basename(self, File.extname(self))
59
+ end
60
+
61
+ def inspect
62
+ "#<Path:#{(object_id * 2).to_s(16)}:#{self}>"
63
+ end
64
+
65
+ def self.relative
66
+ path = get_path_from_caller(caller)
67
+ dir = File.dirname(path)
68
+ raise "Path doesn't seem to exist:#{last_call.inspect}" if !File.exist?(dir)
69
+ Path.new(dir)
70
+ end
71
+
72
+ def self.home
73
+ Path.new(ENV['HOME'])
74
+ end
75
+
76
+ def self.root
77
+ Path.new("/")
78
+ end
79
+
80
+ def self.pwd
81
+ Path.new(Dir.pwd)
82
+ end
83
+
84
+ private
85
+
86
+ def self.get_path_from_caller(caller)
87
+ last_call = caller.first
88
+ matches = last_call.scan(/(.*?):(\d*)(:in\s`.*')?$/)
89
+ raise "Path name conflicts with caller syntax:#{last_call.inspect}" if matches.length != 1
90
+ path, line_number, method = matches.first
91
+ path
92
+ end
93
+
94
+ end
95
+
96
+ class String
97
+ def to_path
98
+ Path.new(self)
99
+ end
100
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: camino
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 0
9
+ version: 0.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Noel Warren
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2014-06-28 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Sugar syntax for your path
22
+ email: noelwarr@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/camino.rb
31
+ has_rdoc: true
32
+ homepage:
33
+ licenses:
34
+ - MIT
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.6
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: Sugar syntax for your path
61
+ test_files: []
62
+