oofile 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == 0.0.1
2
+
data/COPYING ADDED
@@ -0,0 +1,32 @@
1
+ SiteBuilder Licence
2
+
3
+ COPYRIGHT AND PERMISSION NOTICE
4
+
5
+ Copyright (c) 2011 Green Bar Software Limited, UK
6
+
7
+ All rights reserved.
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a
10
+ copy of this software and associated documentation files (the
11
+ "Software"), to deal in the Software without restriction, including
12
+ without limitation the rights to use, copy, modify, merge, publish,
13
+ distribute, and/or sell copies of the Software, and to permit persons
14
+ to whom the Software is furnished to do so, provided that the above
15
+ copyright notice(s) and this permission notice appear in all copies of
16
+ the Software and that both the above copyright notice(s) and this
17
+ permission notice appear in supporting documentation.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20
+ OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
22
+ OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23
+ HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
24
+ INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
25
+ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
26
+ NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
27
+ WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28
+
29
+ Except as contained in this notice, the name of a copyright holder
30
+ shall not be used in advertising or otherwise to promote the sale, use
31
+ or other dealings in this Software without prior written authorization
32
+ of the copyright holder.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ == OOFile
2
+ oofile is an object-oriented way of accessing the filesystem.
3
+
4
+ === How to use it
5
+ * Install with:
6
+ gem install oofile
7
+ * Basic operation can be seen in demo.rdoc
8
+ * *source* http://github.com/dafydd/sitebuilder
9
+ * To build me, set an environment variable called *SANDBOX* to the directory above your sitebuilder directory. The tests need this pathname to access test data.
10
+
11
+ === Compatibility
12
+ This project is being developed on OS X. Automated testing for Linux will be included in future releases.
13
+
14
+ === Licence
15
+ This is open source software and comes with no warranty. See COPYING for details.
16
+
17
+ http://www.greenbarsoft.co.uk
18
+
19
+ Copyright 2009-2011 Green Bar Software Limited, UK
data/TODO.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == todo
2
+
data/doc/demo.rdoc ADDED
@@ -0,0 +1,70 @@
1
+ == Sample output
2
+
3
+
4
+ >> require 'oofile'
5
+ => true
6
+ >>
7
+ ?> dir_string = File.join(ENV['SANDBOX'],'oofile','test','data')
8
+ => "/Users/dev/oofile/test/data"
9
+ >> file_string = File.join(dir_string,'testfile.txt')
10
+ => "/Users/dev/oofile/test/data/testfile.txt"
11
+ >>
12
+ ?> file_string
13
+ => "/Users/dev/oofile/test/data/testfile.txt"
14
+ >>
15
+ ?> dir_string
16
+ => "/Users/dev/oofile/test/data"
17
+ >>
18
+ ?> f = OOFile::FsEntry.fs_entry_from(file_string)
19
+ => #<OOFile::FileEntry:0x1016a34f0 @path="/Users/dev/oofile/test/data/testfile.txt">
20
+ >>
21
+ ?> f.extname
22
+ => ".txt"
23
+ >>
24
+ ?> f.extnless
25
+ => "testfile"
26
+ >>
27
+ ?> f.basename
28
+ => "testfile.txt"
29
+ >>
30
+ ?> f.dirname
31
+ => "/Users/dev/oofile/test/data"
32
+ >>
33
+ ?> f.ctime
34
+ => Sat Feb 26 01:34:57 +0000 2011
35
+ >>
36
+ ?> f.size
37
+ => 28
38
+ >>
39
+ ?> # equality comparison
40
+ ?> f == OOFile::FsEntry.fs_entry_from(file_string)
41
+ => true
42
+ >>
43
+ ?> # directories
44
+ ?>
45
+ ?> d= OOFile::FsEntry.fs_entry_from(dir_string)
46
+ => #<OOFile::DirEntry:0x10168a5b8 @path="/Users/dev/oofile/test/data">
47
+ >>
48
+ ?> d.extname
49
+ => ""
50
+ >>
51
+ ?> d.extnless
52
+ => "data"
53
+ >>
54
+ ?> d.basename
55
+ => "data"
56
+ >>
57
+ ?> d.dirname
58
+ => "/Users/dev/oofile/test"
59
+ >>
60
+ ?> d.ctime
61
+ => Sat Feb 26 01:34:57 +0000 2011
62
+ >>
63
+ ?> d.size
64
+ => 102
65
+ >>
66
+ ?>
67
+ ?> >> => nil
68
+
69
+
70
+ oofile 0.0.1
data/doc/demo.script ADDED
@@ -0,0 +1,43 @@
1
+ require 'oofile'
2
+
3
+ dir_string = File.join(ENV['SANDBOX'],'oofile','test','data')
4
+ file_string = File.join(dir_string,'testfile.txt')
5
+
6
+ file_string
7
+
8
+ dir_string
9
+
10
+ f = OOFile::FsEntry.fs_entry_from(file_string)
11
+
12
+ f.extname
13
+
14
+ f.extnless
15
+
16
+ f.basename
17
+
18
+ f.dirname
19
+
20
+ f.ctime
21
+
22
+ f.size
23
+
24
+ # equality comparison
25
+ f == OOFile::FsEntry.fs_entry_from(file_string)
26
+
27
+ # directories
28
+
29
+ d= OOFile::FsEntry.fs_entry_from(dir_string)
30
+
31
+ d.extname
32
+
33
+ d.extnless
34
+
35
+ d.basename
36
+
37
+ d.dirname
38
+
39
+ d.ctime
40
+
41
+ d.size
42
+
43
+
@@ -0,0 +1,95 @@
1
+ #require 'FileUtils'
2
+ module OOFile
3
+
4
+ # Abstract class which is the abstract base class for file system instances
5
+ class FsEntry
6
+ # This is the canonical path to the file system entry
7
+ attr_reader :path
8
+
9
+ def initialize(path)
10
+ @path = File.expand_path(path)
11
+ end
12
+
13
+ def ==(other)
14
+ self.path==(other.path)
15
+ end
16
+
17
+ def extname
18
+ File::extname(@path)
19
+ end
20
+
21
+ # the bare filename without the file extension
22
+ def extnless
23
+ b = basename
24
+ b[0,b.size-extname.size]
25
+ end
26
+
27
+ def basename
28
+ File::basename(@path)
29
+ end
30
+
31
+ def dirname
32
+ File::dirname(@path)
33
+ end
34
+
35
+ def ctime
36
+ File::ctime(@path)
37
+ end
38
+
39
+ def size
40
+ File.size(@path)
41
+ end
42
+
43
+ # creates a file system entry for a fully qualified pathname
44
+ def self.fs_entry_from(f)
45
+ return FileEntry.new(f) if File.file?(f)
46
+ return DirEntry.new(f) if File.directory?(f)
47
+ UnknownEntry.new(f)
48
+ end
49
+
50
+ # creates a file system entry for a filename in the current file system entry object
51
+ def fs_entry_from(fs_entry)
52
+ FsEntry.fs_entry_from(File.join(@path, fs_entry))
53
+ end
54
+
55
+ end
56
+
57
+ # I represent a file in the filesystem
58
+ class FileEntry < FsEntry
59
+
60
+ # I am visitable with a traverser.
61
+ def traverse(traverser)
62
+ traverser.traverse_file(self)
63
+ end
64
+ end
65
+
66
+ # I represent filesystem entries that have been detected but aren't supported.
67
+ class UnknownEntry < FsEntry
68
+ end
69
+
70
+ # I represent directories in the filesystem.
71
+ class DirEntry < FsEntry
72
+
73
+ # I am visitable with a Traverser. I perform inorder traversal of FsEntries.
74
+ def traverse(traverser)
75
+ traverser.traverse_dir(self)
76
+ Dir.new(@path).each do |stringpath|
77
+ subentry = fs_entry_from(stringpath)
78
+ subentry.traverse(traverser) unless stringpath=='.' || stringpath=='..'
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ # Visitor for traversing a filesystem
85
+ class Traverser
86
+ # callback when traversing a DirEntry object
87
+ def traverse_dir(dir_entry)
88
+ end
89
+
90
+ # callback when traversing a FileEntry object
91
+ def traverse_file(file_entry)
92
+ end
93
+ end
94
+
95
+ end
data/lib/oofile.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'oofile/filesystem'
2
+
@@ -0,0 +1 @@
1
+ My ctime is used for tests.
@@ -0,0 +1,106 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+ require 'oofile/filesystem'
5
+
6
+
7
+ FS = File::SEPARATOR
8
+ TEST_DIRNAME = 'data'
9
+ TESTDATA_DIR = ENV['SANDBOX']+FS+'sitebuilder'+FS+'test'+FS+TEST_DIRNAME
10
+ TEST_FILENAME = 'testfile.txt'
11
+ TEST_FILEPATH = TESTDATA_DIR+FS+TEST_FILENAME
12
+
13
+ class FsEntryTest < Test::Unit::TestCase
14
+
15
+ def test_instances_and_equality
16
+ a_file = OOFile::FsEntry.new('/this/that/foo.txt')
17
+ another_file = OOFile::FsEntry.new('/this/that/foo.txt')
18
+ different_file=OOFile::FsEntry.new('/this/that/other.txt')
19
+
20
+ assert_equal(another_file, a_file)
21
+ assert_equal(a_file, another_file)
22
+ assert_not_equal(a_file, different_file)
23
+ assert_not_equal(another_file, different_file)
24
+ end
25
+
26
+ def test_extname
27
+ assert_equal('.txt', OOFile::FsEntry.new('/this/that/foo.txt').extname)
28
+ assert_equal('', OOFile::FsEntry.new('/this/that/foo.').extname)
29
+ assert_equal('', OOFile::FsEntry.new('/this/that/foo').extname)
30
+ assert_equal('', OOFile::FsEntry.new('/this/that/.foo').extname)
31
+ end
32
+
33
+ def test_extnless
34
+ assert_equal('foo', OOFile::FsEntry.new('/this/that/foo.txt').extnless)
35
+ assert_equal('foo.',OOFile::FsEntry.new('/this/that/foo.').extnless)
36
+ assert_equal('foo', OOFile::FsEntry.new('/this/that/foo').extnless)
37
+ assert_equal('.foo',OOFile::FsEntry.new('/this/that/.foo').extnless)
38
+ end
39
+
40
+ def test_basename
41
+ assert_equal('foo.txt', OOFile::FsEntry.new('/this/that/foo.txt').basename)
42
+ assert_equal('foo.txt', OOFile::FsEntry.new('/foo.txt').basename)
43
+ end
44
+
45
+ def test_dirname
46
+ assert_equal('/this/that', OOFile::FsEntry.new('/this/that/foo.txt').dirname)
47
+ assert_equal('/this/that', OOFile::FsEntry.new('/this/that/foo/').dirname)
48
+ end
49
+
50
+ def test_ctime
51
+ assert_equal(File::ctime(TEST_FILEPATH), OOFile::FsEntry.new(TEST_FILEPATH).ctime)
52
+ assert OOFile::FsEntry.new(TEST_FILEPATH).ctime.to_i > 0
53
+ end
54
+
55
+ def test_size
56
+ assert_equal(28, OOFile::FsEntry.new(TEST_FILEPATH).size)
57
+ end
58
+
59
+ def test_fs_entry_from
60
+ assert_equal OOFile::FileEntry, OOFile::FsEntry.fs_entry_from(TEST_FILEPATH).class
61
+ assert_equal OOFile::DirEntry, OOFile::FsEntry.fs_entry_from(TESTDATA_DIR).class
62
+ assert_equal OOFile::UnknownEntry, OOFile::FsEntry.fs_entry_from('/dev/null').class
63
+ end
64
+
65
+ def test_instance_fs_entry_from
66
+ instance = OOFile::FsEntry.fs_entry_from(TESTDATA_DIR)
67
+ file_result = instance.fs_entry_from(TEST_FILENAME)
68
+ dir_result = instance.fs_entry_from('.')
69
+
70
+ assert_equal instance.path+FS+TEST_FILENAME, file_result.path
71
+ assert_equal instance.path, dir_result.path
72
+ end
73
+
74
+ end
75
+
76
+ class FileEntryTest < Test::Unit::TestCase
77
+
78
+ def test_traverse
79
+ traverser = OOFile::Traverser.new
80
+ traverser.expects(:traverse_file).once().with do |file|
81
+ OOFile::FileEntry==file.class && 'testfile.txt'==file.basename
82
+ end
83
+ traverser.expects(:traverse_dir).never()
84
+
85
+ file = OOFile::FsEntry.fs_entry_from(TEST_FILEPATH)
86
+ file.traverse(traverser)
87
+ end
88
+
89
+ end
90
+
91
+ class DirEntryTest < Test::Unit::TestCase
92
+
93
+ def test_traverse
94
+ traverser = OOFile::Traverser.new
95
+ traverser.expects(:traverse_file).once().with do |file|
96
+ OOFile::FileEntry==file.class && 'testfile.txt'==file.basename
97
+ end
98
+ traverser.expects(:traverse_dir).once().with do |dir|
99
+ OOFile::DirEntry==dir.class && TESTDATA_DIR==dir.path
100
+ end
101
+
102
+ file = OOFile::FsEntry.fs_entry_from(TESTDATA_DIR)
103
+ file.traverse(traverser)
104
+ end
105
+
106
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oofile
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Dafydd Rees
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-26 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: mocha
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 43
30
+ segments:
31
+ - 0
32
+ - 9
33
+ - 8
34
+ version: 0.9.8
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rcov
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 55
46
+ segments:
47
+ - 0
48
+ - 9
49
+ - 6
50
+ version: 0.9.6
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Object-oriented access to the filesystem.
54
+ email: os@greenbarsoft.co.uk
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - CHANGELOG.rdoc
61
+ - COPYING
62
+ - README.rdoc
63
+ - doc/demo.rdoc
64
+ - TODO.rdoc
65
+ files:
66
+ - doc/demo.script
67
+ - lib/oofile/filesystem.rb
68
+ - lib/oofile.rb
69
+ - test/data/testfile.txt
70
+ - test/filesystem_test.rb
71
+ - ./test/filesystem_test.rb
72
+ - CHANGELOG.rdoc
73
+ - COPYING
74
+ - README.rdoc
75
+ - doc/demo.rdoc
76
+ - TODO.rdoc
77
+ has_rdoc: true
78
+ homepage: http://www.greenbarsoft.co.uk/software/oofile
79
+ licenses: []
80
+
81
+ post_install_message:
82
+ rdoc_options: []
83
+
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.7
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Static site generator
111
+ test_files:
112
+ - ./test/filesystem_test.rb