classy 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +20 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/classy/aliasable.rb +50 -0
- data/lib/classy/subclass_aware.rb +32 -0
- data/lib/classy.rb +3 -0
- data/spec/aliasable_spec.rb +81 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/subclass_aware_spec.rb +55 -0
- metadata +79 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 John Hyland
|
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,20 @@
|
|
1
|
+
= classy
|
2
|
+
|
3
|
+
Classy is a collection of meta-programming heavy modules which you can extend
|
4
|
+
in order to give various capabilities to your Ruby classes. For example,
|
5
|
+
SubclassAware lets a class know about all of its subclasses (and
|
6
|
+
sub-subclasses, etc), and Aliasable lets you refer to classes via symbols
|
7
|
+
(useful for creating friendly DSLs).
|
8
|
+
|
9
|
+
== Note on Patches/Pull Requests
|
10
|
+
|
11
|
+
* Fork the project.
|
12
|
+
* Make your feature addition or bug fix.
|
13
|
+
* Add tests for it. This is important so I don't break it in a
|
14
|
+
future version unintentionally.
|
15
|
+
* 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)
|
16
|
+
* Send me a pull request. Bonus points for topic branches.
|
17
|
+
|
18
|
+
== Copyright
|
19
|
+
|
20
|
+
Copyright (c) 2009 John Hyland. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "classy"
|
8
|
+
gem.summary = %Q{A collection of modules to enhance the capabilities of Ruby classes in various ways.}
|
9
|
+
gem.description = %Q{Classy is a collection of meta-programming heavy modules which you can extend in order to give various capabilities to your Ruby classes. For example, SubclassAware lets a class know about all of its subclasses (and sub-subclasses, etc), and Aliasable lets you refer to classes via symbols (useful for creating friendly DSLs).}
|
10
|
+
gem.email = "github@djspinmonkey.com"
|
11
|
+
gem.homepage = "http://github.com/djspinmonkey/classy"
|
12
|
+
gem.authors = ["John Hyland"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "classy #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Aliasable
|
2
|
+
def self.extended (klass) #nodoc;
|
3
|
+
klass.class_exec do
|
4
|
+
class_variable_set(:@@aliases, {})
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
##
|
9
|
+
# When passed a class, just returns it. When passed a symbol that is an
|
10
|
+
# alias for a class, returns that class.
|
11
|
+
#
|
12
|
+
# Testify::Framework::Base.find(:rspec) # => Testify::Framework::RSpec
|
13
|
+
#
|
14
|
+
def find (klass)
|
15
|
+
return klass if klass.kind_of? Class
|
16
|
+
class_variable_get(:@@aliases)[klass] or raise ArgumentError, "Could not find alias #{klass}"
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Forget all known aliases.
|
21
|
+
#
|
22
|
+
def forget_aliases
|
23
|
+
class_variable_get(:@@aliases).clear
|
24
|
+
end
|
25
|
+
|
26
|
+
##
|
27
|
+
# Specifies a symbol (or several) that a given framework might be known
|
28
|
+
# by. For example, if you wanted to refer to RSpec by :rspec or :spec,
|
29
|
+
# you might do this:
|
30
|
+
#
|
31
|
+
# class RSpec
|
32
|
+
# aka :rspec, spec
|
33
|
+
# ...
|
34
|
+
# end
|
35
|
+
#
|
36
|
+
def aka (*names)
|
37
|
+
names.each do |name|
|
38
|
+
raise ArgumentError, "Called aka with an alias that is already taken." if class_variable_get(:@@aliases).include? name
|
39
|
+
class_variable_get(:@@aliases)[name] = self
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Return a hash of known aliases to Class objects
|
45
|
+
#
|
46
|
+
def aliases
|
47
|
+
class_variable_get(:@@aliases).dup
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module SubclassAware
|
4
|
+
|
5
|
+
def self.extended (klass) #nodoc;
|
6
|
+
klass.class_exec { class_variable_set(:@@subclasses, Set.new) }
|
7
|
+
end
|
8
|
+
|
9
|
+
# TODO: Find a way for self.inherited on the extended class not to blow
|
10
|
+
# this away without requiring a bunch of alias chain hoops to be jumped
|
11
|
+
# through.
|
12
|
+
def inherited(sub) #nodoc;
|
13
|
+
class_exec { class_variable_get(:@@subclasses).add sub }
|
14
|
+
end
|
15
|
+
|
16
|
+
##
|
17
|
+
# Return an array of all known subclasses (and sub-subclasses, etc) of this
|
18
|
+
# class.
|
19
|
+
#
|
20
|
+
def subclasses
|
21
|
+
class_exec { class_variable_get(:@@subclasses).to_a }
|
22
|
+
end
|
23
|
+
|
24
|
+
##
|
25
|
+
# Clear all info about known subclasses. Usefull for testing, but it is
|
26
|
+
# unlikely you would use it for much else.
|
27
|
+
#
|
28
|
+
def forget_subclasses
|
29
|
+
class_exec { class_variable_get(:@@subclasses).clear }
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
data/lib/classy.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe "Aliasable" do
|
4
|
+
|
5
|
+
# TODO: It would be better to tear these down and rebuild them before each
|
6
|
+
# test, since some of the tests add or remove aliases.
|
7
|
+
before :all do
|
8
|
+
class Base
|
9
|
+
extend Aliasable
|
10
|
+
aka :base
|
11
|
+
end
|
12
|
+
|
13
|
+
class ChildA < Base
|
14
|
+
aka :childA, :first_child
|
15
|
+
end
|
16
|
+
|
17
|
+
class ChildB < Base
|
18
|
+
aka :childB
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".aliases" do
|
24
|
+
it "should return all known aliases" do
|
25
|
+
Base.aliases.should eql({
|
26
|
+
:base => Base,
|
27
|
+
:childA => ChildA,
|
28
|
+
:first_child => ChildA,
|
29
|
+
:childB => ChildB
|
30
|
+
})
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ".aka" do
|
35
|
+
it "should allow an alias to be set" do
|
36
|
+
lambda {
|
37
|
+
class ChildC < Base
|
38
|
+
aka :childC
|
39
|
+
end
|
40
|
+
}.should_not raise_error
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '.find' do
|
45
|
+
it "should allow you to look up a class via its alias" do
|
46
|
+
Base.find(:first_child).should equal ChildA
|
47
|
+
Base.find(:childB).should equal ChildB
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should keep aliases of different class hierarchies separate" do
|
52
|
+
class AnotherBase
|
53
|
+
extend Aliasable
|
54
|
+
end
|
55
|
+
|
56
|
+
lambda {
|
57
|
+
class AnotherChildA < AnotherBase
|
58
|
+
aka :first_child
|
59
|
+
end
|
60
|
+
}.should_not raise_error
|
61
|
+
|
62
|
+
AnotherBase.find(:first_child).should equal AnotherChildA
|
63
|
+
Base.find(:first_child).should equal ChildA
|
64
|
+
end
|
65
|
+
|
66
|
+
it "shouldn't allow the same alias to be used more than once in the same hierarchy" do
|
67
|
+
lambda {
|
68
|
+
class BogusDuplicateChildA < Base
|
69
|
+
aka :childA
|
70
|
+
end
|
71
|
+
}.should raise_error( ArgumentError, /already taken/ )
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '.forget_aliases' do
|
75
|
+
it "should clear all aliases for this hierarchy" do
|
76
|
+
Base.forget_aliases
|
77
|
+
Base.aliases.should be_empty
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe "SubclassAware" do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
# TODO: It would be nicer if this were torn down and re-built between each test.
|
7
|
+
|
8
|
+
class ParentA
|
9
|
+
extend SubclassAware
|
10
|
+
|
11
|
+
# This is kind of a pain in the butt, but it's what you need to do if you
|
12
|
+
# define your own self.inherited to keep from blowing away the one
|
13
|
+
# SubclassAware sets up. There's a todo in subclass_aware.rb about this.
|
14
|
+
class << self; alias :old_inherited :inherited end
|
15
|
+
def self.inherited(sub)
|
16
|
+
old_inherited(sub)
|
17
|
+
# do your stuff here
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class ParentB
|
22
|
+
extend SubclassAware
|
23
|
+
end
|
24
|
+
|
25
|
+
class SubclassA1 < ParentA; end
|
26
|
+
class SubclassA2 < ParentA; end
|
27
|
+
class SubclassA3 < ParentA; end
|
28
|
+
|
29
|
+
class SubclassB1 < ParentB; end
|
30
|
+
class SubclassB2 < ParentB; end
|
31
|
+
class SubclassB3 < ParentB; end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should keep track of all subclasses (and sub-sub, etc. classes) of a module which extends it" do
|
35
|
+
ParentA::subclasses.should include(SubclassA1, SubclassA2, SubclassA3)
|
36
|
+
ParentB::subclasses.should include(SubclassB1, SubclassB2, SubclassB3)
|
37
|
+
ParentA::subclasses.should have(3).subclasses
|
38
|
+
ParentB::subclasses.should have(3).subclasses
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not confuse the subclasses of two different extending classes" do
|
42
|
+
ParentA.subclasses.should_not include(SubclassB1, SubclassB2, SubclassB3)
|
43
|
+
ParentB.subclasses.should_not include(SubclassA1, SubclassA2, SubclassA3)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should forget all subclasses when forget_subclasses() is called" do
|
47
|
+
ParentA.forget_subclasses
|
48
|
+
ParentA.subclasses.should be_empty
|
49
|
+
|
50
|
+
ParentB::subclasses.should include(SubclassB1, SubclassB2, SubclassB3)
|
51
|
+
ParentB.subclasses.should have(3).subclasses
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: classy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Hyland
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-24 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: Classy is a collection of meta-programming heavy modules which you can extend in order to give various capabilities to your Ruby classes. For example, SubclassAware lets a class know about all of its subclasses (and sub-subclasses, etc), and Aliasable lets you refer to classes via symbols (useful for creating friendly DSLs).
|
26
|
+
email: github@djspinmonkey.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/classy.rb
|
42
|
+
- lib/classy/aliasable.rb
|
43
|
+
- lib/classy/subclass_aware.rb
|
44
|
+
- spec/aliasable_spec.rb
|
45
|
+
- spec/spec.opts
|
46
|
+
- spec/spec_helper.rb
|
47
|
+
- spec/subclass_aware_spec.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: http://github.com/djspinmonkey/classy
|
50
|
+
licenses: []
|
51
|
+
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.3.5
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: A collection of modules to enhance the capabilities of Ruby classes in various ways.
|
76
|
+
test_files:
|
77
|
+
- spec/aliasable_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/subclass_aware_spec.rb
|