Class: Contraption::Location

Inherits:
Object
  • Object
show all
Defined in:
lib/contraption/location.rb

Instance Method Summary collapse

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



58
59
60
# File 'lib/contraption/location.rb', line 58

def == other_location
  path == other_location.path
end

#cd(directory) ⇒ Object



32
33
34
35
36
# File 'lib/contraption/location.rb', line 32

def cd directory
  l = Location.new(pn+directory)
  l.create!  unless l.pn.directory?
  l
end

#create!Object



38
39
40
# File 'lib/contraption/location.rb', line 38

def create!
  pn.mkpath unless exist?
end

#executable?Boolean

Returns:



74
75
76
# File 'lib/contraption/location.rb', line 74

def executable?
  pn.executable?
end

#exist?Boolean

Returns:



62
63
64
# File 'lib/contraption/location.rb', line 62

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

#pathObject



19
20
21
# File 'lib/contraption/location.rb', line 19

def path
  pn.to_s
end

#read(file_name) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/contraption/location.rb', line 23

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

Returns:



66
67
68
# File 'lib/contraption/location.rb', line 66

def readable?
  pn.readable?
end

#remove(file_name) ⇒ Object



42
43
44
45
46
47
# File 'lib/contraption/location.rb', line 42

def remove file_name
  complete_name = pn+file_name
  return :file_does_not_exist unless complete_name.exist?

  complete_name.delete
end

#writable?Boolean

Returns:



70
71
72
# File 'lib/contraption/location.rb', line 70

def writable?
  pn.writable?
end

#write(file_name, content) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/contraption/location.rb', line 49

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