super_module 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -87
- data/VERSION +1 -1
- data/examples/reddit-readers/banister/foo.rb +1 -2
- data/lib/super_module.rb +41 -26
- data/lib/super_module/v1.rb +37 -0
- data/lib/super_module/v1/module_body_method_call_recorder.rb +42 -0
- data/lib/super_module/v1/singleton_method_definition_store.rb +106 -0
- data/spec/lib/super_module_spec.rb +159 -145
- data/spec/support/baz.rb +1 -3
- data/spec/support/v1.rb +6 -0
- data/spec/support/v1/bar.rb +51 -0
- data/spec/support/v1/baz.rb +28 -0
- data/spec/support/v1/fake_active_model.rb +16 -0
- data/spec/support/v1/foo.rb +87 -0
- data/spec/support/v2.rb +6 -0
- data/spec/support/v2/bar.rb +52 -0
- data/spec/support/v2/baz.rb +27 -0
- data/spec/support/v2/fake_active_model.rb +14 -0
- data/spec/support/v2/foo.rb +87 -0
- data/spec/support/v2_alt.rb +6 -0
- data/spec/support/v2_alt/bar.rb +52 -0
- data/spec/support/v2_alt/baz.rb +27 -0
- data/spec/support/v2_alt/fake_active_model.rb +14 -0
- data/spec/support/v2_alt/foo.rb +87 -0
- data/super_module.gemspec +21 -6
- metadata +20 -5
- data/lib/super_module/module_body_method_call_recorder.rb +0 -40
- data/lib/super_module/singleton_method_definition_store.rb +0 -102
- data/spec/support/support.rb +0 -4
data/super_module.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: super_module 1.
|
5
|
+
# stub: super_module 1.2.0 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "super_module"
|
9
|
-
s.version = "1.
|
9
|
+
s.version = "1.2.0"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Andy Maleh"]
|
14
|
-
s.date = "
|
14
|
+
s.date = "2016-05-03"
|
15
15
|
s.description = "SuperModule allows defining class methods and method invocations the same way a super class does without using def included(base). This also succeeds ActiveSupport::Concern by offering lighter syntax"
|
16
16
|
s.extra_rdoc_files = [
|
17
17
|
"LICENSE.txt",
|
@@ -26,15 +26,30 @@ Gem::Specification.new do |s|
|
|
26
26
|
"examples/reddit-readers/banister/foo.rb",
|
27
27
|
"examples/reddit-readers/banister/world.rb",
|
28
28
|
"lib/super_module.rb",
|
29
|
-
"lib/super_module/
|
30
|
-
"lib/super_module/
|
29
|
+
"lib/super_module/v1.rb",
|
30
|
+
"lib/super_module/v1/module_body_method_call_recorder.rb",
|
31
|
+
"lib/super_module/v1/singleton_method_definition_store.rb",
|
31
32
|
"ruby187.Gemfile",
|
32
33
|
"spec/lib/super_module_spec.rb",
|
33
34
|
"spec/support/bar.rb",
|
34
35
|
"spec/support/baz.rb",
|
35
36
|
"spec/support/fake_active_model.rb",
|
36
37
|
"spec/support/foo.rb",
|
37
|
-
"spec/support/
|
38
|
+
"spec/support/v1.rb",
|
39
|
+
"spec/support/v1/bar.rb",
|
40
|
+
"spec/support/v1/baz.rb",
|
41
|
+
"spec/support/v1/fake_active_model.rb",
|
42
|
+
"spec/support/v1/foo.rb",
|
43
|
+
"spec/support/v2.rb",
|
44
|
+
"spec/support/v2/bar.rb",
|
45
|
+
"spec/support/v2/baz.rb",
|
46
|
+
"spec/support/v2/fake_active_model.rb",
|
47
|
+
"spec/support/v2/foo.rb",
|
48
|
+
"spec/support/v2_alt.rb",
|
49
|
+
"spec/support/v2_alt/bar.rb",
|
50
|
+
"spec/support/v2_alt/baz.rb",
|
51
|
+
"spec/support/v2_alt/fake_active_model.rb",
|
52
|
+
"spec/support/v2_alt/foo.rb",
|
38
53
|
"super_module.gemspec"
|
39
54
|
]
|
40
55
|
s.homepage = "http://github.com/AndyObtiva/super_module"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: super_module
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Maleh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-05-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: method_source
|
@@ -84,15 +84,30 @@ files:
|
|
84
84
|
- examples/reddit-readers/banister/foo.rb
|
85
85
|
- examples/reddit-readers/banister/world.rb
|
86
86
|
- lib/super_module.rb
|
87
|
-
- lib/super_module/
|
88
|
-
- lib/super_module/
|
87
|
+
- lib/super_module/v1.rb
|
88
|
+
- lib/super_module/v1/module_body_method_call_recorder.rb
|
89
|
+
- lib/super_module/v1/singleton_method_definition_store.rb
|
89
90
|
- ruby187.Gemfile
|
90
91
|
- spec/lib/super_module_spec.rb
|
91
92
|
- spec/support/bar.rb
|
92
93
|
- spec/support/baz.rb
|
93
94
|
- spec/support/fake_active_model.rb
|
94
95
|
- spec/support/foo.rb
|
95
|
-
- spec/support/
|
96
|
+
- spec/support/v1.rb
|
97
|
+
- spec/support/v1/bar.rb
|
98
|
+
- spec/support/v1/baz.rb
|
99
|
+
- spec/support/v1/fake_active_model.rb
|
100
|
+
- spec/support/v1/foo.rb
|
101
|
+
- spec/support/v2.rb
|
102
|
+
- spec/support/v2/bar.rb
|
103
|
+
- spec/support/v2/baz.rb
|
104
|
+
- spec/support/v2/fake_active_model.rb
|
105
|
+
- spec/support/v2/foo.rb
|
106
|
+
- spec/support/v2_alt.rb
|
107
|
+
- spec/support/v2_alt/bar.rb
|
108
|
+
- spec/support/v2_alt/baz.rb
|
109
|
+
- spec/support/v2_alt/fake_active_model.rb
|
110
|
+
- spec/support/v2_alt/foo.rb
|
96
111
|
- super_module.gemspec
|
97
112
|
homepage: http://github.com/AndyObtiva/super_module
|
98
113
|
licenses:
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module SuperModule
|
2
|
-
module ModuleBodyMethodCallRecorder
|
3
|
-
def __super_module_singleton_methods_excluded_from_call_recording
|
4
|
-
@__super_module_singleton_methods_excluded_from_call_recording ||= [
|
5
|
-
:__record_method_call,
|
6
|
-
:__method_signature
|
7
|
-
]
|
8
|
-
end
|
9
|
-
|
10
|
-
def __module_body_method_calls
|
11
|
-
@__module_body_method_calls ||= []
|
12
|
-
end
|
13
|
-
|
14
|
-
def __method_signature(method_name, args)
|
15
|
-
"#{method_name}(#{args.to_a.map(&:to_s).join(",")})"
|
16
|
-
end
|
17
|
-
|
18
|
-
#TODO handle case of a method call being passed a block (e.g. validates do custom validator end )
|
19
|
-
def __record_method_call(method_name, *args, &block)
|
20
|
-
return if self.is_a?(Class)
|
21
|
-
__module_body_method_calls << [method_name, args, block]
|
22
|
-
end
|
23
|
-
|
24
|
-
def __all_module_body_method_calls_in_definition_order
|
25
|
-
ancestor_module_body_method_calls = included_super_modules.map(&:__module_body_method_calls).flatten(1)
|
26
|
-
all_module_body_method_calls = __module_body_method_calls + ancestor_module_body_method_calls
|
27
|
-
all_module_body_method_calls.reverse
|
28
|
-
end
|
29
|
-
|
30
|
-
def __singleton_method_call_recorder(method_name, method_args)
|
31
|
-
unless __super_module_singleton_methods_excluded_from_call_recording.include?(method_name)
|
32
|
-
method_call_recorder_args = "'#{method_name}'"
|
33
|
-
method_call_recorder_args << ", #{method_args}" unless method_args.to_s.strip == ''
|
34
|
-
"self.__record_method_call(#{method_call_recorder_args})"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
|
@@ -1,102 +0,0 @@
|
|
1
|
-
require 'method_source'
|
2
|
-
require File.expand_path(File.join(File.dirname(__FILE__), 'module_body_method_call_recorder')) # backwards compatible with Ruby 1.8.7
|
3
|
-
|
4
|
-
module SuperModule
|
5
|
-
module SingletonMethodDefinitionStore
|
6
|
-
# excluded list of singleton methods to define (perhaps give a better name)
|
7
|
-
def __super_module_singleton_methods_excluded_from_base_definition
|
8
|
-
@__super_module_singleton_methods_excluded_from_base_definition ||= [
|
9
|
-
:__all_module_body_method_calls_in_definition_order,
|
10
|
-
:__build_singleton_method_body_source,
|
11
|
-
:__define_super_module_singleton_methods,
|
12
|
-
:__invoke_module_body_method_calls,
|
13
|
-
:__module_body_method_calls,
|
14
|
-
:__overwrite_singleton_method_from_current_super_module,
|
15
|
-
:__singleton_method_args,
|
16
|
-
:__singleton_method_body,
|
17
|
-
:__singleton_method_body_for,
|
18
|
-
:__singleton_method_call_recorder,
|
19
|
-
:__singleton_method_definition_regex,
|
20
|
-
:__super_module_having_method,
|
21
|
-
:__super_module_singleton_methods,
|
22
|
-
:__super_module_singleton_methods_excluded_from_base_definition,
|
23
|
-
:__super_module_singleton_methods_excluded_from_call_recording,
|
24
|
-
:class_eval,
|
25
|
-
:dbg_print, #debugger library friendly exclusion
|
26
|
-
:dbg_puts, #debugger library friendly exclusion
|
27
|
-
:included,
|
28
|
-
:included_super_modules,
|
29
|
-
:singleton_method_added
|
30
|
-
]
|
31
|
-
end
|
32
|
-
|
33
|
-
def __super_module_singleton_methods
|
34
|
-
@__super_module_singleton_methods ||= []
|
35
|
-
end
|
36
|
-
|
37
|
-
def __singleton_method_body_for(super_module, method_name)
|
38
|
-
super_module.__super_module_singleton_methods.detect {|sm_method_name, sm_method_body| sm_method_name == method_name}[1]
|
39
|
-
end
|
40
|
-
|
41
|
-
def included_super_modules
|
42
|
-
included_modules.select {|m| m.include?(SuperModule)}
|
43
|
-
end
|
44
|
-
|
45
|
-
def __all_methods(object)
|
46
|
-
object.public_methods + object.protected_methods + object.private_methods
|
47
|
-
end
|
48
|
-
|
49
|
-
def __super_module_having_method(method_name)
|
50
|
-
included_super_modules.detect {|included_super_module| __all_methods(included_super_module).map(&:to_s).include?(method_name.to_s)}
|
51
|
-
end
|
52
|
-
|
53
|
-
def __singleton_method_definition_regex(method_name)
|
54
|
-
/(public|protected|private)?(send)?[ \t(:"']*def(ine_method)?[ \t,:"']+(self\.)?#{method_name}\)?[ \tdo{(|]*([^\n)|;]*)?[ \t)|;]*/m
|
55
|
-
end
|
56
|
-
|
57
|
-
def __singleton_method_args(method_name, method_body)
|
58
|
-
method_arg_match = method_body.match(__singleton_method_definition_regex(method_name)).to_a[5]
|
59
|
-
end
|
60
|
-
|
61
|
-
def __singleton_method_access_level(method_name)
|
62
|
-
%w(private protected public).detect do |method_access|
|
63
|
-
method_group = "#{method_access}_methods"
|
64
|
-
send(method_group).map(&:to_s).include?(method_name.to_s)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def __build_singleton_method_body_source(method_name)
|
69
|
-
method_body = self.method(method_name).source
|
70
|
-
method_args = __singleton_method_args(method_name, method_body)
|
71
|
-
method_body = "def #{method_name}\n#{method_body}\nend" if method_args.nil?
|
72
|
-
class_self_method_def_enclosure = "class << self\n#{__singleton_method_access_level(method_name)}\ndef #{method_name}(#{method_args})\n#{__singleton_method_call_recorder(method_name, method_args)}\n"
|
73
|
-
method_body.sub(__singleton_method_definition_regex(method_name), class_self_method_def_enclosure) + "\nend\n"
|
74
|
-
end
|
75
|
-
|
76
|
-
def __singleton_method_body(method_name)
|
77
|
-
super_module_having_method = __super_module_having_method(method_name)
|
78
|
-
super_module_having_method ? __singleton_method_body_for(super_module_having_method, method_name) : __build_singleton_method_body_source(method_name)
|
79
|
-
end
|
80
|
-
|
81
|
-
def __overwrite_singleton_method_from_current_super_module(method_name, method_body)
|
82
|
-
if __super_module_having_method(method_name).nil?
|
83
|
-
__super_module_singleton_methods_excluded_from_base_definition << method_name
|
84
|
-
class_eval(method_body)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def singleton_method_added(method_name)
|
89
|
-
unless __super_module_singleton_methods_excluded_from_base_definition.include?(method_name)
|
90
|
-
method_body = __singleton_method_body(method_name)
|
91
|
-
__super_module_singleton_methods << [method_name, method_body]
|
92
|
-
__overwrite_singleton_method_from_current_super_module(method_name, method_body)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def self.extended(base)
|
97
|
-
base.extend(SuperModule::ModuleBodyMethodCallRecorder) unless base.respond_to?(:__record_method_call)
|
98
|
-
base.singleton_method_added(:__method_signature)
|
99
|
-
base.singleton_method_added(:__record_method_call)
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
data/spec/support/support.rb
DELETED