explicit-return 0.0.0

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/explicit-return.rb +73 -0
  3. metadata +44 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5fd3169b94fcce6f5b0767aa224929f9474d644
4
+ data.tar.gz: 5c44af3220ddf56843cfb87154faeb28b2537a29
5
+ SHA512:
6
+ metadata.gz: d3acdf65695e79580df2120d6a23ac4f5ecae9c556c307a10dfdb639629c9e80b97e5ed8e4b7935ae9f32b08f34e7a8e25b3f1fed1cb5d0bdd167d644b9a1a99
7
+ data.tar.gz: 5168d94be13c4607e375358a5c07a586d481dd930eedfeb6fc0357eaae76d7457530854047f073110de93f30dce9d85a48564874690ec01657518167233b4fa4
@@ -0,0 +1,73 @@
1
+ module ExplicitReturn
2
+
3
+ def self.included(context)
4
+ context.extend(self)
5
+ context.extend(MethodAddedObserver)
6
+ end
7
+
8
+ def explicit(*args)
9
+ ExplicitReturn::ExplicitResult.new(*args)
10
+ end
11
+
12
+ def self.explicit(*args)
13
+ ExplicitReturn::ExplicitResult.new(*args)
14
+ end
15
+
16
+ module MethodAddedObserver
17
+
18
+ def method_added(method_name)
19
+ unless MethodWrapper.busy?
20
+ unbound_method = self.instance_method(method_name)
21
+ obj = self.allocate
22
+ unbound_method.bind(obj)
23
+ MethodWrapper.wrap_method(self, obj.method(method_name), :instance)
24
+ end
25
+ end
26
+
27
+ def singleton_method_added(method_name)
28
+ unless MethodWrapper.busy?
29
+ MethodWrapper.wrap_method(self, self.method(method_name), :singleton)
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ class ExplicitResult
36
+
37
+ attr_accessor :value
38
+
39
+ def initialize(value)
40
+ @value = value
41
+ end
42
+
43
+ end
44
+
45
+ module MethodWrapper
46
+
47
+ @@wrapping_method = false
48
+
49
+ def self.busy?
50
+ @@wrapping_method
51
+ end
52
+
53
+ def self.wrap_method(context, method, type)
54
+ proc = wrap_proc(method.to_proc)
55
+ @@wrapping_method = true
56
+ case type
57
+ when :instance
58
+ context.send(:define_method, method.name, &proc)
59
+ when :singleton
60
+ context.send(:define_singleton_method, method.name, &proc)
61
+ end
62
+ @@wrapping_method = false
63
+ end
64
+
65
+ def self.wrap_proc(proc)
66
+ proc_wrapper = Proc.new{|*args, &block|
67
+ ret = proc.call(*args, &block)
68
+ ret.value if ret.is_a?(ExplicitReturn::ExplicitResult)
69
+ }
70
+ end
71
+ end
72
+
73
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: explicit-return
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Noel Warren
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Methods will return nil unless explicitally returned
14
+ email: noelwarr@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/explicit-return.rb
20
+ homepage: https://github.com/noelwarr/explicit-return
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project: explicit-return
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Enable explicit returns for Ruby
44
+ test_files: []