instantiator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ ## Instantiator
2
+
3
+ I want to be able to instantiate an arbitrary Ruby class without knowing anything about the constructor.
4
+
5
+ ### Usage
6
+
7
+ class ArbitraryClass
8
+ def initialize(arg1, arg2, *other_args)
9
+ # stuff
10
+ end
11
+ end
12
+
13
+ require "instantiator"
14
+
15
+ instance = ArbitraryClass.instantiate
16
+
17
+ p instance.class # => ArbitraryClass
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ task :default => :test
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << "test"
8
+ t.test_files = FileList['test/*_test.rb']
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "instantiator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "instantiator"
7
+ s.version = Instantiator::VERSION
8
+ s.authors = ["James Mead"]
9
+ s.email = ["james@floehopper.org"]
10
+ s.homepage = ""
11
+ s.summary = %q{Instantiate an arbitrary Ruby class}
12
+
13
+ s.rubyforge_project = "instantiator"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency("blankslate")
21
+ end
@@ -0,0 +1,27 @@
1
+ require "instantiator/version"
2
+ require "blankslate"
3
+
4
+ module Instantiator
5
+
6
+ class MethodInvocationSink < ::BlankSlate
7
+ def method_missing(*args, &block)
8
+ MethodInvocationSink.new
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ def instantiate
14
+ if singleton_methods(false).map(&:to_sym).include?(:new)
15
+ arity = method(:new).arity
16
+ else
17
+ arity = instance_method(:initialize).arity
18
+ end
19
+ number_of_parameters = arity >= 0 ? arity : -(arity + 1)
20
+ new(*Array.new(number_of_parameters) { MethodInvocationSink.new })
21
+ end
22
+ end
23
+ end
24
+
25
+ class Class
26
+ include Instantiator::ClassMethods
27
+ end
@@ -0,0 +1,3 @@
1
+ module Instantiator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,195 @@
1
+ require "test_helper"
2
+ require "instantiator"
3
+
4
+ class InstantiatorTest < Test::Unit::TestCase
5
+ def test_should_instantiate_class_with_no_explicit_initialize
6
+ klass = Class.new
7
+ instance = klass.instantiate
8
+ assert_equal klass, instance.class
9
+ end
10
+
11
+ def test_should_instantiate_class_with_initialize_with_no_parameters
12
+ klass = Class.new do
13
+ def initialize; end
14
+ end
15
+ instance = klass.instantiate
16
+ assert_equal klass, instance.class
17
+ end
18
+
19
+ def test_should_instantiate_class_with_initialize_with_one_parameter
20
+ klass = Class.new do
21
+ def initialize(parameter_one); end
22
+ end
23
+ instance = klass.instantiate
24
+ assert_equal klass, instance.class
25
+ end
26
+
27
+ def test_should_instantiate_class_with_initialize_with_multiple_parameters
28
+ klass = Class.new do
29
+ def initialize(parameter_one, parameter_two, parameter_three, parameter_four); end
30
+ end
31
+ instance = klass.instantiate
32
+ assert_equal klass, instance.class
33
+ end
34
+
35
+ def test_should_instantiate_class_with_initialize_with_only_optional_parameters
36
+ klass = Class.new do
37
+ def initialize(*optional_parameters); end
38
+ end
39
+ instance = klass.instantiate
40
+ assert_equal klass, instance.class
41
+ end
42
+
43
+ def test_should_instantiate_class_with_initialize_with_some_required_parameters_and_some_optional_parameters
44
+ klass = Class.new do
45
+ def initialize(parameter_one, parameter_two, *optional_parameters); end
46
+ end
47
+ instance = klass.instantiate
48
+ assert_equal klass, instance.class
49
+ end
50
+
51
+ def test_should_instantiate_class_which_calls_method_on_parameter
52
+ klass = Class.new do
53
+ def initialize(parameter_one)
54
+ parameter_one.foo
55
+ end
56
+ end
57
+ instance = klass.instantiate
58
+ assert_equal klass, instance.class
59
+ end
60
+
61
+ def test_should_instantiate_class_which_calls_method_on_multiple_parameters
62
+ klass = Class.new do
63
+ def initialize(parameter_one, parameter_two, parameter_three, parameter_four)
64
+ parameter_one.foo
65
+ parameter_two.foo
66
+ parameter_three.foo
67
+ parameter_four.foo
68
+ end
69
+ end
70
+ instance = klass.instantiate
71
+ assert_equal klass, instance.class
72
+ end
73
+
74
+ def test_should_instantiate_class_which_calls_method_on_result_of_calling_method_on_parameter
75
+ klass = Class.new do
76
+ def initialize(parameter_one)
77
+ parameter_one.foo.bar
78
+ end
79
+ end
80
+ instance = klass.instantiate
81
+ assert_equal klass, instance.class
82
+ end
83
+
84
+ def test_should_instantiate_class_which_defines_its_own_new_method_with_no_parameters
85
+ klass = Class.new do
86
+ class << self
87
+ alias_method :__new__, :new
88
+ def new
89
+ __new__("parameter_one")
90
+ end
91
+ end
92
+ def initialize(parameter_one); end
93
+ end
94
+ instance = klass.instantiate
95
+ assert_equal klass, instance.class
96
+ end
97
+
98
+ def test_should_instantiate_class_which_defines_its_own_new_method_with_one_parameter
99
+ klass = Class.new do
100
+ class << self
101
+ alias_method :__new__, :new
102
+ def new(parameter_one)
103
+ __new__
104
+ end
105
+ end
106
+ end
107
+ instance = klass.instantiate
108
+ assert_equal klass, instance.class
109
+ end
110
+
111
+ def test_should_instantiate_class_which_defines_its_own_new_method_with_multiple_parameters
112
+ klass = Class.new do
113
+ class << self
114
+ alias_method :__new__, :new
115
+ def new(parameter_one, parameter_two, parameter_three, parameter_four)
116
+ __new__
117
+ end
118
+ end
119
+ end
120
+ instance = klass.instantiate
121
+ assert_equal klass, instance.class
122
+ end
123
+
124
+ def test_should_instantiate_class_which_defines_its_own_new_method_with_only_optional_parameters
125
+ klass = Class.new do
126
+ class << self
127
+ alias_method :__new__, :new
128
+ def new(*optional_parameters)
129
+ __new__
130
+ end
131
+ end
132
+ end
133
+ instance = klass.instantiate
134
+ assert_equal klass, instance.class
135
+ end
136
+
137
+ def test_should_instantiate_class_which_defines_its_own_new_method_with_some_required_parameters_and_some_optional_parameters
138
+ klass = Class.new do
139
+ class << self
140
+ alias_method :__new__, :new
141
+ def new(parameter_one, parameter_two, *optional_parameters)
142
+ __new__
143
+ end
144
+ end
145
+ end
146
+ instance = klass.instantiate
147
+ assert_equal klass, instance.class
148
+ end
149
+
150
+ def test_should_instantiate_class_which_defines_its_own_new_method_which_calls_method_on_parameter
151
+ klass = Class.new do
152
+ class << self
153
+ alias_method :__new__, :new
154
+ def new(parameter_one)
155
+ parameter_one.foo
156
+ __new__
157
+ end
158
+ end
159
+ end
160
+ instance = klass.instantiate
161
+ assert_equal klass, instance.class
162
+ end
163
+
164
+ def test_should_instantiate_class_which_defines_its_own_new_method_which_calls_method_on_multiple_parameters
165
+ klass = Class.new do
166
+ class << self
167
+ alias_method :__new__, :new
168
+ def new(parameter_one, parameter_two, parameter_three, parameter_four)
169
+ parameter_one.foo
170
+ parameter_two.foo
171
+ parameter_three.foo
172
+ parameter_four.foo
173
+ __new__
174
+ end
175
+ end
176
+ end
177
+ instance = klass.instantiate
178
+ assert_equal klass, instance.class
179
+ end
180
+
181
+ def test_should_instantiate_class_which_defines_its_own_new_method_which_calls_method_on_result_of_calling_method_on_parameter
182
+ klass = Class.new do
183
+ class << self
184
+ alias_method :__new__, :new
185
+ def new(parameter_one)
186
+ parameter_one.foo.bar
187
+ __new__
188
+ end
189
+ end
190
+ end
191
+ instance = klass.instantiate
192
+ assert_equal klass, instance.class
193
+ end
194
+
195
+ end
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.setup(:default, :development)
4
+
5
+ require "test/unit"
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: instantiator
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
+ - James Mead
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-27 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: blankslate
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description:
36
+ email:
37
+ - james@floehopper.org
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - README.md
48
+ - Rakefile
49
+ - instantiator.gemspec
50
+ - lib/instantiator.rb
51
+ - lib/instantiator/version.rb
52
+ - test/instantiator_test.rb
53
+ - test/test_helper.rb
54
+ has_rdoc: true
55
+ homepage: ""
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project: instantiator
84
+ rubygems_version: 1.6.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Instantiate an arbitrary Ruby class
88
+ test_files:
89
+ - test/instantiator_test.rb
90
+ - test/test_helper.rb