kelredd-repository 0.2.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/README.rdoc ADDED
@@ -0,0 +1,53 @@
1
+ = Repository
2
+
3
+ == Description
4
+
5
+ Abstracts a file storage system.
6
+
7
+ === FileSystem
8
+ Including FileSystem class will mix in methods for accessing a file
9
+ system. All path designations are made with arrays, referenced from
10
+ a common root, where each array element corresponds to a path segment.
11
+ The class handles joining the segments with the appropriate seperator
12
+ for the file system being used. You should override the ROOT constant
13
+ appropriately, depending on where you want your repository to live.
14
+ Check out the code for more detail.
15
+
16
+ == Installation
17
+
18
+ sudo gem install kelredd-repository --source http://gems.github.com
19
+
20
+ == Usage
21
+
22
+ require 'rubygems'
23
+ require 'repository/file_system'
24
+ Repository::FileSystem::ROOT = "/some/path/to/a/repo"
25
+
26
+
27
+ (mixes in all kinds of helper methods for dealing with files and paths at above root)
28
+ include FileSystem
29
+
30
+ == License
31
+
32
+ Copyright (c) <year> <copyright holders>
33
+
34
+ Permission is hereby granted, free of charge, to any person
35
+ obtaining a copy of this software and associated documentation
36
+ files (the "Software"), to deal in the Software without
37
+ restriction, including without limitation the rights to use,
38
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
39
+ copies of the Software, and to permit persons to whom the
40
+ Software is furnished to do so, subject to the following
41
+ conditions:
42
+
43
+ The above copyright notice and this permission notice shall be
44
+ included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
48
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
50
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
51
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
52
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
53
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require 'lib/repository/version'
6
+
7
+ task :default => :test
8
+
9
+ spec = Gem::Specification.new do |s|
10
+ s.name = 'repository'
11
+ s.version = Repository::Version.to_s
12
+ s.has_rdoc = true
13
+ s.extra_rdoc_files = %w(README.rdoc)
14
+ s.rdoc_options = %w(--main README.rdoc)
15
+ s.summary = "Abstracts a file storage system."
16
+ s.author = 'Kelredd'
17
+ s.email = ''
18
+ s.homepage = ''
19
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
20
+ # s.executables = ['repository']
21
+
22
+ # s.add_dependency('gem_name', '~> 0.0.1')
23
+ end
24
+
25
+ Rake::GemPackageTask.new(spec) do |pkg|
26
+ pkg.gem_spec = spec
27
+ end
28
+
29
+ Rake::TestTask.new do |t|
30
+ t.libs << 'test'
31
+ t.test_files = FileList["test/**/*_test.rb"]
32
+ t.verbose = true
33
+ end
34
+
35
+ desc 'Generate the gemspec to serve this Gem from Github'
36
+ task :github do
37
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
38
+ File.open(file, 'w') {|f| f << spec.to_ruby }
39
+ puts "Created gemspec: #{file}"
40
+ end
@@ -0,0 +1,86 @@
1
+ module Repository
2
+ class FileSystem
3
+
4
+ # all path designations should be passed as arrays, where each array element corresponds to a path segment
5
+ # the class handles joining the segments with the appropriate seperator
6
+
7
+ # you should override ROOT appropriately, depending on where you want your repo located
8
+ ROOT ||= '/'
9
+ WRITE = "w"
10
+ READ = "r"
11
+
12
+ # Return the array of the repository root
13
+ def self.root_array
14
+ ROOT.split(File::SEPARATOR)
15
+ end
16
+
17
+ # Return the full file system path of the given directory or file
18
+ def self.sys_path_array(rep_path = [])
19
+ self.root_array+rep_path
20
+ end
21
+
22
+ # Return the full file system path of the given directory or file
23
+ def self.sys_path(rep_path = [])
24
+ sys_path_array(rep_path).join(File::SEPARATOR)
25
+ end
26
+
27
+ # Read the file data specified by the given path
28
+ def self.read(rep_path)
29
+ File.open(self.sys_path(rep_path), READ) { |file| yield file.read }
30
+ end
31
+
32
+ # Delete a file at the given path in the repository
33
+ def self.delete(rep_path)
34
+ File.delete(self.sys_path(rep_path)) if self.exists?(rep_path)
35
+ end
36
+
37
+ # Change the permission of a file
38
+ def self.chmod(rep_path, permission)
39
+ File.chmod(permission, self.sys_path(rep_path))
40
+ end
41
+
42
+ # Return whether a path exists or not
43
+ def self.exists?(rep_path)
44
+ File.exists?(self.sys_path(rep_path))
45
+ end
46
+
47
+ # Writes file data to the given path
48
+ def self.write(data, file_rep_path)
49
+ File.open(self.sys_path(file_rep_path), WRITE) { |file| file.write(data) }
50
+ end
51
+
52
+ # Create the specified directory and any parent dirs in the root
53
+ def self.mkdir(rep_dir, permission=0777)
54
+ FileUtils.makedirs(self.sys_path(rep_dir))
55
+ FileUtils.chmod(permission, self.sys_path(rep_dir))
56
+ rep_dir
57
+ end
58
+
59
+ # Copy the file from param1 to location specified in param2
60
+ def self.cp(from_rep_path, to_rep_path)
61
+ FileUtils.cp(self.sys_path(from_rep_path), self.sys_path(to_rep_path))
62
+ end
63
+
64
+ # Same as cp except recrusive
65
+ def self.cpdir(from_rep_path, to_rep_path)
66
+ FileUtils.cp_r(self.sys_path(from_rep_path), self.sys_path(to_rep_path))
67
+ end
68
+
69
+ # Delete the specified directory and contents in the root
70
+ def self.rmdir(rep_dir)
71
+ sys_path = self.sys_path(rep_dir)
72
+ if File.exists?(sys_path)
73
+ Dir.foreach(sys_path) {|file_name| File.delete(self.sys_path(rep_dir+[file_name])) if !File.directory?(self.sys_path(rep_dir+[file_name])) }
74
+ Dir.delete(sys_path)
75
+ end
76
+ rep_dir
77
+ end
78
+
79
+ # Return the filtered contents of a directory in an array
80
+ def self.directory_contents(rep_path, filter = '*')
81
+ Dir[self.sys_path(rep_path+[filter])]
82
+ end
83
+
84
+ end
85
+
86
+ end
@@ -0,0 +1,24 @@
1
+ module Repository
2
+ class FileSystem
3
+ module Helpers
4
+ FILE_SIZE_ABBREVS = ['Bytes', 'KB', 'MB', 'GB']
5
+
6
+ def abbrev_file_size(size_in_bytes)
7
+ value = size_in_bytes
8
+ FILE_SIZE_ABBREVS.each do |abbrev|
9
+ return value.to_s + ' ' + abbrev if value < 1000 || abbrev == FILE_SIZE_ABBREVS.last
10
+ value /= 1000
11
+ end
12
+ end
13
+
14
+ def full_file_name=(full_name)
15
+ self.extension = File.extname(full_name).downcase.chomp if new_record?
16
+ self.file_name = clean_file_name(full_name)
17
+ end
18
+
19
+ def clean_file_name(file_name)
20
+ File.basename(file_name,'.*').gsub(/\s+/,'_').gsub(/_+/,'_').gsub(/[^\w._-]/,'').chomp
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,2 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require 'file_system/base'
@@ -0,0 +1,13 @@
1
+ module Repository
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 2
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
data/lib/repository.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ p "Don't require in this file, require in the type of repository class you want to use, ie require 'repository/file_system'"
@@ -0,0 +1,10 @@
1
+ # http://sneaq.net/textmate-wtf
2
+ $:.reject! { |e| e.include? 'TextMate' }
3
+
4
+ require 'rubygems'
5
+ require 'test/unit'
6
+ require 'matchy'
7
+ require 'context'
8
+ require 'mocha'
9
+
10
+ require File.dirname(__FILE__) + '/../lib/repository'
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class RepositoryTest < Test::Unit::TestCase
4
+
5
+ describe "An instance of the Repository class" do
6
+
7
+ it "should flunk" do
8
+ flunk "Please provide some tests"
9
+ end
10
+
11
+ end
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kelredd-repository
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Kelredd
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-22 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: ""
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - README.rdoc
26
+ - Rakefile
27
+ - lib/repository
28
+ - lib/repository/file_system
29
+ - lib/repository/file_system/base.rb
30
+ - lib/repository/file_system/helpers.rb
31
+ - lib/repository/file_system.rb
32
+ - lib/repository/version.rb
33
+ - lib/repository.rb
34
+ - test/test_helper.rb
35
+ - test/unit
36
+ - test/unit/repository_test.rb
37
+ has_rdoc: true
38
+ homepage: ""
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README.rdoc
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Abstracts a file storage system.
64
+ test_files: []
65
+