typecollection 0.0.2

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 James Petty
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
File without changes
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "typecollection"
18
+ gem.homepage = "http://github.com/pettyjamesm/TypeCollection"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{TypeCollection maintains a record of sub-types for a given
21
+ class based on a common suffix.}
22
+ gem.description = %Q{Easily map subtypes into their parent type for later retrieval}
23
+ gem.email = "jp@jibe.com"
24
+ gem.authors = ["James Petty"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ test.rcov_opts << '--exclude "gems/*"'
42
+ end
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "typecollection #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,11 @@
1
+ require 'typecollection'
2
+ require 'typecollection/class+inferred_type'
3
+ require 'typecollection/class_methods'
4
+
5
+ module TypeCollection
6
+ # The intended means to use a TypeCollection is to include TypeCollection
7
+ # in your class definition.
8
+ def self.included(base)
9
+ base.extend(TypeCollection::ClassMethods)
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class Class
2
+ def inferred_type()
3
+ klass_name = self.name.split("::").last
4
+ parent_klass = self.superclass
5
+ while(parent_klass != nil)
6
+ check = parent_klass.name.split("::").last
7
+ if (klass_name.end_with?(check))
8
+ return klass_name.gsub(/#{check}\z/, "")
9
+ end
10
+ parent_klass = parent_klass.superclass
11
+ end
12
+ return nil
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ module TypeCollection
2
+ # Indicates that a TypeCollection has been extended by a base class that
3
+ # fails to comply with the correct naming convention for TypeCollection
4
+ # to function properly
5
+ class InvalidChildType < StandardError; end
6
+
7
+ # Indicates that a request has been made to retrieve a child type that
8
+ # is not currently registered with the type collection
9
+ class UnknownChildType < StandardError; end
10
+
11
+ # Extended by TypeCollections to provide class level functionality (almost
12
+ # all functionality is class level as of the current design)
13
+ module ClassMethods
14
+ # Contains the Members mapped by type
15
+ def __tc_members(); @_members ||= { }; end
16
+
17
+ # Returns all of the known subclasses for this collection
18
+ def all_types()
19
+ return __tc_members().values()
20
+ end
21
+
22
+ # Gets the type associated with the provided value (Class or Otherwise)
23
+ def get_type(type)
24
+ type = type.inferred_type() if (type.kind_of?(Class))
25
+ mems = __tc_members()
26
+ raise TypeCollection::UnknownChildType.new() unless (mems.has_key?(type))
27
+ return mems[type]
28
+ end
29
+
30
+ # Overrides the default behavior when being extended by a child class.
31
+ # It ensures the child is mapped for future retrieval and checks the
32
+ # subclass name to ensure it is a valid one
33
+ def inherited(child)
34
+ super if (defined?(super))
35
+ type = child.inferred_type()
36
+ if (type.nil?)
37
+ error = "Invalid Name: #{child.name}! Child class names must end with \"#{self.name}\"."
38
+ raise TypeCollection::InvalidChildType.new(error)
39
+ end
40
+ __tc_members()[type] = child
41
+ end
42
+ end
43
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'typecollection'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,19 @@
1
+ require 'helper'
2
+
3
+ class TestTypecollection < Test::Unit::TestCase
4
+ should "Actually Function" do
5
+ require 'typecollection'
6
+ # => Define a Type Collection
7
+ class SomeType
8
+ include TypeCollection
9
+ end
10
+ # => Extend that Type Collection
11
+ class ExtendedSomeType < SomeType
12
+
13
+ end
14
+ # => Ensure it can be retrieved
15
+ unless (SomeType.all_types().length == 1 and SomeType.get_type("Extended") == ExtendedSomeType)
16
+ flunk "Failed to Register Extended with SomeType!"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "typecollection"
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Petty"]
12
+ s.date = "2012-01-10"
13
+ s.description = "Easily map subtypes into their parent type for later retrieval"
14
+ s.email = "jp@jibe.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/typecollection.rb",
27
+ "lib/typecollection/class+inferred_type.rb",
28
+ "lib/typecollection/class_methods.rb",
29
+ "test/helper.rb",
30
+ "test/test_typecollection.rb",
31
+ "typecollection.gemspec"
32
+ ]
33
+ s.homepage = "http://github.com/pettyjamesm/TypeCollection"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.10"
37
+ s.summary = "TypeCollection maintains a record of sub-types for a given class based on a common suffix."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
46
+ s.add_development_dependency(%q<rcov>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
51
+ s.add_dependency(%q<rcov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
57
+ s.add_dependency(%q<rcov>, [">= 0"])
58
+ end
59
+ end
60
+
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typecollection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Petty
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoulda
16
+ requirement: &70319050220100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70319050220100
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &70319050215500 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.0.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70319050215500
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &70319050210820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.6.4
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70319050210820
47
+ - !ruby/object:Gem::Dependency
48
+ name: rcov
49
+ requirement: &70319050200520 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70319050200520
58
+ description: Easily map subtypes into their parent type for later retrieval
59
+ email: jp@jibe.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files:
63
+ - LICENSE.txt
64
+ - README.rdoc
65
+ files:
66
+ - .document
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.rdoc
70
+ - Rakefile
71
+ - VERSION
72
+ - lib/typecollection.rb
73
+ - lib/typecollection/class+inferred_type.rb
74
+ - lib/typecollection/class_methods.rb
75
+ - test/helper.rb
76
+ - test/test_typecollection.rb
77
+ - typecollection.gemspec
78
+ homepage: http://github.com/pettyjamesm/TypeCollection
79
+ licenses:
80
+ - MIT
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ segments:
92
+ - 0
93
+ hash: -698932350453145529
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.10
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: TypeCollection maintains a record of sub-types for a given class based on
106
+ a common suffix.
107
+ test_files: []