instantiator 0.0.2 → 0.0.3
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/README.md +2 -0
- data/lib/instantiator.rb +22 -3
- data/lib/instantiator/version.rb +1 -1
- data/test/instantiator_test.rb +5 -0
- data/test/method_invocation_sink_test.rb +27 -0
- metadata +5 -3
data/README.md
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
I want to be able to instantiate an arbitrary Ruby class without knowing anything about the constructor.
|
4
4
|
|
5
|
+
The initial requirement for this has come from my [Introspection](https://github.com/floehopper/introspection) project.
|
6
|
+
|
5
7
|
### Usage
|
6
8
|
|
7
9
|
class ArbitraryClass
|
data/lib/instantiator.rb
CHANGED
@@ -3,7 +3,15 @@ require "blankslate"
|
|
3
3
|
|
4
4
|
module Instantiator
|
5
5
|
|
6
|
-
|
6
|
+
UNSUPPORTED_NAMESPACES = %w(Introspection OptionParser::Switch URI Net).freeze
|
7
|
+
UNSUPPORTED_CLASSES = %w(Introspection OptionParser::Switch Gem::Installer Gem::Package::TarInput Zlib::GzipReader Zlib::GzipWriter Zlib::GzipFile Zlib::ZStream Bundler::Dependency Bundler::Definition Digest::Base Binding UnboundMethod Method Proc Process::Status Dir File::Stat MatchData Struct Bignum Float Fixnum Integer Continuation Thread NameError::message SignalException FalseClass TrueClass Data Symbol NilClass Socket UNIXServer UNIXSocket TCPServer TCPSocket UDPSocket IPSocket BasicSocket Trying).freeze
|
8
|
+
UNSUPPORTED_REGEX = Regexp.new((UNSUPPORTED_NAMESPACES.map { |ns| "^#{ns}::" } + UNSUPPORTED_CLASSES.map { |c| "^#{c}$" }).join("|")).freeze
|
9
|
+
|
10
|
+
def self.unsupported_class?(klass)
|
11
|
+
klass.to_s[UNSUPPORTED_REGEX]
|
12
|
+
end
|
13
|
+
|
14
|
+
class Error < StandardError; end
|
7
15
|
|
8
16
|
class MethodInvocationSink < ::BlankSlate
|
9
17
|
def method_missing(method_name, *args, &block)
|
@@ -12,10 +20,22 @@ module Instantiator
|
|
12
20
|
def to_str
|
13
21
|
String.new
|
14
22
|
end
|
23
|
+
def to_int
|
24
|
+
Integer(0)
|
25
|
+
end
|
26
|
+
def to_ary
|
27
|
+
Array.new
|
28
|
+
end
|
29
|
+
def to_hash
|
30
|
+
Hash.new
|
31
|
+
end
|
15
32
|
end
|
16
33
|
|
17
34
|
module ClassMethods
|
18
35
|
def instantiate
|
36
|
+
if Instantiator.unsupported_class?(self)
|
37
|
+
raise Error.new("#{self}.instantiate is not yet supported")
|
38
|
+
end
|
19
39
|
if singleton_methods(false).map(&:to_sym).include?(:new)
|
20
40
|
arity = method(:new).arity
|
21
41
|
else
|
@@ -28,10 +48,9 @@ module Instantiator
|
|
28
48
|
instance = new(*Array.new(number_of_parameters) { MethodInvocationSink.new })
|
29
49
|
return instance if instance
|
30
50
|
rescue ArgumentError => e
|
31
|
-
ensure
|
32
51
|
end
|
33
52
|
end
|
34
|
-
raise
|
53
|
+
raise Error.new("Unable to instantiate #{self}")
|
35
54
|
end
|
36
55
|
end
|
37
56
|
end
|
data/lib/instantiator/version.rb
CHANGED
data/test/instantiator_test.rb
CHANGED
@@ -197,4 +197,9 @@ class InstantiatorTest < Test::Unit::TestCase
|
|
197
197
|
instance = StringScanner.instantiate
|
198
198
|
end
|
199
199
|
|
200
|
+
def test_should_raise_exception_if_instantiating_class_is_unsupported
|
201
|
+
e = assert_raises(Instantiator::Error) { Proc.instantiate }
|
202
|
+
assert_equal "Proc.instantiate is not yet supported", e.message
|
203
|
+
end
|
204
|
+
|
200
205
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "instantiator"
|
3
|
+
|
4
|
+
class MethodInvocationSinkTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include Instantiator
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@sink = MethodInvocationSink.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_return_instance_of_string
|
13
|
+
assert_instance_of String, @sink.to_str
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_return_instance_of_fixnum
|
17
|
+
assert_instance_of Fixnum, @sink.to_int
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_return_instance_of_array
|
21
|
+
assert_instance_of Array, @sink.to_ary
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_return_instance_of_hash
|
25
|
+
assert_instance_of Hash, @sink.to_hash
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: instantiator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- James Mead
|
@@ -50,6 +50,7 @@ files:
|
|
50
50
|
- lib/instantiator.rb
|
51
51
|
- lib/instantiator/version.rb
|
52
52
|
- test/instantiator_test.rb
|
53
|
+
- test/method_invocation_sink_test.rb
|
53
54
|
- test/test_helper.rb
|
54
55
|
has_rdoc: true
|
55
56
|
homepage: ""
|
@@ -87,4 +88,5 @@ specification_version: 3
|
|
87
88
|
summary: Instantiate an arbitrary Ruby class
|
88
89
|
test_files:
|
89
90
|
- test/instantiator_test.rb
|
91
|
+
- test/method_invocation_sink_test.rb
|
90
92
|
- test/test_helper.rb
|