abstract_class 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +65 -0
- data/Rakefile +22 -0
- data/lib/abstract_class.rb +38 -0
- data/lib/abstract_class/test_helper.rb +14 -0
- data/lib/abstract_class/version.rb +17 -0
- data/test/abstract_class_test.rb +49 -0
- data/test/test_helper.rb +7 -0
- metadata +78 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Sean Huber - shuber@huberry.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
= abstract_class
|
2
|
+
|
3
|
+
Abstract classes in ruby
|
4
|
+
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
gem install abstract_class
|
9
|
+
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
class ActiveRecord::Base
|
14
|
+
abstract
|
15
|
+
end
|
16
|
+
|
17
|
+
class User < ActiveRecord::Base
|
18
|
+
end
|
19
|
+
|
20
|
+
# RuntimeError: abstract class ActiveRecord::Base can't be instantiated/allocated
|
21
|
+
ActiveRecord::Base.new
|
22
|
+
ActiveRecord::Base.allocate
|
23
|
+
|
24
|
+
# instantiates/allocates like normal
|
25
|
+
User.new
|
26
|
+
User.allocate
|
27
|
+
|
28
|
+
# returns true or false
|
29
|
+
ActiveRecord::Base.abstract?
|
30
|
+
User.abstract?
|
31
|
+
|
32
|
+
|
33
|
+
== Testing abstract classes
|
34
|
+
|
35
|
+
Include <tt>AbstractClass::TestHelper</tt> in your test framework
|
36
|
+
|
37
|
+
Test::Unit::TestCase.send(:include, AbstractClass::TestHelper)
|
38
|
+
|
39
|
+
Then you can use <tt>assert_abstract_class</tt> or <tt>assert_not_abstract_class</tt>) in your tests
|
40
|
+
|
41
|
+
class AbstractClass
|
42
|
+
abstract
|
43
|
+
end
|
44
|
+
|
45
|
+
class NormalClass < AbstractClass
|
46
|
+
end
|
47
|
+
|
48
|
+
class AbstractClassTest < Test::Unit::TestCase
|
49
|
+
def test_should_be_abstract_class
|
50
|
+
assert_abstract_class AbstractClass
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_should_not_be_abstract_class
|
54
|
+
assert_not_abstract_class NormalClass
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
== Patches and pull requests
|
60
|
+
|
61
|
+
* Fork the project.
|
62
|
+
* Make your feature addition or bug fix.
|
63
|
+
* Add tests for it. This is important so I don't break it in a future version unintentionally.
|
64
|
+
* Commit, do not mess with Rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
65
|
+
* Send me a pull request. Bonus points for topic branches.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the abstract_class gem.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs += ['lib', 'test']
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the abstract_class gem.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'abstract_class'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README*')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# Adds the ability to declare classes as abstract so that they can't be instantiated or allocated
|
2
|
+
module AbstractClass
|
3
|
+
autoload :TestHelper, 'abstract_class/test_helper'
|
4
|
+
autoload :Version, 'abstract_class/version'
|
5
|
+
|
6
|
+
# When included, it will <tt>alias_method_chain</tt> <tt>allocate</tt> and <tt>new</tt> methods with <tt>:abstract_class</tt> in <tt>klass</tt>
|
7
|
+
def self.included(klass)
|
8
|
+
klass.class_eval do
|
9
|
+
alias_method :allocate_without_abstract_class, :allocate
|
10
|
+
alias_method :allocate, :allocate_with_abstract_class
|
11
|
+
|
12
|
+
alias_method :new_without_abstract_class, :new
|
13
|
+
alias_method :new, :new_with_abstract_class
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Declares this class as abstract and prevents it from being instantiated or allocated
|
18
|
+
def abstract
|
19
|
+
@abstract = true
|
20
|
+
end
|
21
|
+
|
22
|
+
# Checks if this class has been declared as abstract
|
23
|
+
def abstract?
|
24
|
+
@abstract ||= false
|
25
|
+
end
|
26
|
+
|
27
|
+
# Raises RuntimeError if class is abstract, otherwise returns <tt>allocate_without_abstract_class</tt>
|
28
|
+
def allocate_with_abstract_class
|
29
|
+
abstract? ? raise("abstract class #{self} can't be allocated") : allocate_without_abstract_class
|
30
|
+
end
|
31
|
+
|
32
|
+
# Raises RuntimeError if class is abstract, otherwise returns <tt>new_without_abstract_class</tt>
|
33
|
+
def new_with_abstract_class(*args, &block)
|
34
|
+
abstract? ? raise("abstract class #{self} can't be instantiated") : new_without_abstract_class(*args, &block)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
Class.send(:include, AbstractClass)
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module AbstractClass
|
2
|
+
# Contains abstract class testing assertions to include in your test framework
|
3
|
+
module TestHelper
|
4
|
+
# Asserts that the specified <tt>klass</tt> has been declared as abstract
|
5
|
+
def assert_abstract_class(klass)
|
6
|
+
assert klass.abstract?, "#{klass} was expected to be abstract"
|
7
|
+
end
|
8
|
+
|
9
|
+
# Asserts that the specified <tt>klass</tt> has not been declared as abstract
|
10
|
+
def assert_not_abstract_class(klass)
|
11
|
+
assert !klass.abstract?, "#{klass} was not expected to be abstract"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module AbstractClass
|
2
|
+
# Contains information about this gem's version
|
3
|
+
module Version
|
4
|
+
MAJOR = 0
|
5
|
+
MINOR = 0
|
6
|
+
PATCH = 1
|
7
|
+
|
8
|
+
# Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
|
9
|
+
#
|
10
|
+
# Example
|
11
|
+
#
|
12
|
+
# Version.string # '1.0.2'
|
13
|
+
def self.string
|
14
|
+
[MAJOR, MINOR, PATCH].join('.')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class BaseClass
|
4
|
+
abstract
|
5
|
+
end
|
6
|
+
|
7
|
+
class DerivedClass
|
8
|
+
end
|
9
|
+
|
10
|
+
class AbstractClassTest < Test::Unit::TestCase
|
11
|
+
def test_should_not_allow_abstract_class_to_be_initialized
|
12
|
+
assert_raises(RuntimeError) { BaseClass.new }
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_not_allow_abstract_class_to_be_allocated
|
16
|
+
assert_raises(RuntimeError) { BaseClass.allocate }
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_allow_derived_class_to_be_initialized
|
20
|
+
assert DerivedClass.new.is_a?(DerivedClass)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_should_allow_derived_class_to_be_allocated
|
24
|
+
assert DerivedClass.allocate.is_a?(DerivedClass)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_check_if_a_class_is_abstract
|
28
|
+
assert BaseClass.abstract?
|
29
|
+
assert !DerivedClass.abstract?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_bypass_instantiation_restrictions
|
33
|
+
assert DerivedClass.new_without_abstract_class.is_a?(DerivedClass)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_should_bypass_allocation_restrictions
|
37
|
+
assert DerivedClass.allocate_without_abstract_class.is_a?(DerivedClass)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_assert_class_is_abstract
|
41
|
+
assert_abstract_class BaseClass
|
42
|
+
assert_raises(Test::Unit::AssertionFailedError) { assert_abstract_class DerivedClass }
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_assert_class_is_not_abstract
|
46
|
+
assert_not_abstract_class DerivedClass
|
47
|
+
assert_raises(Test::Unit::AssertionFailedError) { assert_not_abstract_class BaseClass }
|
48
|
+
end
|
49
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abstract_class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Sean Huber
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-10 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Abstract classes in ruby
|
23
|
+
email: shuber@huberry.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/abstract_class.rb
|
32
|
+
- lib/abstract_class/test_helper.rb
|
33
|
+
- lib/abstract_class/version.rb
|
34
|
+
- MIT-LICENSE
|
35
|
+
- Rakefile
|
36
|
+
- README.rdoc
|
37
|
+
- test/abstract_class_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/shuber/abstract_class
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --line-numbers
|
46
|
+
- --inline-source
|
47
|
+
- --main
|
48
|
+
- README.rdoc
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.5.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Abstract classes in ruby
|
76
|
+
test_files:
|
77
|
+
- test/abstract_class_test.rb
|
78
|
+
- test/test_helper.rb
|