file_crawler 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 663f7b97d589d043cf9d9467f466c5740c494b68
4
- data.tar.gz: a93c71887ad38ef8154b986838a7b0a1621b5ee8
3
+ metadata.gz: b18468fb9ef26484e074f797eca9d4323ea34012
4
+ data.tar.gz: b970cc673130d37e5f46b6b59b2eb000b7dd8789
5
5
  SHA512:
6
- metadata.gz: 88ed5c4b6a96bac111c6bf5d338ef35f3e1affcf192f910a1eb505455a88d53396e085576e8444b2ddd0f80bc233d8b9a786f87fe137d595768237d90371dba4
7
- data.tar.gz: 18016ed7d0b2c27c5b0cc3918f1294914bb69e564698a3e467fee92b185840e99dd3bf06c4c905887220a26a3fea087af00d0c872410154e3c554817caff285e
6
+ metadata.gz: b2d1fadd395355fbda390bbc1683a0752077fd3cec402f15512b0613a9e0201cfd16fed8ad99aa715f328e941dd58ff748ae401cb6122e280fcf58685fdca405
7
+ data.tar.gz: 7fbb2d29b40af52deb561cabf703a9bd768e822eb54a05c2596b5bd8b436ccc4ea6bbe83136208a1365f8af0be97f7e26e6764162f2980c3efdad690faf51e69
@@ -3,18 +3,14 @@ require_relative 'finder/command'
3
3
  module FileCrawler
4
4
 
5
5
  class Finder
6
- attr_accessor :path
7
6
  attr_reader :rows
8
7
 
9
8
  include Command::Collect
10
9
  include Command::Move
11
- include Command::Organize
12
10
  include Command::Search
13
11
 
14
- def initialize(path = nil)
15
- @path = path
12
+ def initialize
16
13
  @rows = []
17
- select_in_path(path) unless path.nil?
18
14
  end
19
15
  end
20
16
 
@@ -23,18 +19,8 @@ module FileCrawler
23
19
  # exntesion [Array]
24
20
  # extension_in_directory [Array]
25
21
  def self.search(path, conditions = {})
26
- finder = FileCrawler::Finder.new(path)
27
-
28
- case
29
- when !conditions[:extension].nil?
30
- finder.select_with_extension(conditions[:extension])
31
- when conditions[:directory] == true
32
- finder.select_directories
33
- when conditions[:directory] == false
34
- finder.select_files
35
- when !conditions[:extension_in_directory].nil?
36
- finder.select_with_extension_in_directory(conditions[:extension_in_directory])
37
- end
22
+ finder = FileCrawler::Finder.new
23
+ finder.search(path, conditions)
38
24
 
39
25
  finder.rows
40
26
  end
@@ -45,37 +31,15 @@ module FileCrawler
45
31
  def self.move(path, destination, conditions = {})
46
32
  raise ArgumentError unless File.directory?(destination)
47
33
 
48
- search_conditions = {
49
- directory: true,
50
- extension_in_directory: conditions[:extension_in_directory]
51
- }
52
- search_conditions[:directory] = nil unless search_conditions[:extension_in_directory].nil?
53
-
54
- directories = search(path, search_conditions)
55
-
56
34
  finder = FileCrawler::Finder.new
35
+ finder.search(path).move(destination)
57
36
 
58
- case
59
- when conditions[:numbering] == true
60
- finder.move_directories_with_numbering(directories, destination)
61
- else
62
- finder.move_directories_not_exist_destination(directories, destination)
63
- end
37
+ finder.rows
64
38
  end
65
39
 
66
40
  # conditions
67
41
  # - if dont have extension_in_directory, directory true
68
- def self.collect(directories, conditions = {})
69
- search_conditions = {
70
- directory: true,
71
- extension_in_directory: conditions[:extension_in_directory]
72
- }
73
- search_conditions[:directory] = nil unless search_conditions[:extension_in_directory].nil?
74
-
75
- files = directories.map {|path|
76
- search(path, search_conditions)
77
- }.flatten
78
-
42
+ def self.collect(path, conditions = {})
79
43
  finder = FileCrawler::Finder.new
80
44
  unless conditions[:regexs].nil?
81
45
  conditions[:regexs].each {|regex|
@@ -83,18 +47,23 @@ module FileCrawler
83
47
  }
84
48
  end
85
49
 
86
- finder.collect(files, conditions)
50
+ finder.search(path).collect(conditions)
51
+
52
+ finder.rows
87
53
  end
88
54
 
89
- def self.organize(directories, destination, conditions = {})
55
+ def self.organize(path, destination, conditions = {})
90
56
  finder = FileCrawler::Finder.new
91
57
 
92
- result = collect(directories, conditions).map {|key, value|
93
- directory = destination + '/' + key
94
- finder.move_directories_with_numbering(value, directory)
95
- }.flatten
58
+ unless conditions[:regexs].nil?
59
+ conditions[:regexs].each {|regex|
60
+ finder.regexs << regex
61
+ }
62
+ end
63
+
64
+ finder.search(path).collect(conditions).move_from_collection(destination)
96
65
 
97
- result
66
+ finder.rows
98
67
  end
99
68
 
100
69
  end
@@ -1,4 +1,3 @@
1
1
  require_relative 'command/collect'
2
2
  require_relative 'command/move'
3
- require_relative 'command/organize'
4
3
  require_relative 'command/search'
@@ -26,21 +26,10 @@ module FileCrawler
26
26
  @regexs ||= []
27
27
  end
28
28
 
29
- def collect(file_paths, conditions = {})
30
- collection = collect_into_filename(file_paths)
31
-
32
- collection
33
- end
34
-
35
- def decide_index_for_collect(string)
36
- if !regexs.empty?
37
- regexs.each {|regex|
38
- return $1 unless regex.pattern.match(string).nil?
39
- }
40
- end
41
-
42
- pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー  ]+/
43
- return string.strip.scan(pattern).first
29
+ def collect(conditions = {})
30
+ tap {
31
+ @rows = collect_into_filename(@rows)
32
+ }
44
33
  end
45
34
 
46
35
  def collect_into_filename(file_paths)
@@ -56,6 +45,20 @@ module FileCrawler
56
45
  hash
57
46
  end
58
47
 
48
+ def decide_index_for_collect(string)
49
+ if !regexs.empty?
50
+ regexs.each {|regex|
51
+ return $1 unless regex.pattern.match(string).nil?
52
+ }
53
+ end
54
+
55
+ pattern = /[\p{Hiragana}|\p{Katakana}|\p{Han}|[a-zA-Z0-9]ー  ]+/
56
+ result = string.strip.scan(pattern).first
57
+ return result unless result.nil?
58
+
59
+ string
60
+ end
61
+
59
62
  end
60
63
  end
61
64
  end
@@ -5,28 +5,24 @@ module FileCrawler
5
5
  module Command
6
6
  module Move
7
7
 
8
- def move_directories_not_exist_destination(directories, destination)
9
- move_targets = directories.select {|directory|
10
- valiable_to_move?(directory, destination)
8
+ def move(destination)
9
+ tap {
10
+ @rows = move_with_numbering(@rows, destination)
11
11
  }
12
- not_move_targets = directories.select {|directory|
13
- !valiable_to_move?(directory, destination)
14
- }
15
-
16
- create_directory_if_needed(destination)
17
- FileUtils.mv(move_targets, destination)
12
+ end
18
13
 
19
- move_targets.map {|directory|
20
- destination + '/' + File.basename(directory)
21
- } + not_move_targets
14
+ def move_from_collection(destination)
15
+ tap {
16
+ @rows = move_from_collection_with_numbering(@rows, destination)
17
+ }
22
18
  end
23
19
 
24
- def move_directories_with_numbering(directories, destination)
20
+ def move_with_numbering(source, destination)
25
21
  move_targets = []
26
22
  not_move_targets = []
27
23
  rename_targets = []
28
24
 
29
- directories.each {|directory|
25
+ source.each {|directory|
30
26
  if is_same?(directory, destination)
31
27
  not_move_targets << directory
32
28
  next
@@ -57,6 +53,15 @@ module FileCrawler
57
53
  } + not_move_targets + renamed_targets
58
54
  end
59
55
 
56
+ def move_from_collection_with_numbering(source, destination)
57
+ result = source.map {|key, value|
58
+ directory = destination + '/' + key
59
+ move_with_numbering(value, directory)
60
+ }.flatten
61
+
62
+ result
63
+ end
64
+
60
65
  def create_directory_if_needed(directory)
61
66
  Dir.mkdir(directory, 0777) unless File.exist?(directory)
62
67
  end
@@ -66,8 +71,8 @@ module FileCrawler
66
71
 
67
72
  index = 1
68
73
  new_filename = "#{filename} (#{index})"
69
- while File.exist?(new_filename)
70
- i += 1
74
+ while exist_file?(new_filename, destination)
75
+ index += 1
71
76
  new_filename = "#{filename} (#{index})"
72
77
  end
73
78
 
@@ -3,80 +3,36 @@ module FileCrawler
3
3
  module Command
4
4
  module Search
5
5
 
6
- def select_in_path(path)
6
+ def search(path, conditions = {})
7
7
  tap {
8
- @rows = find_rows_in_path(path)
8
+ @rows = search_directories(path)
9
9
  }
10
10
  end
11
11
 
12
- def find_rows_in_path(path)
13
- Dir.entries(path).select {|item|
14
- !item.start_with?('.')
15
- }.map {|item|
16
- path + '/' + item
17
- }
18
- end
19
-
20
- def select_with_extension(extension)
21
- tap {
22
- @rows = find_rows_with_extension(extension, rows)
23
- }
24
- end
25
-
26
- def find_rows_with_extension(extension, resource)
27
- find_rows_files(resource).select {|item|
28
- extension.any? {|e|
29
- extname = File.extname(item).downcase
30
- extname == ".#{e.downcase}"
31
- }
32
- }
33
- end
34
-
35
- def select_files
36
- tap {
37
- @rows = find_rows_files(rows)
38
- }
39
- end
12
+ def search_directories(path)
13
+ directories = search_directories_in_path(path)
14
+ return [path] if directories.empty?
40
15
 
41
- def find_rows_files(resource)
42
- resource.select {|item|
43
- !File.directory?(item)
16
+ result = []
17
+ directories.each {|item|
18
+ result += search_directories(item)
44
19
  }
45
- end
46
20
 
47
- def select_directories
48
- tap {
49
- @rows = find_rows_directories(rows)
50
- }
21
+ result
51
22
  end
52
23
 
53
- def find_rows_directories(resource)
54
- resource.select {|item|
24
+ def search_directories_in_path(path)
25
+ find_files_in_path(path).select {|item|
55
26
  File.directory?(item)
56
27
  }
57
28
  end
58
29
 
59
- def select_with_extension_in_directory(extension)
60
- tap {
61
- @rows = find_rows_with_extension_in_directories(extension, find_rows_directories(rows))
62
- }
63
- end
64
-
65
- def find_rows_with_extension_in_directories(extension, directories)
66
- directories.select {|directory|
67
- variable_to_exist_with_extension_in_directory(extension, directory)
68
- }
69
- end
70
-
71
- def variable_to_exist_with_extension_in_directory(extension, directory)
72
- subfiles = find_rows_in_path(directory)
73
- result = find_rows_with_extension(extension, subfiles)
74
- return true if result.size > 0
75
-
76
- subdirectories = find_rows_directories(subfiles).select {|subdirectory|
77
- variable_to_exist_with_extension_in_directory(extension, subdirectory)
30
+ def find_files_in_path(path)
31
+ Dir.entries(path).select {|item|
32
+ !item.start_with?('.')
33
+ }.map {|item|
34
+ path + '/' + item
78
35
  }
79
- subdirectories.size > 0
80
36
  end
81
37
  end
82
38
  end
@@ -1,3 +1,3 @@
1
1
  module FileCrawler
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_crawler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hirohisa Kawasaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-29 00:00:00.000000000 Z
11
+ date: 2016-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,6 @@ files:
67
67
  - lib/file_crawler/finder/command.rb
68
68
  - lib/file_crawler/finder/command/collect.rb
69
69
  - lib/file_crawler/finder/command/move.rb
70
- - lib/file_crawler/finder/command/organize.rb
71
70
  - lib/file_crawler/finder/command/search.rb
72
71
  - lib/file_crawler/version.rb
73
72
  homepage: https://github.com/hirohisa/file_crawler
@@ -1,8 +0,0 @@
1
- module FileCrawler
2
- class Finder
3
- module Command
4
- module Organize
5
- end
6
- end
7
- end
8
- end