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



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

#destroyObject



40
41
42
# File 'lib/contraption/location.rb', line 40

def destroy
  FileUtils.rm_rf pn.expand_path
end

#entriesObject



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

def entries
  pn.entries.map{|e| e.expand_path(pn) }
end

#executable?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/contraption/location.rb', line 86

def executable?
  pn.executable?
end

#exist?Boolean

Returns:

  • (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

#pathObject



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

Returns:

  • (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_sObject



27
28
29
# File 'lib/contraption/location.rb', line 27

def to_s
  pn.expand_path.to_s
end

#writable?Boolean

Returns:

  • (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