AbsoluteRenamer 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
  require 'absolute_renamer/with_children'
2
2
 
3
3
  module AbsoluteRenamer
4
- # Parsers parent class.
5
- # Parsers must inherit of it.
6
- class IParser < AbsoluteRenamer::WithChildren
7
- end
4
+ # Parsers parent class.
5
+ # Parsers must inherit of it.
6
+ class IParser < AbsoluteRenamer::WithChildren
7
+ end
8
8
  end
@@ -1,11 +1,11 @@
1
1
  require 'absolute_renamer/with_children'
2
2
 
3
3
  module AbsoluteRenamer
4
- # Plugins parent class.
5
- # Plugins must inherit of it.
6
- class IPlugin < AbsoluteRenamer::WithChildren
7
- def self.symbol
8
- name.intern
9
- end
4
+ # Plugins parent class.
5
+ # Plugins must inherit of it.
6
+ class IPlugin < AbsoluteRenamer::WithChildren
7
+ def self.symbol
8
+ name.intern
10
9
  end
10
+ end
11
11
  end
@@ -1,11 +1,11 @@
1
1
  # Extension of existing File class.
2
2
  class << File
3
- # Returns the extension of a file.
4
- # path: the path of the file
5
- # dot: starting from the end, number of dots to count before cuting extension.
6
- def extname(path, dot = 1)
7
- pattern = (0...dot).inject('') { |pat,x| pat << '\.[^\.]+' } << '$'
8
- ext = File.basename(path).match(pattern).to_s
9
- ext.empty? ? "" : ext[1..ext.length]
10
- end
3
+ # Returns the extension of a file.
4
+ # path: the path of the file
5
+ # dot: starting from the end, number of dots to count before cuting extension.
6
+ def extname(path, dot = 1)
7
+ pattern = (0...dot).inject('') { |pat,x| pat << '\.[^\.]+' } << '$'
8
+ ext = File.basename(path).match(pattern).to_s
9
+ ext.empty? ? "" : ext[1..ext.length]
10
+ end
11
11
  end
@@ -1,28 +1,28 @@
1
1
  # Extension of existing Hash class
2
2
  class Hash
3
- # Returns a new hash containing resulting of a recursive merge between two hashes
4
- # new_hash: the hash to merge
5
- # h1 = { :key1 => :val1, :key2 => { :key21 => :val21 } }
6
- # h2 = { :key2 => { :key22 => :val22 }, :key3 => val3 }
7
- # h1.deep_marge! h2 #=> { :key1 => val1, :key2 => { :key21 => :val21. :key22 => :val22 }, test3 => val3 }
8
- # h1 #=> { :key1 => :val1, :key2 => { :key21 => :val21 } }
9
- def deep_merge(new_hash)
10
- self.clone.deep_merge! new_hash
11
- end
3
+ # Returns a new hash containing resulting of a recursive merge between two hashes
4
+ # new_hash: the hash to merge
5
+ # h1 = { :key1 => :val1, :key2 => { :key21 => :val21 } }
6
+ # h2 = { :key2 => { :key22 => :val22 }, :key3 => val3 }
7
+ # h1.deep_marge! h2 #=> { :key1 => val1, :key2 => { :key21 => :val21. :key22 => :val22 }, test3 => val3 }
8
+ # h1 #=> { :key1 => :val1, :key2 => { :key21 => :val21 } }
9
+ def deep_merge(new_hash)
10
+ self.clone.deep_merge! new_hash
11
+ end
12
12
 
13
- # Adds the contents of new_hash to another hash recusively
14
- # new_hash: the hash to merge
15
- # h1 = { :key1 => :val1, :key2 => { :key21 => :val21 } }
16
- # h2 = { :key2 => { :key22 => :val22 }, :key3 => val3 }
17
- # h1.deep_marge! h2
18
- # h1 #=> { :key1 => val1, :key2 => { :key21 => :val21. :key22 => :val22 }, test3 => val3 }
19
- def deep_merge!(new_hash)
20
- merge! new_hash do |key, v1, v2|
21
- if v1.is_a? Hash
22
- v1.deep_merge! v2
23
- else
24
- self[key] = v2
25
- end
26
- end
13
+ # Adds the contents of new_hash to another hash recusively
14
+ # new_hash: the hash to merge
15
+ # h1 = { :key1 => :val1, :key2 => { :key21 => :val21 } }
16
+ # h2 = { :key2 => { :key22 => :val22 }, :key3 => val3 }
17
+ # h1.deep_marge! h2
18
+ # h1 #=> { :key1 => val1, :key2 => { :key21 => :val21. :key22 => :val22 }, test3 => val3 }
19
+ def deep_merge!(new_hash)
20
+ merge! new_hash do |key, v1, v2|
21
+ if v1.is_a? Hash
22
+ v1.deep_merge! v2
23
+ else
24
+ self[key] = v2
25
+ end
27
26
  end
27
+ end
28
28
  end
@@ -1,26 +1,26 @@
1
1
  # Extension of existing String class
2
2
  class String
3
- # Returns a camelized version of a string
4
- # word_separator: a regular expression used to separate words.
5
- # str = "hello.THE world"
6
- # str.camelize # => "Hello.The World"
7
- # str # => "hello.THE world"
8
- def camelize
9
- self.clone.camelize!
10
- end
3
+ # Returns a camelized version of a string
4
+ # word_separator: a regular expression used to separate words.
5
+ # str = "hello.THE world"
6
+ # str.camelize # => "Hello.The World"
7
+ # str # => "hello.THE world"
8
+ def camelize
9
+ self.clone.camelize!
10
+ end
11
11
 
12
- # Camelizes a string and returns it.
13
- # str = "Hello.THE World"
14
- # str.camelize! # => "Hello.The World"
15
- # str # => "Hello.The World"
16
- def camelize!
17
- word_separators = /[^a-zA-Z0-9']/
18
- self.downcase!
19
- self.each_char.each_with_index do |c,i|
20
- if self[i-1].chr =~ word_separators or i.zero?
21
- self[i] = c.upcase if c =~ /[a-z]/
22
- end
23
- end
24
- self
12
+ # Camelizes a string and returns it.
13
+ # str = "Hello.THE World"
14
+ # str.camelize! # => "Hello.The World"
15
+ # str # => "Hello.The World"
16
+ def camelize!
17
+ word_separators = /[^a-zA-Z0-9']/
18
+ self.downcase!
19
+ self.each_char.each_with_index do |c,i|
20
+ if self[i-1].chr =~ word_separators or i.zero?
21
+ self[i] = c.upcase if c =~ /[a-z]/
22
+ end
25
23
  end
24
+ self
25
+ end
26
26
  end
@@ -4,70 +4,69 @@ require 'absolute_renamer/use_config'
4
4
  require 'pp'
5
5
 
6
6
  module AbsoluteRenamer
7
- # Class in charge of the command line parsing.
8
- class Parser
9
- class << self
10
- include AbsoluteRenamer::UseConfig
7
+ # Class in charge of the command line parsing.
8
+ class Parser
9
+ class << self
10
+ include AbsoluteRenamer::UseConfig
11
11
 
12
- # Calls all registred parsers.
13
- # The parsers are written in configuration files.
14
- # The core parsers are automaticaly added.
15
- def parse_cmd_line
16
- ARGV.options do |parser|
17
- begin
18
- list = AbsoluteRenamer::IParser.children
19
- list.each do |class_name|
20
- class_name.add_options(parser, conf[:options])
21
- end
22
- parser.parse!
23
- rescue RuntimeError => ex
24
- STDERR.puts(ex)
25
- exit 1
26
- end
27
- conf[:files] = self.get_files(ARGV, 0) || []
28
- conf[:options][:maxdepth] ||= 0
29
- conf[:options][:interactive] ||= :never
30
- conf[:options][:mode] ||= :rename
31
- pp conf.get if conf[:debug]
32
- end
12
+ # Calls all registred parsers.
13
+ # The parsers are written in configuration files.
14
+ # The core parsers are automaticaly added.
15
+ def parse_cmd_line
16
+ ARGV.options do |parser|
17
+ begin
18
+ list = AbsoluteRenamer::IParser.children
19
+ list.each do |class_name|
20
+ class_name.add_options(parser, conf[:options])
33
21
  end
22
+ parser.parse!
23
+ rescue RuntimeError => ex
24
+ STDERR.puts(ex)
25
+ exit 1
26
+ end
27
+ conf[:files] = self.get_files(ARGV, 0) || []
28
+ pp conf.get if conf[:debug]
29
+ end
30
+ end
34
31
 
35
- # Creates a list of all files and directories to rename.
36
- # All options that have not been matched are considered as path.
37
- # For directories, if the recursive otpion is set to true (conf[:rec] == true),
38
- # files are searched in sub directories.
39
- #
40
- # list: a list of path to explore
41
- # depth: maximum recursion depth
42
- #
43
- # Returns the files/directories list
44
- def get_files(list, depth)
45
- files = []
46
- options = conf[:options]
47
-
48
- list.each do |entry|
49
- return files unless File.exists?(entry)
50
- is_dir = File.directory?(entry)
51
- mod_dir = options[:dir]
52
- depth_ok = (depth < options[:maxdepth] or options[:maxdepth].zero?)
53
- mod_rec = (options[:rec] and depth_ok)
32
+ # Creates a list of all files and directories to rename.
33
+ # All options that have not been matched are considered as path.
34
+ # For directories, if the recursive otpion is set to true (conf[:rec] == true),
35
+ # files are searched in sub directories.
36
+ #
37
+ # list: a list of path to explore
38
+ # depth: maximum recursion depth
39
+ #
40
+ # Returns the files/directories list
41
+ def get_files(list, depth)
42
+ files = []
43
+ options = conf[:options]
54
44
 
55
- add_dir = (is_dir and mod_dir)
56
- add_file = (!is_dir and !mod_dir)
57
- add_sub = (is_dir and (mod_rec or (!mod_dir and depth < 1)))
45
+ list.each do |entry|
46
+ if File.exists?(entry)
47
+ is_dir = File.directory?(entry)
48
+ mod_dir = options[:dir]
49
+ depth_ok = (depth < options[:maxdepth] or options[:maxdepth].zero?)
50
+ mod_rec = (options[:rec] and depth_ok)
58
51
 
59
- files << FileInfo.new(entry) if (add_dir or add_file)
60
- files += self.get_files(self.get_subentries(entry), depth + 1) if (add_sub)
61
- end
62
- files
63
- end
52
+ add_dir = (is_dir and mod_dir)
53
+ add_file = (!is_dir and !mod_dir)
54
+ add_sub = (is_dir and (mod_rec or (!mod_dir and depth < 1)))
64
55
 
65
- # Returns files and directories contained in +path+.
66
- def get_subentries(path)
67
- files = Dir.entries(path)
68
- files.delete_if { |file| file[0,1] == '.' }
69
- files.collect! { |file| path + '/' + file }
70
- end
56
+ files << FileInfo.new(entry) if (add_dir or add_file)
57
+ files += self.get_files(self.get_subentries(entry), depth + 1) if (add_sub)
58
+ end
71
59
  end
60
+
61
+ files
62
+ end
63
+
64
+ # Returns files and directories contained in +path+.
65
+ def get_subentries(path)
66
+ files = Dir.entries(path)
67
+ files.delete_if { |file| file[0,1] == '.' }
68
+ files.collect! { |file| File.join(path, file) }
69
+ end
72
70
  end
71
+ end
73
72
  end
@@ -1,11 +1,11 @@
1
1
  require 'absolute_renamer/config'
2
2
 
3
3
  module AbsoluteRenamer
4
- # Module that provide configuration usage.
5
- module UseConfig
6
- # Returns the configuration class
7
- def conf
8
- @conf ||= AbsoluteRenamer::Config
9
- end
4
+ # Module that provide configuration usage.
5
+ module UseConfig
6
+ # Returns the configuration class
7
+ def conf
8
+ @conf ||= AbsoluteRenamer::Config
10
9
  end
10
+ end
11
11
  end
@@ -0,0 +1,3 @@
1
+ module AbsoluteRenamer
2
+ VERSION = "1.1.1"
3
+ end
@@ -1,22 +1,22 @@
1
1
  require 'absolute_renamer/use_config'
2
2
 
3
3
  module AbsoluteRenamer
4
- # Class allowing childs listing.
5
- class WithChildren
6
- include AbsoluteRenamer::UseConfig
4
+ # Class allowing childs listing.
5
+ class WithChildren
6
+ include AbsoluteRenamer::UseConfig
7
7
 
8
- @children = []
9
- class << self
10
- attr_reader :children
8
+ @children = []
9
+ class << self
10
+ attr_reader :children
11
11
 
12
- # Inheritance callback.
13
- # When a class inherit from a WithChildren class, it is added to
14
- # the childs list of this class.
15
- # This list is available as the +children+ attribute.
16
- def inherited(by)
17
- @children << by
18
- by.instance_variable_set(:@children, [])
19
- end
20
- end
12
+ # Inheritance callback.
13
+ # When a class inherit from a WithChildren class, it is added to
14
+ # the childs list of this class.
15
+ # This list is available as the +children+ attribute.
16
+ def inherited(by)
17
+ @children << by
18
+ by.instance_variable_set(:@children, [])
19
+ end
21
20
  end
21
+ end
22
22
  end
@@ -2,16 +2,10 @@ require 'test_helper'
2
2
 
3
3
  class ConfigTest < Test::Unit::TestCase
4
4
  context "The Config instance" do
5
-
5
+
6
6
  should "exist" do
7
7
  assert_not_nil AbsoluteRenamer::Config
8
8
  end
9
-
10
- should "raise ENOENT if config file not found" do
11
- assert_raise(Errno::ENOENT) do
12
- AbsoluteRenamer::Config.load('a file that must not be found')
13
- end
14
- end
15
9
 
16
10
  should "not raise ENOENT if config file is found" do
17
11
  assert_nothing_raised(Errno::ENOENT) do
@@ -38,7 +32,6 @@ class ConfigTest < Test::Unit::TestCase
38
32
  AbsoluteRenamer::Config[:test_key] = :test_val
39
33
  assert_equal(:test_val, AbsoluteRenamer::Config[:test_key])
40
34
  end
41
-
42
35
  end
43
36
 
44
37
  end
@@ -2,27 +2,28 @@ require 'test_helper'
2
2
 
3
3
  class FileInfoTest < Test::Unit::TestCase
4
4
  context "A FileInfo instance" do
5
- context "loaded with some/path/to/a_file.txt" do
5
+ context "loaded with /tmp/test-absrenamer.txt" do
6
6
 
7
7
  setup do
8
- @pwd = Dir.pwd
9
- @fileinfo = AbsoluteRenamer::FileInfo.new('some/path/to/a_file.txt')
8
+ AbsoluteRenamer::Config.load('conf/absrenamer/absrenamer.conf')
9
+ `touch /tmp/test-absrenamer.txt`
10
+ @fileinfo = AbsoluteRenamer::FileInfo.new('/tmp/test-absrenamer.txt')
10
11
  end
11
12
 
12
- should "have as name : a_file" do
13
- assert_equal('a_file', @fileinfo.name)
13
+ should "have as name : test-absrenamer" do
14
+ assert_equal('test-absrenamer', @fileinfo.name)
14
15
  end
15
16
 
16
- should "have as dir_path : pwd/some/path/to" do
17
- assert_equal(@pwd + '/some/path/to', @fileinfo.dir_path)
17
+ should "have as dir_path : /tmp" do
18
+ assert_equal('/tmp', @fileinfo.dir_path)
18
19
  end
19
20
 
20
- should "have as path : some/path/to/a_file.txt" do
21
- assert_equal('some/path/to/a_file.txt', @fileinfo.path)
21
+ should "have as path : /tmp/test-absrenamer.txt" do
22
+ assert_equal('/tmp/test-absrenamer.txt', @fileinfo.path)
22
23
  end
23
24
 
24
- should "have as real_path : pwd/some/path/to/a_file.txt" do
25
- assert_equal(@pwd + '/some/path/to/a_file.txt', @fileinfo.real_path)
25
+ should "have as real_path : /tmp/test-absrenamer.txt" do
26
+ assert_equal('/tmp/test-absrenamer.txt', @fileinfo.real_path)
26
27
  end
27
28
 
28
29
  should "have as ext : txt" do
@@ -39,27 +40,27 @@ class FileInfoTest < Test::Unit::TestCase
39
40
 
40
41
  end
41
42
 
42
- context "loaded with lib/absolute_renamer" do
43
+ context "loaded with /usr/bin" do
43
44
 
44
45
  setup do
45
- @pwd = Dir.pwd
46
- @fileinfo = AbsoluteRenamer::FileInfo.new('lib/absolute_renamer')
46
+ AbsoluteRenamer::Config.load('conf/absrenamer/absrenamer.conf')
47
+ @fileinfo = AbsoluteRenamer::FileInfo.new('/usr/bin')
47
48
  end
48
49
 
49
- should "have as name : absolute_renamer" do
50
- assert_equal('absolute_renamer', @fileinfo.name)
50
+ should "have as name : bin" do
51
+ assert_equal('bin', @fileinfo.name)
51
52
  end
52
53
 
53
- should "have as dir_path : pwd/lib" do
54
- assert_equal(@pwd + '/lib', @fileinfo.dir_path)
54
+ should "have as dir_path : /usr" do
55
+ assert_equal('/usr', @fileinfo.dir_path)
55
56
  end
56
57
 
57
- should "have as path : lib/absolute_renamer" do
58
- assert_equal('lib/absolute_renamer', @fileinfo.path)
58
+ should "have as path : usr/bin" do
59
+ assert_equal('/usr/bin', @fileinfo.path)
59
60
  end
60
61
 
61
- should "have as real_path : pwd/lib/absolute_renamer" do
62
- assert_equal(@pwd + '/lib/absolute_renamer', @fileinfo.real_path)
62
+ should "have as real_path : /usr/bin" do
63
+ assert_equal('/usr/bin', @fileinfo.real_path)
63
64
  end
64
65
 
65
66
  should "have as ext : nil" do