cleanup 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3cd8b930e88f0c816fb4d6141a8747ec1d8145a9
4
+ data.tar.gz: 81dc629fa598e47bece6324768a1d5af0fed6b31
5
+ SHA512:
6
+ metadata.gz: e4ede30c782945b85656440f96ca20bf5da6b5bedf3c23e5e7aa52f9e062814967eee419f2e9dcafaee9af4936a694d123f9524c7434aae26eb45f7e6c4590fe
7
+ data.tar.gz: 993484c48f32e0ecbaed7dedc324898ff24eac062d9eb912e9ba1bcf30963c53939f38537daddf7a3eb24a1248f8e15753d3eb42b1a89412c14b46a2379d65e6
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /config/rules.rb
11
+ rubocop.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in clean_up.gemspec
4
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 nickolai-sm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # CleanUp
2
+
3
+ Move and copy files and directories to appointed directories according defined conditions.
4
+
5
+ ## Usage
6
+
7
+ Define your rules in `config/rules.rb` file.
8
+
9
+ Example:
10
+
11
+ ```` ruby
12
+
13
+ CleanUp.define do
14
+ move do
15
+ source '~/Downloads'
16
+ target '~/'
17
+
18
+ file do
19
+ extension 'mp3'
20
+ dir 'Music'
21
+ end
22
+
23
+ file do
24
+ extension 'avi', 'mov'
25
+ dir 'Movies'
26
+ end
27
+
28
+ file do
29
+ pattern 'report_'
30
+ dir 'Documents/Reports'
31
+ end
32
+
33
+ file do
34
+ extension 'torrent'
35
+ dir '.Trash'
36
+ end
37
+ end
38
+ end
39
+
40
+ ````
41
+
42
+ and run
43
+
44
+ $ bin/cleanup
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nickolai-sm/clean_up.
49
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/cleanup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler'
4
+ Bundler.require
5
+
6
+ load 'config/rules.rb'
7
+
8
+ CleanUp.check
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'clean_up'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/clean_up.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'clean_up/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'cleanup'
8
+ spec.version = CleanUp::VERSION
9
+ spec.authors = ['Nickolai Smirnov']
10
+ spec.email = ['sm-nickolai@gmail.com']
11
+
12
+ spec.license = 'MIT'
13
+ spec.summary = 'Files and Directories clean up tool'
14
+ spec.homepage = 'https://github.com/nickolai-sm/clean_up'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'bin'
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.12'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'pry', '~> 0.10.3'
24
+ end
data/lib/clean_up.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'clean_up/version'
2
+
3
+ require 'clean_up/rules'
4
+ require 'clean_up/conditions'
5
+ require 'clean_up/folder'
6
+ require 'clean_up/folders_rules'
7
+ require 'clean_up/option_values'
8
+
9
+ module CleanUp
10
+ IGNORED_ENTRIES = %w(. .. .DS_Store .localized).freeze
11
+
12
+ class << self
13
+ def define(&block)
14
+ @folders_rules = FoldersRules.collect(&block)
15
+ end
16
+
17
+ def folders_rules
18
+ @folders_rules || []
19
+ end
20
+
21
+ def check
22
+ folders_rules.each do |folder_rules|
23
+ dir_entries(folder_rules.source).each do |entry|
24
+ if File.directory?(entry)
25
+ folder_rules.process_directory(entry)
26
+ else
27
+ folder_rules.process_file(entry)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def dir_entries(folder)
34
+ (Dir.entries(folder) - IGNORED_ENTRIES).map do |entry|
35
+ File.expand_path(entry, folder)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ require 'clean_up/conditions/extension'
2
+ require 'clean_up/conditions/contains'
3
+ require 'clean_up/conditions/pattern'
4
+ require 'clean_up/conditions/name'
5
+ require 'clean_up/conditions/size'
6
+
7
+ module CleanUp
8
+ module Conditions
9
+ # TODO: created_at condition
10
+ TYPES_CONDITIONS = {
11
+ file: %w(name extension pattern size),
12
+ directory: %w(contains name pattern)
13
+ }.freeze
14
+
15
+ class << self
16
+ def build_for_file(options)
17
+ build_by_type(:file, options)
18
+ end
19
+
20
+ def build_for_directory(options)
21
+ build_by_type(:directory, options)
22
+ end
23
+
24
+ private
25
+
26
+ def build_by_type(conditions_type, options)
27
+ options.keys.each_with_object([]) do |type, memo|
28
+ memo << build(type, *options.delete(type)) if TYPES_CONDITIONS[conditions_type].include?(type)
29
+ end
30
+ end
31
+
32
+ def build(type, *args)
33
+ case type
34
+ when 'pattern', 'size', 'name', 'extension'
35
+ Conditions.const_get(type.capitalize).new(*args)
36
+ when 'contains'
37
+ Conditions::Contains.new(&args.first)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,35 @@
1
+ module CleanUp
2
+ module Conditions
3
+ class Contains
4
+ def initialize(&block)
5
+ @file_conditions = []
6
+ instance_eval(&block)
7
+ end
8
+
9
+ def at_least(number, &block)
10
+ number.times { instance_eval(&block) }
11
+ end
12
+
13
+ def file(&block)
14
+ @file_conditions << Conditions.build_for_file(OptionValues.parse(&block))
15
+ end
16
+
17
+ def match?(folder)
18
+ files = CleanUp.dir_entries(folder).reject { |entry| File.directory?(entry) }
19
+
20
+ match_file_conditions?(files)
21
+ end
22
+
23
+ private
24
+
25
+ def match_file_conditions?(files)
26
+ @file_conditions.all? do |conditions|
27
+ file = files.detect do |f|
28
+ conditions.all? { |c| c.match?(f) }
29
+ end
30
+ file && files.delete(file)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ module CleanUp
2
+ module Conditions
3
+ class Extension
4
+ def initialize(*value)
5
+ @pattern = *value
6
+ end
7
+
8
+ def match?(file)
9
+ extension = File.extname(file)[1..-1]
10
+
11
+ extension && @pattern.include?(extension)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module CleanUp
2
+ module Conditions
3
+ class Name
4
+ def initialize(value)
5
+ @pattern = Array(value)
6
+ end
7
+
8
+ def match?(file)
9
+ @pattern.any? do |filename|
10
+ filename == File.basename(file, '.*')
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module CleanUp
2
+ module Conditions
3
+ class Pattern
4
+ def initialize(*value)
5
+ @pattern = value.map { |regexp| Regexp.new(regexp) }
6
+ end
7
+
8
+ def match?(file)
9
+ @pattern.any? { |regexp| regexp.match(File.basename(file, '.*')) }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module CleanUp
2
+ module Conditions
3
+ # TODO: size conditions format
4
+ class Size
5
+ def initialize(*value)
6
+ @pattern = parse(value.first)
7
+ end
8
+
9
+ def match?(file)
10
+ @pattern.cover?(File.size(file))
11
+ end
12
+
13
+ protected
14
+
15
+ def parse(value)
16
+ Range.new(value[:min] || 0, value[:max] || Float::INFINITY)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,66 @@
1
+ module CleanUp
2
+ class Folder
3
+ attr_reader :options, :strategy, :files_rules, :directory_rules
4
+
5
+ def self.collect(strategy, &block)
6
+ new(strategy, &block)
7
+ end
8
+
9
+ def initialize(strategy, &block)
10
+ @strategy, @files_rules, @directory_rules = strategy, [], []
11
+ instance_eval(&block)
12
+ end
13
+
14
+ # Implement setter & getter for source folder.
15
+ def source(folder = nil)
16
+ @source ||= File.expand_path(folder)
17
+ end
18
+
19
+ # Implement setter & getter for target folder.
20
+ def target(folder = nil)
21
+ @target ||= File.expand_path(folder)
22
+ end
23
+
24
+ def file(&block)
25
+ with_options(block) do
26
+ @files_rules << file_rule_class.new(options)
27
+ end
28
+ end
29
+
30
+ def directory(&block)
31
+ with_options(block) do
32
+ @directory_rules << directory_rule_class.new(options)
33
+ end
34
+ end
35
+
36
+ def process_directory(directory)
37
+ directory_rules.detect do |rule|
38
+ rule.call(directory, target)
39
+ end
40
+ end
41
+
42
+ def process_file(file)
43
+ files_rules.detect do |rule|
44
+ rule.call(file, target)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def file_rule_class
51
+ strategy == :move ? Rules::MoveFile : Rules::CopyFile
52
+ end
53
+
54
+ def directory_rule_class
55
+ strategy == :move ? Rules::MoveDirectory : Rules::CopyDirectory
56
+ end
57
+
58
+ def with_options(options_block)
59
+ @options = OptionValues.parse(&options_block)
60
+
61
+ yield if block_given?
62
+ ensure
63
+ @options = nil
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,22 @@
1
+ module CleanUp
2
+ class FoldersRules
3
+ attr_reader :rules_set
4
+
5
+ def self.collect(&block)
6
+ new(&block).rules_set
7
+ end
8
+
9
+ def initialize(&block)
10
+ @rules_set = []
11
+ instance_eval(&block)
12
+ end
13
+
14
+ def move(&block)
15
+ @rules_set << CleanUp::Folder.collect(:move, &block)
16
+ end
17
+
18
+ def copy(&block)
19
+ @rules_set << CleanUp::Folder.collect(:copy, &block)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ module CleanUp
2
+ class OptionValues
3
+ SUPPORTED_OPTIONS = %w(dir extension pattern files_amount size).freeze
4
+ SUPPORTED_BLOCK_OPTIONS = %w(contains).freeze
5
+
6
+ attr_reader :options
7
+
8
+ def self.parse(&block)
9
+ new(&block).options
10
+ end
11
+
12
+ def initialize(&block)
13
+ @options = {}
14
+ instance_eval(&block)
15
+ end
16
+
17
+ def respond_to_missing?(method, *)
18
+ SUPPORTED_OPTIONS.include?(method.to_s) || super
19
+ end
20
+
21
+ def method_missing(method, *args, &block)
22
+ if SUPPORTED_BLOCK_OPTIONS.include?(method.to_s)
23
+ @options[method.to_s] = block
24
+ elsif SUPPORTED_OPTIONS.include?(method.to_s)
25
+ @options[method.to_s] = args
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,10 @@
1
+ require 'clean_up/rules/base'
2
+ require 'clean_up/rules/copy_directory'
3
+ require 'clean_up/rules/copy_file'
4
+ require 'clean_up/rules/move_file'
5
+ require 'clean_up/rules/move_directory'
6
+
7
+ module CleanUp
8
+ module Rules
9
+ end
10
+ end
@@ -0,0 +1,30 @@
1
+ module CleanUp
2
+ module Rules
3
+ class Base
4
+ attr_reader :options
5
+
6
+ def initialize(options)
7
+ @conditions = build_conditions(options)
8
+ @options = options
9
+ end
10
+
11
+ def call(_entry, _source, _target)
12
+ raise NotImplementedError
13
+ end
14
+
15
+ private
16
+
17
+ def build_conditions(_options)
18
+ raise NotImplementedError
19
+ end
20
+
21
+ def match_conditions?(entry)
22
+ @conditions.all? { |c| c.match?(entry) }
23
+ end
24
+
25
+ def full_target_folder(target)
26
+ File.join(File.expand_path(target), options['dir'])
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ module CleanUp
2
+ module Rules
3
+ class CopyDirectory < Base
4
+ def build_conditions(options)
5
+ Conditions.build_for_directory(options)
6
+ end
7
+
8
+ def call(entry, target)
9
+ if match_conditions?(entry)
10
+ FileUtils.cp_r(entry, full_target_folder(target), verbose: true)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module CleanUp
2
+ module Rules
3
+ class CopyFile < Base
4
+ def build_conditions(options)
5
+ Conditions.build_for_file(options)
6
+ end
7
+
8
+ def call(entry, target)
9
+ if match_conditions?(entry)
10
+ FileUtils.cp(entry, full_target_folder(target), verbose: true)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module CleanUp
2
+ module Rules
3
+ class MoveDirectory < Base
4
+ def build_conditions(options)
5
+ Conditions.build_for_directory(options)
6
+ end
7
+
8
+ def call(entry, target)
9
+ if match_conditions?(entry)
10
+ FileUtils.mv(entry, full_target_folder(target), verbose: true)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module CleanUp
2
+ module Rules
3
+ class MoveFile < Base
4
+ def build_conditions(options)
5
+ Conditions.build_for_file(options)
6
+ end
7
+
8
+ def call(entry, target)
9
+ if match_conditions?(entry)
10
+ FileUtils.mv(entry, full_target_folder(target), verbose: true)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module CleanUp
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cleanup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nickolai Smirnov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.3
55
+ description:
56
+ email:
57
+ - sm-nickolai@gmail.com
58
+ executables:
59
+ - cleanup
60
+ - console
61
+ - setup
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - Gemfile
67
+ - LICENSE.md
68
+ - README.md
69
+ - Rakefile
70
+ - bin/cleanup
71
+ - bin/console
72
+ - bin/setup
73
+ - clean_up.gemspec
74
+ - lib/clean_up.rb
75
+ - lib/clean_up/conditions.rb
76
+ - lib/clean_up/conditions/contains.rb
77
+ - lib/clean_up/conditions/extension.rb
78
+ - lib/clean_up/conditions/name.rb
79
+ - lib/clean_up/conditions/pattern.rb
80
+ - lib/clean_up/conditions/size.rb
81
+ - lib/clean_up/folder.rb
82
+ - lib/clean_up/folders_rules.rb
83
+ - lib/clean_up/option_values.rb
84
+ - lib/clean_up/rules.rb
85
+ - lib/clean_up/rules/base.rb
86
+ - lib/clean_up/rules/copy_directory.rb
87
+ - lib/clean_up/rules/copy_file.rb
88
+ - lib/clean_up/rules/move_directory.rb
89
+ - lib/clean_up/rules/move_file.rb
90
+ - lib/clean_up/version.rb
91
+ homepage: https://github.com/nickolai-sm/clean_up
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Files and Directories clean up tool
115
+ test_files: []