interloper 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36d127b88f17a8225278ae3b5ad85bd5e2527dfa
4
- data.tar.gz: aa9558b14180c9116da68f1f043fb7cba20e1a19
3
+ metadata.gz: 5cb36dfc9e08e0e172e7ac3f0de7bd0c34f46a93
4
+ data.tar.gz: f29ca06378acd3da798d29772e2db4c2b4ce5e84
5
5
  SHA512:
6
- metadata.gz: 85dd025740b1013c4825c9142110f9812fd335dcc038a93ef38e830c727279b01132362180d35e46b4313bbd6e02d18970397147a9d5b3a26c109e1634abd713
7
- data.tar.gz: 41ab9cee231c8c7271d73c3173b56759876e7d66facbfdc918e4b26f995186407923a3d27155a375d4c3e5a4885a79aeb4d225c7c40c780314b6d51fe33e3d2c
6
+ metadata.gz: 936220a5ed55ef83bc124bf30a27bad78f6dbc9acd0e1fe4f1e3b56a5e52b0f170a6537823fb3a762257e3e084547d044cceada31d2612874c913ea3a66324c0
7
+ data.tar.gz: 1d8b52e6d03765526b8b36ea1af14176e21c1d7424b8484ecf98870f251324dba4fa2dd768e9fc9db6d7fcf338681f6d8cd78ebb5acb5640359ba55bc94db920
@@ -1,3 +1,3 @@
1
1
  module Interloper
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/interloper.rb CHANGED
@@ -3,7 +3,7 @@ require "interloper/version"
3
3
  module Interloper
4
4
 
5
5
  def interloper_module
6
- self.class.interloper_module
6
+ @interloper_module ||= self.class.interloper_module
7
7
  end
8
8
 
9
9
  def Interloper.included(base)
@@ -12,17 +12,25 @@ module Interloper
12
12
 
13
13
  module ClassMethods
14
14
 
15
- # Generates an Interloper module that is namespeced under the including
15
+ def interloper_const_name
16
+ if self.name
17
+ "#{self.name}Interloper"
18
+ else
19
+ "AnonymousInterloper#{self.object_id}"
20
+ end.to_sym
21
+ end
22
+
23
+ # Generates an Interloper module that is namespaced under the including
16
24
  # class, if one does not already exist. Then prepends the Interloper
17
25
  # module to the including class.
18
26
  # @return Module the Interloper module that was prepended to the including
19
27
  # class.
20
28
  def interloper_module
21
- # Create the Interloper module if it doesn't exist already
22
- const_set(:Interloper, generate_interloper_module) unless self.constants.include? :Interloper
23
- # Prepend the interloper module
24
- prepend const_get(:Interloper)
25
- const_get(:Interloper)
29
+ unless self.constants.include? interloper_const_name
30
+ const_set(interloper_const_name, generate_interloper_module)
31
+ prepend const_get(interloper_const_name)
32
+ end
33
+ const_get(interloper_const_name)
26
34
  end
27
35
 
28
36
  def generate_interloper_module
@@ -77,14 +85,18 @@ module Interloper
77
85
  end
78
86
  end
79
87
 
80
- def define_interloper_methods(*method_names)
88
+ def define_interloper_methods(*method_names, interloper_module_name)
81
89
  method_names.each do |method_name|
82
- define_method(method_name) do |*args, &block|
83
- called_method = __method__
84
- interloper_module.run_callbacks(:before, called_method, self, *args, &block)
85
- return_val = super(*args,&block)
86
- interloper_module.run_callbacks(:after, called_method, self, *args, &block)
87
- return_val
90
+ unless instance_methods.include? method_name
91
+ module_eval do
92
+ eval <<-CODE
93
+ def #{method_name}(*args, &block)
94
+ self.class.const_get(:#{interloper_module_name}).run_callbacks(:before, :#{method_name}, self, *args, &block)
95
+ super(*args, &block)
96
+ self.class.const_get(:#{interloper_module_name}).run_callbacks(:after, :#{method_name}, self, *args, &block)
97
+ end
98
+ CODE
99
+ end
88
100
  end
89
101
  end
90
102
  end
@@ -93,12 +105,12 @@ module Interloper
93
105
  end
94
106
 
95
107
  def before(*method_names, &callback)
96
- interloper_module.define_interloper_methods(*method_names)
108
+ interloper_module.define_interloper_methods(*method_names, interloper_const_name)
97
109
  interloper_module.add_callbacks(:before, *method_names, &callback)
98
110
  end
99
111
 
100
112
  def after(*method_names, &callback)
101
- interloper_module.define_interloper_methods(*method_names)
113
+ interloper_module.define_interloper_methods(*method_names, interloper_const_name)
102
114
  interloper_module.add_callbacks(:after, *method_names, &callback)
103
115
  end
104
116
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: interloper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Myers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-22 00:00:00.000000000 Z
11
+ date: 2017-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler