Class: Filename

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

Constant Summary collapse

DIR_SEPARATOR =
'/'
WINDOWS_DIR_SEPARATOR =
'\\'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Filename

Returns a new instance of Filename.



8
9
10
11
12
# File 'lib/filename.rb', line 8

def initialize filename
	filename = filename.gsub(WINDOWS_DIR_SEPARATOR, DIR_SEPARATOR)
	@path_items = filename.split(DIR_SEPARATOR).delete_if{|item| item.empty?}
	@absolute_path = filename.start_with? DIR_SEPARATOR
end

Instance Attribute Details

#path_itemsObject

Returns the value of attribute path_items.



6
7
8
# File 'lib/filename.rb', line 6

def path_items
  @path_items
end

Instance Method Details

#absolute_path?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/filename.rb', line 14

def absolute_path?
	@absolute_path
end

#extract_last_path_itemObject



30
31
32
# File 'lib/filename.rb', line 30

def extract_last_path_item
	Filename.new(nil_to_empty_string @path_items.last)
end

#relative_path?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/filename.rb', line 18

def relative_path?
	not absolute_path?
end

#remove_last_path_itemObject



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

def remove_last_path_item
	Filename.new(make_string(@path_items[0..-2]))
end

#to_sObject



46
47
48
# File 'lib/filename.rb', line 46

def to_s
	to_unix_s
end

#to_unix_sObject



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

def to_unix_s
	make_string @path_items
end

#to_windows_sObject



42
43
44
# File 'lib/filename.rb', line 42

def to_windows_s
	make_string @path_items, WINDOWS_DIR_SEPARATOR
end

#without_extensionObject



22
23
24
25
26
27
28
# File 'lib/filename.rb', line 22

def without_extension 
	if @path_items.size > 0 and @path_items.last.include? '.' then
		Filename.new(to_s.split('.')[0..-2].join('.'))
	else
		clone
	end
end