foxy_factory 0.1.1
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.markdown +155 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/foxy_factory.gemspec +58 -0
- data/lib/foxy_factory.rb +155 -0
- data/spec/factory/factory_create_spec.rb +35 -0
- data/spec/factory/factory_fixture.rb +34 -0
- data/spec/factory/factory_nested_ns_spec.rb +28 -0
- data/spec/factory/factory_spec.rb +54 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +8 -0
- metadata +82 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
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.markdown
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
# Foxy Factory #
|
2
|
+
|
3
|
+
Find Constants, including Modules and Classes registered in the Ruby kernel using convenient finder methods.
|
4
|
+
Create new instances factory style. This is a way more advanced utility than 'constantize' and similar more "primitive" factory methods.
|
5
|
+
Foxy Factory caches each successful lookup for faster future retrieval, instead of having to go through the kernel.
|
6
|
+
If a constant is not found in the cache, a kernel lookup will always be performed as a fall-back.
|
7
|
+
|
8
|
+
## Install ##
|
9
|
+
|
10
|
+
<code>$ gem install foxy_factory</code>
|
11
|
+
|
12
|
+
## Usage ##
|
13
|
+
|
14
|
+
<code>require 'foxy_factory'</code>
|
15
|
+
|
16
|
+
## Configuration ##
|
17
|
+
|
18
|
+
Imagine this namespace structure:
|
19
|
+
|
20
|
+
<pre><code>
|
21
|
+
class Basic
|
22
|
+
def initialize(number, say = "Hello", &block)
|
23
|
+
@number = number
|
24
|
+
@say = say
|
25
|
+
if block
|
26
|
+
block.arity < 1 ? obj.instance_eval(&block) : block.call(obj)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module Howrah
|
32
|
+
class MyClass < Basic
|
33
|
+
def initialize(number, &block)
|
34
|
+
super
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module MyModule
|
39
|
+
class MyNestedClass < Basic
|
40
|
+
def initialize(number, &block)
|
41
|
+
super
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module MyNestedModule
|
46
|
+
class MyDoubleNestedClass
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
</code></pre>
|
52
|
+
|
53
|
+
Create factory with base namespace 'Howrah' that this factory will use as the root (or base) namespace
|
54
|
+
|
55
|
+
<code>factory = Foxy::Factory.new :howrah</code>
|
56
|
+
|
57
|
+
You can at any time change the implicit root for a given factory
|
58
|
+
<pre><code>
|
59
|
+
factory = Foxy::Factory.new
|
60
|
+
...
|
61
|
+
factory.base_namespace = :my_root
|
62
|
+
</code></pre>
|
63
|
+
|
64
|
+
## Find constant ##
|
65
|
+
|
66
|
+
Find constant `Howrah::MyClass`
|
67
|
+
<pre><code>factory.find_constant :my_class
|
68
|
+
|
69
|
+
=> Howrah::MyClass
|
70
|
+
</code></pre>
|
71
|
+
|
72
|
+
Find constant `Howrah::NonExistingClass` - should raise error
|
73
|
+
<pre><code>factory.find_constant :non_existing_class
|
74
|
+
|
75
|
+
=> error: ConstantNotFoundError
|
76
|
+
</code></pre>
|
77
|
+
|
78
|
+
Find constant `Howrah::MyModule`
|
79
|
+
<pre><code>factory.find_constant :my_module
|
80
|
+
|
81
|
+
=> Howrah::MyModule
|
82
|
+
</code></pre>
|
83
|
+
|
84
|
+
Find constant `Howrah::MyModule::MyNestedClass`
|
85
|
+
|
86
|
+
<pre><code>factory.find_constant :my_module, :my_nested_class
|
87
|
+
|
88
|
+
=> Howrah::MyModule::MyNestedClass
|
89
|
+
</code></pre>
|
90
|
+
|
91
|
+
Find constant `Howrah::MyModule::MyNestedModule::MyDoubleNestedClass`
|
92
|
+
<pre><code>factory.find_constant :my_module, :my_nested_module, :my_double_nested_class
|
93
|
+
|
94
|
+
=> Howrah::MyModule::MyNestedModule::MyDoubleNestedClass
|
95
|
+
</code></pre>
|
96
|
+
|
97
|
+
## Find module ##
|
98
|
+
|
99
|
+
Find module `Howrah::MyModule`
|
100
|
+
<pre><code>factory.find_module :my_module
|
101
|
+
=> Howrah::MyModule
|
102
|
+
</code></pre>
|
103
|
+
|
104
|
+
Find module `Howrah::MyModule::MyNestedModule`
|
105
|
+
<pre><code>factory.find_module :my_module, :my_nested_module
|
106
|
+
|
107
|
+
=> Howrah::MyModule::MyNestedModule
|
108
|
+
</code></pre>
|
109
|
+
|
110
|
+
Find module `Howrah::MyModule::MyNestedModule::MyNestedClass` but not class!
|
111
|
+
<pre><code>factory.find_module! :my_module, :my_nested_class
|
112
|
+
|
113
|
+
=> error: ConstantIsNotModuleError
|
114
|
+
</code></pre>
|
115
|
+
|
116
|
+
## Find Class ##
|
117
|
+
|
118
|
+
Find class `Howrah::MyModule`
|
119
|
+
<pre><code>factory.find_class :my_module
|
120
|
+
|
121
|
+
=> error: ConstantIsNotClassError
|
122
|
+
</code></pre>
|
123
|
+
|
124
|
+
Find class `Howrah::MyModule::MyNestedModule::MyDoubleNestedClass`
|
125
|
+
<pre><code>factory.find_class :my_module, :my_nested_module, :my_nested_class
|
126
|
+
|
127
|
+
=> Howrah::MyModule::MyNestedModule::MyDoubleNestedClass
|
128
|
+
</code></pre>
|
129
|
+
|
130
|
+
## Create instance ##
|
131
|
+
|
132
|
+
<pre><code> factory.create :my_module, :my_nested_class, :args => [2, 'Hello'] do |p|
|
133
|
+
puts p.number * p.say
|
134
|
+
end
|
135
|
+
|
136
|
+
=> Hello Hello
|
137
|
+
|
138
|
+
factory.create :my_class, :args => 2 do |p|
|
139
|
+
puts p.number * "Hello "
|
140
|
+
end
|
141
|
+
</code></pre>
|
142
|
+
|
143
|
+
## Note on Patches/Pull Requests ##
|
144
|
+
|
145
|
+
* Fork the project.
|
146
|
+
* Make your feature addition or bug fix.
|
147
|
+
* Add tests for it. This is important so I don't break it in a
|
148
|
+
future version unintentionally.
|
149
|
+
* Commit, do not mess with rakefile, version, or history.
|
150
|
+
(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)
|
151
|
+
* Send me a pull request. Bonus points for topic branches.
|
152
|
+
|
153
|
+
## Copyright ##
|
154
|
+
|
155
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gem|
|
4
|
+
gem.name = "foxy_factory"
|
5
|
+
gem.summary = %Q{Easily find registered constants, modules and classes}
|
6
|
+
gem.description = %Q{Find a constant registered by Ruby kernel and cache it for next retrieval}
|
7
|
+
gem.email = "kmandrup@gmail.com"
|
8
|
+
gem.homepage = "http://github.com/kristianmandrup/factory"
|
9
|
+
gem.authors = ["Kristian Mandrup"]
|
10
|
+
# gem.add_development_dependency "rspec", ">= 1.2.9"
|
11
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
12
|
+
end
|
13
|
+
Jeweler::GemcutterTasks.new
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
# require 'spec/rake/spectask'
|
19
|
+
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
20
|
+
# spec.libs << 'lib' << 'spec'
|
21
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# Spec::Rake::SpecTask.new(:rcov) do |spec|
|
25
|
+
# spec.libs << 'lib' << 'spec'
|
26
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
27
|
+
# spec.rcov = true
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# task :spec => :check_dependencies
|
31
|
+
#
|
32
|
+
# task :default => :spec
|
33
|
+
#
|
34
|
+
# require 'rake/rdoctask'
|
35
|
+
# Rake::RDocTask.new do |rdoc|
|
36
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
37
|
+
#
|
38
|
+
# rdoc.rdoc_dir = 'rdoc'
|
39
|
+
# rdoc.title = "factory #{version}"
|
40
|
+
# rdoc.rdoc_files.include('README*')
|
41
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
42
|
+
# end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{foxy_factory}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kristian Mandrup"]
|
12
|
+
s.date = %q{2010-06-18}
|
13
|
+
s.description = %q{Find a constant registered by Ruby kernel and cache it for next retrieval}
|
14
|
+
s.email = %q{kmandrup@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.markdown",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"foxy_factory.gemspec",
|
27
|
+
"lib/foxy_factory.rb",
|
28
|
+
"spec/factory/factory_create_spec.rb",
|
29
|
+
"spec/factory/factory_fixture.rb",
|
30
|
+
"spec/factory/factory_nested_ns_spec.rb",
|
31
|
+
"spec/factory/factory_spec.rb",
|
32
|
+
"spec/spec.opts",
|
33
|
+
"spec/spec_helper.rb"
|
34
|
+
]
|
35
|
+
s.homepage = %q{http://github.com/kristianmandrup/factory}
|
36
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
37
|
+
s.require_paths = ["lib"]
|
38
|
+
s.rubygems_version = %q{1.3.7}
|
39
|
+
s.summary = %q{Easily find registered constants, modules and classes}
|
40
|
+
s.test_files = [
|
41
|
+
"spec/factory/factory_create_spec.rb",
|
42
|
+
"spec/factory/factory_fixture.rb",
|
43
|
+
"spec/factory/factory_nested_ns_spec.rb",
|
44
|
+
"spec/factory/factory_spec.rb",
|
45
|
+
"spec/spec_helper.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
53
|
+
else
|
54
|
+
end
|
55
|
+
else
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/foxy_factory.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
module Foxy
|
2
|
+
class Factory
|
3
|
+
attr_reader :ns_registry, :base_namespace
|
4
|
+
|
5
|
+
class ConstantNotFoundError < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
class ConstantIsNotClassError < StandardError
|
9
|
+
end
|
10
|
+
|
11
|
+
class ConstantIsNotModuleError < StandardError
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def initialize(*namespaces)
|
16
|
+
# raise ArgumentError, "Factory must be initialized with an array of modules to set the base namespace" if namespaces.empty?
|
17
|
+
@ns_registry = namespaces.last.kind_of?(Hash) ? remove_last(namespaces) : {}
|
18
|
+
@base_namespace = namespace(namespaces)
|
19
|
+
end
|
20
|
+
|
21
|
+
def create(*args, &block)
|
22
|
+
if !args.last.kind_of?(Hash) || !args.last[:args]
|
23
|
+
raise ArgumentError, "Last argument before block must be a hash of type :args => [...]" if block
|
24
|
+
raise ArgumentError, "Last argument must be a hash of type :args => [...]"
|
25
|
+
end
|
26
|
+
instance_args = args.delete(args.last)[:args]
|
27
|
+
ns_list, name = parse_args(args)
|
28
|
+
namespaces = get_namespaces(ns_list)
|
29
|
+
obj = find_class(namespaces, name).new *instance_args
|
30
|
+
if block
|
31
|
+
block.arity < 1 ? obj.instance_eval(&block) : block.call(obj)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def find_class(*args)
|
36
|
+
ns_list, name = parse_args(args)
|
37
|
+
namespaces = get_namespaces(ns_list)
|
38
|
+
clazz = find_constant(*args)
|
39
|
+
return clazz if clazz.is_a? Class
|
40
|
+
ns = full_ns_name(namespaces, name)
|
41
|
+
raise Factory::ConstantIsNotClassError, "#{ns} is not a class"
|
42
|
+
end
|
43
|
+
|
44
|
+
def find_module(*args)
|
45
|
+
ns_list, name = parse_args(args)
|
46
|
+
namespaces = get_namespaces(ns_list)
|
47
|
+
m_dule = find_constant(*args)
|
48
|
+
return m_dule if m_dule.is_a?(Module)
|
49
|
+
ns = full_ns_name(namespaces, name)
|
50
|
+
raise Factory::ConstantIsNotModuleError, "#{ns} is not a module"
|
51
|
+
end
|
52
|
+
|
53
|
+
def find_module!(*args)
|
54
|
+
ns_list, name = parse_args(args)
|
55
|
+
namespaces = get_namespaces(ns_list)
|
56
|
+
m_dule = find_constant(*args)
|
57
|
+
return m_dule if m_dule.is_a?(Module) && !m_dule.is_a?(Class)
|
58
|
+
ns = full_ns_name(namespaces, name)
|
59
|
+
raise Factory::ConstantIsNotModuleError, "#{ns} is not a module"
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
def find_constant(*args)
|
64
|
+
ns_list, name = parse_args(args)
|
65
|
+
namespaces = get_namespaces(ns_list)
|
66
|
+
ns = namespace(namespaces)
|
67
|
+
|
68
|
+
# put entry in cache for namespace if not present
|
69
|
+
register_namespace(ns) if !registered?(ns)
|
70
|
+
|
71
|
+
# get namespace entry in cache
|
72
|
+
cache = ns_cache(ns)
|
73
|
+
begin
|
74
|
+
if !cache[name]
|
75
|
+
full_name = ns_name(ns, name)
|
76
|
+
cache[name] = constantize(full_name)
|
77
|
+
end
|
78
|
+
cache[name]
|
79
|
+
rescue
|
80
|
+
raise Factory::ConstantNotFoundError, "No constant found for #{camelize(name)} in namespace #{real_ns_name(ns)}"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
# From ActiveSupport/inflector.rb
|
86
|
+
def constantize(camel_cased_word)
|
87
|
+
Factory.get_const(camel_cased_word)
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.get_const(camel_cased_word)
|
91
|
+
names = camel_cased_word.split('::')
|
92
|
+
names.shift if names.empty? || names.first.empty?
|
93
|
+
constant = Object
|
94
|
+
names.each do |name|
|
95
|
+
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
|
96
|
+
end
|
97
|
+
constant
|
98
|
+
end
|
99
|
+
|
100
|
+
def real_ns_name(ns)
|
101
|
+
return camelize(base_namespace) if ns == 'RootNs'
|
102
|
+
ns
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse_args(args)
|
106
|
+
raise ArgumentError, "Supplied options must not be empty" if !args || args.empty?
|
107
|
+
ns_list = [] if args.size == 1
|
108
|
+
ns_list = args[0..-2] if args.size > 1
|
109
|
+
name = args.last
|
110
|
+
[ns_list, name]
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
def get_namespaces(ns_list)
|
115
|
+
return [:root_ns] if !ns_list || ns_list.empty?
|
116
|
+
ns_list
|
117
|
+
end
|
118
|
+
|
119
|
+
def remove_last(namespaces)
|
120
|
+
namespaces.delete(namespaces.last)
|
121
|
+
end
|
122
|
+
|
123
|
+
def register_namespace(ns)
|
124
|
+
ns_registry[ns] = {}
|
125
|
+
end
|
126
|
+
|
127
|
+
def registered?(ns)
|
128
|
+
ns_cache(ns)
|
129
|
+
end
|
130
|
+
|
131
|
+
def ns_cache(ns)
|
132
|
+
ns_registry[ns]
|
133
|
+
end
|
134
|
+
|
135
|
+
def ns_name(ns, name)
|
136
|
+
return "#{base_namespace}::#{camelize(name)}" if ns == 'RootNs'
|
137
|
+
"#{base_namespace}::#{ns}::#{camelize(name)}"
|
138
|
+
end
|
139
|
+
|
140
|
+
def full_ns_name(*namespaces, name)
|
141
|
+
ns = namespace(namespaces)
|
142
|
+
ns_name(ns, name)
|
143
|
+
end
|
144
|
+
|
145
|
+
def namespace(namespaces)
|
146
|
+
raise ArgumentError, "Argument must be an array of namespaces" if !namespaces.kind_of? Array
|
147
|
+
namespaces.map{|n| camelize(n.to_s)}.join('::')
|
148
|
+
end
|
149
|
+
|
150
|
+
def camelize(str)
|
151
|
+
str.to_s.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
require 'factory/factory_fixture'
|
3
|
+
|
4
|
+
module Foxy
|
5
|
+
describe Factory do
|
6
|
+
before(:each) do
|
7
|
+
@clean_factory = Foxy::Factory.new :howrah
|
8
|
+
end
|
9
|
+
|
10
|
+
context "clean factory" do
|
11
|
+
it "should NOT find constant 'base' in 'bad' namespace" do
|
12
|
+
lambda { @clean_factory.create(:bad, 'base') }.should raise_error ArgumentError
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should create class 'my_nested_class' in 'my_module' namespace as it's a class" do
|
16
|
+
@clean_factory.create(:my_class, :args => 2) do |p|
|
17
|
+
p.number.should == 2
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should create class 'my_nested_class' in 'my_module' namespace as it's a class" do
|
22
|
+
@clean_factory.create(:my_module, :my_nested_class, :args => [2]) do |p|
|
23
|
+
p.number.should == 2
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should create class 'my_nested_class' in 'my_module' namespace as it's a class" do
|
28
|
+
@clean_factory.create(:my_module, :my_nested_class, :args => [2, 'Goodbye']) do |p|
|
29
|
+
p.number.should == 2
|
30
|
+
p.say.should == "Goodbye"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
puts "Fixture loaded"
|
2
|
+
|
3
|
+
class Basic
|
4
|
+
attr_reader :number, :say
|
5
|
+
|
6
|
+
def initialize(number, say = "Hello", &block)
|
7
|
+
@number = number
|
8
|
+
@say = say
|
9
|
+
if block
|
10
|
+
block.arity < 1 ? obj.instance_eval(&block) : block.call(obj)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module Howrah
|
16
|
+
class MyClass < Basic
|
17
|
+
def initialize(number, say = "Hello", &block)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module MyModule
|
23
|
+
class MyNestedClass < Basic
|
24
|
+
def initialize(number, say = "Hello", &block)
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module MyNestedModule
|
30
|
+
class MyDoubleNestedClass
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
require 'factory/factory_fixture'
|
3
|
+
|
4
|
+
module Foxy
|
5
|
+
describe Factory do
|
6
|
+
before(:each) do
|
7
|
+
@clean_factory = Foxy::Factory.new :howrah
|
8
|
+
end
|
9
|
+
|
10
|
+
context "clean factory" do
|
11
|
+
it "should NOT find constant 'base' in 'bad' namespace" do
|
12
|
+
lambda { @clean_factory.find_constant(:bad, 'base') }.should raise_error Foxy::Factory::ConstantNotFoundError
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should find class 'my_nested_class' in 'my_module' namespace as it's a class" do
|
16
|
+
@clean_factory.find_class(:my_module, 'my_nested_class').should_not be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should NOT find class 'my_nested_module' in 'my_module' namespace as it's a module" do
|
20
|
+
lambda { @clean_factory.find_class(:my_module, :my_nested_module) }.should raise_error Foxy::Factory::ConstantIsNotClassError
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should find constant 'my_double_nested_class' in 'my_module::mynested_module' namespace" do
|
24
|
+
@clean_factory.find_constant(:my_module, :my_nested_module, :my_double_nested_class).should_not be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
require 'factory/factory_fixture'
|
3
|
+
|
4
|
+
module Foxy
|
5
|
+
describe Factory do
|
6
|
+
before(:each) do
|
7
|
+
@clean_factory = Foxy::Factory.new :howrah
|
8
|
+
end
|
9
|
+
|
10
|
+
context ":howrah factory" do
|
11
|
+
it "should have an empty namespace registry" do
|
12
|
+
@clean_factory.ns_registry.should == {}
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#find_constant" do
|
16
|
+
it "should raise ArgumentError if no arguments given" do
|
17
|
+
lambda { @clean_factory.find_constant }.should raise_error ArgumentError
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should NOT find constant 'bad_class'" do
|
21
|
+
lambda { @clean_factory.find_constant('bad_class') }.should raise_error Foxy::Factory::ConstantNotFoundError
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should find constant 'my_class' in 'howrah' namespace" do
|
25
|
+
@clean_factory.find_constant('my_class').should
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should find constant 'my_module' in 'howrah' namespace" do
|
29
|
+
@clean_factory.find_constant('my_module').should
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#find_class" do
|
34
|
+
it "should NOT find class 'my_module' in 'howrah' namespace as it's a module" do
|
35
|
+
lambda { @clean_factory.find_class('my_module') }.should raise_error Foxy::Factory::ConstantIsNotClassError
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should find class 'my_class' in 'howrah' namespace as it's a module" do
|
39
|
+
@clean_factory.find_class('my_class').should
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "#find_module" do
|
44
|
+
it "should NOT find module 'my_class' in 'howrah' namespace as it's a module" do
|
45
|
+
lambda { @clean_factory.find_module('my_class') }.should raise_error Foxy::Factory::ConstantIsNotModuleError
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should find module 'my_module' in 'howrah' namespace as it's a module" do
|
49
|
+
@clean_factory.find_module('my_module').should
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: foxy_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-18 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Find a constant registered by Ruby kernel and cache it for next retrieval
|
22
|
+
email: kmandrup@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.markdown
|
30
|
+
files:
|
31
|
+
- .document
|
32
|
+
- .gitignore
|
33
|
+
- LICENSE
|
34
|
+
- README.markdown
|
35
|
+
- Rakefile
|
36
|
+
- VERSION
|
37
|
+
- foxy_factory.gemspec
|
38
|
+
- lib/foxy_factory.rb
|
39
|
+
- spec/factory/factory_create_spec.rb
|
40
|
+
- spec/factory/factory_fixture.rb
|
41
|
+
- spec/factory/factory_nested_ns_spec.rb
|
42
|
+
- spec/factory/factory_spec.rb
|
43
|
+
- spec/spec.opts
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/kristianmandrup/factory
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Easily find registered constants, modules and classes
|
77
|
+
test_files:
|
78
|
+
- spec/factory/factory_create_spec.rb
|
79
|
+
- spec/factory/factory_fixture.rb
|
80
|
+
- spec/factory/factory_nested_ns_spec.rb
|
81
|
+
- spec/factory/factory_spec.rb
|
82
|
+
- spec/spec_helper.rb
|