file_distribution 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.
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2013 Adam Kubica <caffecoder@kaizen-step.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,56 @@
1
+ #
2
+ # @author: Adam Kubica (caffecoder) <caffecoder@kaizen-step.com>
3
+ #
4
+
5
+ require 'ftools'
6
+
7
+ #
8
+ # Class for manage hashed file distribution.
9
+ #
10
+ class FileDistribution
11
+
12
+ # Creates new instance with directory prefix.
13
+ #
14
+ # Params:
15
+ # - prefix: directory prefix.
16
+ def initialize(prefix)
17
+ @ext = '.dat'
18
+ @prefix = File.expand_path(prefix)
19
+ @path = @prefix
20
+ end
21
+
22
+ # Params:
23
+ # - ext: file extension.
24
+ def set_extension(ext)
25
+ if ext.length > 0 && ext.chars.first != '.'
26
+ @ext = '.' + ext
27
+ else
28
+ @ext = ext
29
+ end
30
+ end
31
+
32
+ # Returns Destination path.
33
+ def get_path
34
+ @path
35
+ end
36
+
37
+ # Params:
38
+ # - id: database file ID etc.
39
+ def hex_path(id)
40
+ hex = "%x" % id
41
+ hex = '0%s' % hex if hex.length % 2 != 0
42
+ @path = File.join(@prefix,hex.scan(/../))
43
+ @path += @ext
44
+ end
45
+
46
+ # Params:
47
+ # - path: source file path.
48
+ #
49
+ # Raises a SystemCallError if the file cannot be renamed.
50
+ def rename_from(path)
51
+ dst_dir = File.dirname(@path)
52
+ File.mkpath(dst_dir) unless File.exists?(dst_dir)
53
+
54
+ File.rename(path,@path)
55
+ end
56
+ end
@@ -0,0 +1,87 @@
1
+ #
2
+ # @author: Adam Kubica (caffecoder) <caffecoder@kaizen-step.com>
3
+ #
4
+
5
+ require 'file_distribution'
6
+ require 'fileutils'
7
+ require 'test/unit'
8
+
9
+ class TestFileDistribution < Test::Unit::TestCase
10
+
11
+ def setup
12
+ if not File.exists?("/tmp/storage")
13
+ FileUtils.mkdir("/tmp/storage")
14
+ end
15
+
16
+ @fd = FileDistribution.new("/tmp/storage//")
17
+ end
18
+
19
+ def test_path
20
+ assert_equal("/tmp/storage", @fd.get_path())
21
+ end
22
+
23
+ def test_case1
24
+ @fd.set_extension("tmp")
25
+ @fd.set_extension(".dat")
26
+ @fd.hex_path(102423)
27
+ assert_equal("/tmp/storage/01/90/17.dat", @fd.get_path())
28
+ end
29
+
30
+ def test_case2
31
+ @fd.set_extension("dat")
32
+ @fd.hex_path(256)
33
+ assert_equal("/tmp/storage/01/00.dat", @fd.get_path())
34
+ end
35
+
36
+ # in most cases this is wrong way
37
+ def test_case3
38
+ @fd.set_extension("")
39
+ @fd.hex_path(256)
40
+ assert_equal("/tmp/storage/01/00", @fd.get_path())
41
+ end
42
+
43
+ def test_case4
44
+ @fd.hex_path(1)
45
+ assert_equal("/tmp/storage/01.dat", @fd.get_path())
46
+ end
47
+
48
+ def test_case5
49
+ f = File.open('/tmp/storage/test.txt', 'w')
50
+ f.close()
51
+
52
+ assert_equal(true, File.exists?("/tmp/storage/test.txt"))
53
+
54
+ @fd.set_extension(".dat")
55
+ @fd.hex_path(256)
56
+
57
+ @fd.rename_from("/tmp/storage/test.txt")
58
+ assert_equal(true, File.exists?("/tmp/storage/01/00.dat"))
59
+ end
60
+
61
+ def test_case6
62
+ f = File.open('/tmp/storage/test1.txt', 'w')
63
+ f.close()
64
+
65
+ f = File.open('/tmp/storage/test2.txt', 'w')
66
+ f.close()
67
+
68
+ assert_equal(true, File.exists?("/tmp/storage/test1.txt"))
69
+ assert_equal(true, File.exists?("/tmp/storage/test2.txt"))
70
+
71
+ @fd.set_extension(".dat")
72
+
73
+ @fd.hex_path(1)
74
+ @fd.rename_from("/tmp/storage/test1.txt")
75
+ assert_equal(true, File.exists?("/tmp/storage/01.dat"))
76
+
77
+ @fd.hex_path(256)
78
+ @fd.rename_from("/tmp/storage/test2.txt")
79
+ assert_equal(true, File.exists?("/tmp/storage/01/00.dat"))
80
+ end
81
+
82
+ def teardown
83
+ if File.exists?("/tmp/storage")
84
+ FileUtils.rm_r("/tmp/storage")
85
+ end
86
+ end
87
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_distribution
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Adam Kubica
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-04-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rake
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 79
29
+ segments:
30
+ - 10
31
+ - 0
32
+ - 0
33
+ version: 10.0.0
34
+ type: :development
35
+ version_requirements: *id001
36
+ description: Simple library that allows organize distribution of files within hex based tree.
37
+ email: caffecoder@kaizen-step.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - Rakefile
46
+ - LICENSE
47
+ - lib/file_distribution.rb
48
+ - test/test_file_distribution.rb
49
+ homepage: http://github.org/caffecoder/fdist-ruby
50
+ licenses:
51
+ - MIT
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 57
63
+ segments:
64
+ - 1
65
+ - 8
66
+ - 7
67
+ version: 1.8.7
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.8.25
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: Simple file distribution library.
84
+ test_files:
85
+ - test/test_file_distribution.rb