Class: AbsoluteRenamer::FileInfo

Inherits:
Object
  • Object
show all
Includes:
UseConfig
Defined in:
lib/absolute_renamer/file_info.rb

Overview

Class that represents each file to be renamed. It contains all informations about a file and, in the end, processes to its renaming.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UseConfig

#conf

Constructor Details

#initialize(path) ⇒ FileInfo

Initializes a FileInfo. path: the relative or absolute path of the file.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/absolute_renamer/file_info.rb', line 20

def initialize(path)
    @path = path
    @real_path = File.expand_path(@path)
    @dir_path = File.dirname(@real_path)
    @dir = File.directory?(@real_path)
    @name = File.basename(@real_path)
    unless @dir
        # TODO utiliser une conf :dot
        @ext = File.extname(@name)
        @name.gsub!(Regexp.new('.' << @ext << '$'), '') unless @ext.empty?
        @level = 0
    else
        @level = @real_path.split('/').size
    end
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def dir
  @dir
end

#dir_pathObject

Returns the value of attribute dir_path.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def dir_path
  @dir_path
end

#extObject

Returns the value of attribute ext.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def ext
  @ext
end

#levelObject

Returns the value of attribute level.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def level
  @level
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def name
  @name
end

#new_nameObject

Returns the value of attribute new_name.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def new_name
  @new_name
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def path
  @path
end

#real_pathObject

Returns the value of attribute real_path.



13
14
15
# File 'lib/absolute_renamer/file_info.rb', line 13

def real_path
  @real_path
end

Class Method Details

.compare_level(a, b) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/absolute_renamer/file_info.rb', line 94

def self.compare_level(a, b)
  if (a.level == b.level)
    return (a.name <= b.name) ? -1 : 1
  else
    return (a.level < b.level) ? 1 : -1
  end
end

Instance Method Details

#copyObject

Copies the file.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/absolute_renamer/file_info.rb', line 64

def copy
    display_change
    if @dir
        if @real_path != conf[:options][:dest]
            FileUtils.cp_r(@real_path, conf[:options][:dest])
        else
            puts "#{real_path} ignored"
        end
    else
        File.copy(@real_path, new_path, false)
    end
end

#display_changeObject

Displays the action that will be done on the file. some_fileinfo.display_change # => “rename a_file.txt –> A_File.TXT”



44
45
46
# File 'lib/absolute_renamer/file_info.rb', line 44

def display_change
    puts "#{conf[:options][:mode]} #{@real_path.sub(Dir.pwd+'/', '')} --> #{new_path.sub(Dir.pwd+'/', '')}"
end

#inspectObject

Returns a description of a FileInfo

some_fileinfo.inspect           # => "File: hello_world pdf"


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

def inspect
    "File: #{@name} #{@ext}"
end

Creates a symbolic link to the file.



84
85
86
87
88
89
90
91
92
# File 'lib/absolute_renamer/file_info.rb', line 84

def link
    display_change
    begin
        File.symlink(@real_path, new_path)
    rescue NotImplemented
        # TODO trouver mieux
        puts "Error: cannot create symlinks"
    end
end

#moveObject

Moves a file. Moving to the same directories is just like renaming.



78
79
80
81
# File 'lib/absolute_renamer/file_info.rb', line 78

def move
    display_change
    File.move(@real_path, new_path, false)
end

#new_pathObject

Returns the new path of the file.



49
50
51
52
53
54
55
# File 'lib/absolute_renamer/file_info.rb', line 49

def new_path
    if conf[:options][:dest].nil? or conf[:options][:dest].empty?
        File.join(@dir_path, @new_name)
    else
        File.join(conf[:options][:dest], @new_name)
    end
end

#renameObject

Renames the file.



58
59
60
61
# File 'lib/absolute_renamer/file_info.rb', line 58

def rename
    display_change
    File.rename(@real_path, new_path)
end