Class: Contraption::Location
- Inherits:
-
Object
- Object
- Contraption::Location
- Defined in:
- lib/contraption/location.rb
Instance Method Summary collapse
- #==(other_location) ⇒ Object
- #cd(directory) ⇒ Object
- #create! ⇒ Object
- #destroy ⇒ Object
- #entries ⇒ Object
- #executable? ⇒ Boolean
- #exist? ⇒ Boolean
-
#initialize(path = ".") ⇒ Location
constructor
A new instance of Location.
- #list(ext = /.*/) ⇒ Object
- #path ⇒ Object
- #read(file_name) ⇒ Object
- #readable? ⇒ Boolean
- #remove(file_name) ⇒ Object
- #to_s ⇒ Object
- #writable? ⇒ Boolean
- #write(file_name, content) ⇒ Object
Constructor Details
#initialize(path = ".") ⇒ Location
Returns a new instance of Location.
6 7 8 |
# File 'lib/contraption/location.rb', line 6 def initialize path="." @pn = Pathname.new path end |
Instance Method Details
#==(other_location) ⇒ Object
70 71 72 |
# File 'lib/contraption/location.rb', line 70 def == other_location path == other_location.path end |
#cd(directory) ⇒ Object
44 45 46 47 48 |
# File 'lib/contraption/location.rb', line 44 def cd directory l = Location.new(pn+directory) l.create! unless l.pn.directory? l end |
#create! ⇒ Object
50 51 52 |
# File 'lib/contraption/location.rb', line 50 def create! pn.mkpath unless exist? end |
#destroy ⇒ Object
40 41 42 |
# File 'lib/contraption/location.rb', line 40 def destroy FileUtils.rm_rf pn. end |
#entries ⇒ Object
19 20 21 |
# File 'lib/contraption/location.rb', line 19 def entries pn.entries.map{|e| e.(pn) } end |
#executable? ⇒ Boolean
86 87 88 |
# File 'lib/contraption/location.rb', line 86 def executable? pn.executable? end |
#exist? ⇒ Boolean
74 75 76 |
# File 'lib/contraption/location.rb', line 74 def exist? pn.exist? end |
#list(ext = /.*/) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/contraption/location.rb', line 10 def list ext=/.*/ ext = Array(ext).join('|') ext = Regexp.new ext pn.find .reject { |f| f == pn } .select { |f| ext.match f.extname } .map{ |f| f.basename.to_s } end |
#path ⇒ Object
23 24 25 |
# File 'lib/contraption/location.rb', line 23 def path pn.to_s end |
#read(file_name) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/contraption/location.rb', line 31 def read file_name complete_name = pn+file_name return :file_does_not_exist unless complete_name.exist? return :file_not_readable unless complete_name.readable? return complete_name.read unless block_given? complete_name.open("r") { |f| yield f } end |
#readable? ⇒ Boolean
78 79 80 |
# File 'lib/contraption/location.rb', line 78 def readable? pn.readable? end |
#remove(file_name) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/contraption/location.rb', line 54 def remove file_name complete_name = pn+file_name return :file_does_not_exist unless complete_name.exist? complete_name.delete end |
#to_s ⇒ Object
27 28 29 |
# File 'lib/contraption/location.rb', line 27 def to_s pn..to_s end |
#writable? ⇒ Boolean
82 83 84 |
# File 'lib/contraption/location.rb', line 82 def writable? pn.writable? end |
#write(file_name, content) ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/contraption/location.rb', line 61 def write file_name, content complete_name = pn+file_name return :file_already_exists if complete_name.exist? complete_name.dirname.mkpath unless complete_name.dirname.exist? complete_name.open("w:UTF-8") {|f| f.print content} end |