Class: Pow::Base

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

Direct Known Subclasses

Directory, File

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, mode = nil, &block) ⇒ Base

Returns a new instance of Base.



39
40
41
# File 'lib/pow/pow.rb', line 39

def initialize(path, mode=nil, &block)
  self.path = ::File.expand_path(path)
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



16
17
18
# File 'lib/pow/pow.rb', line 16

def path
  @path
end

Class Method Details

.open(*paths, &block) ⇒ Object

:nodoc:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pow/pow.rb', line 18

def self.open(*paths, &block) #:nodoc:
  paths.collect! {|path| path.to_s}
  path = ::File.join(paths)
  
  klass = if ::File.directory?(path)
    Directory
  elsif ::File.file?(path)
    File
  else
    self
  end
  
  klass.new(path, &block)
end

.working_directoryObject Also known as: cwd

Returns the path to the current working directory as a Pow::Dir object.



34
35
36
# File 'lib/pow/pow.rb', line 34

def self.working_directory
  Pow(Dir.getwd)
end

Instance Method Details

#/(name = nil) ⇒ Object

Shortcut to append info onto a Pow object

tmp = Pow("tmp")
readme_path = tmp/"subdir"/"README"


119
120
121
# File 'lib/pow/pow.rb', line 119

def /(name=nil)
  self.class.open(path, name)
end

#<=>(other) ⇒ Object

Sort based on name



141
142
143
# File 'lib/pow/pow.rb', line 141

def <=>(other)
  name <=> other
end

#==(other) ⇒ Object

Compares the path string



124
125
126
# File 'lib/pow/pow.rb', line 124

def ==(other)
  other.to_s == self.to_s
end

#=~(pattern) ⇒ Object

Regex match on the basename for the path

path = Pow("/tmp/a_file.txt")
path =~ /file/ #=> 2
path =~ /tmp/ #=> nil


136
137
138
# File 'lib/pow/pow.rb', line 136

def =~(pattern)
  name =~ pattern
end

#[](*paths, &block) ⇒ Object

Shortcut to combine paths

tmp = Pow("tmp")
readme_path = tmp["subdir", :README]


112
113
114
# File 'lib/pow/pow.rb', line 112

def [](*paths, &block)
  Pow(path, *paths, &block)
end

#accessed_atObject Also known as: atime



88
89
90
# File 'lib/pow/pow.rb', line 88

def accessed_at
  ::File.atime(path)
end

#changed_atObject Also known as: ctime



93
94
95
# File 'lib/pow/pow.rb', line 93

def changed_at
  ::File.ctime(path)
end

#copy_to(dest) ⇒ Object Also known as: cp



51
52
53
# File 'lib/pow/pow.rb', line 51

def copy_to(dest)
  path_must_exist
end

#copy_to!(dest) ⇒ Object Also known as: cp!



56
57
58
# File 'lib/pow/pow.rb', line 56

def copy_to!(dest)
  path_must_exist
end

#create(mode = "a+", &block) ⇒ Object

Creates a new path. If there is a . in the name, then assume it is a file Block returns a file object when a file is created



184
185
186
# File 'lib/pow/pow.rb', line 184

def create(mode="a+", &block)
  name =~ /\./ ? create_file(mode, &block) : create_directory(&block)
end

#create_directory(&block) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/pow/pow.rb', line 196

def create_directory(&block)
  FileUtils.mkdir_p(self.to_s)
  dir = Directory.new(self.to_s)

  dir.open(&block) if block_given?

  dir
end

#create_file(mode = "a+", &block) ⇒ Object



188
189
190
191
192
193
194
# File 'lib/pow/pow.rb', line 188

def create_file(mode="a+", &block)
  FileUtils.mkdir_p(::File.dirname(self.to_s))
  file = File.new(self.to_s)
  file.open(mode, &block) # Create the file

  file
end

#directory?Boolean Also known as: is_directory?

Returns:

  • (Boolean)


163
164
165
# File 'lib/pow/pow.rb', line 163

def directory?
  ::File.directory?(path)
end

#empty?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/pow/pow.rb', line 178

def empty?
  true
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/pow/pow.rb', line 128

def eql?(other)
  other.eql? self.to_s
end

#exists?Boolean Also known as: exist?

Returns:

  • (Boolean)


158
159
160
# File 'lib/pow/pow.rb', line 158

def exists?
  ::File.exist? path
end

#extensionObject

Returns the extension (the portion of file name in path after the period).



154
155
156
# File 'lib/pow/pow.rb', line 154

def extension
  ::File.extname(path)[1..-1] # Gets rid of the dot
end

#file?Boolean Also known as: is_file?

Returns:

  • (Boolean)


168
169
170
# File 'lib/pow/pow.rb', line 168

def file?
  ::File.file?(path)
end

#modified_atObject Also known as: mtime



98
99
100
# File 'lib/pow/pow.rb', line 98

def modified_at
  ::File.mtime(path)
end

#move_to(dest) ⇒ Object Also known as: mv



61
62
63
# File 'lib/pow/pow.rb', line 61

def move_to(dest)
  path_must_exist
end

#move_to!(dest) ⇒ Object Also known as: mv!



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

def move_to!(dest)
  path_must_exist
end

#name(with_extension = true) ⇒ Object

Returns the last component of the filename given, can optionally exclude the extension

Parameters

with_extension<Boolean>



149
150
151
# File 'lib/pow/pow.rb', line 149

def name(with_extension=true)
  ::File.basename path, (with_extension ? "" : ".#{extension}")
end

#open(mode = "r", &block) ⇒ Object



43
44
45
# File 'lib/pow/pow.rb', line 43

def open(mode="r", &block)
  create(mode, &block)
end

#parentObject

Returns the path the is one level up from the current path



174
175
176
# File 'lib/pow/pow.rb', line 174

def parent
  Pow(::File.dirname(path))
end

#permissionsObject



80
81
82
# File 'lib/pow/pow.rb', line 80

def permissions
  ("%o" % ::File.stat(path.to_s).mode)[2..-1].to_i # Forget about the first two numbers
end

#permissions=(mode) ⇒ Object



75
76
77
78
# File 'lib/pow/pow.rb', line 75

def permissions=(mode)
  mode = mode.to_s.to_i(8) # convert from octal
  FileUtils.chmod(mode, path)
end

#rename_to(new_name) ⇒ Object



71
72
73
# File 'lib/pow/pow.rb', line 71

def rename_to(new_name)
  move_to(parent / new_name)
end

#sizeObject



84
85
86
# File 'lib/pow/pow.rb', line 84

def size
  ::File.size(path)
end

#to_sObject Also known as: to_str

String representation of the expanded path



104
105
106
# File 'lib/pow/pow.rb', line 104

def to_s
  path
end

#write(string) ⇒ Object



47
48
49
# File 'lib/pow/pow.rb', line 47

def write(string)
  create_file.write(string)
end