missingly 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/missingly/array_matcher.rb +4 -0
- data/lib/missingly/matchers.rb +45 -2
- data/lib/missingly/regex_matcher.rb +4 -0
- data/lib/missingly/version.rb +1 -1
- data/spec/class_inheritance_spec.rb +21 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 242b35ec57e5447c85e8ae0e22a9aac429e7eda5
|
4
|
+
data.tar.gz: c866e36784eada8f7b9fac4c598045fd5391b78c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c74940512b88e2413720f84eeef11b59c5d39c0885fc8b87d175b26f20a2ff8b4b2e469caba81624b01187b72fe4715e3b62c0901a5d2777578fb99248e4ede4
|
7
|
+
data.tar.gz: a7da137a6fa783261daf4411d63012057baf08d304b78fda09a3d70b14e5a8c552d1c3f7b4ed764b30dcb8f1040607ff8116132658affa6088b8db40e27cb8c8
|
data/lib/missingly/matchers.rb
CHANGED
@@ -1,26 +1,59 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
1
3
|
module Missingly
|
4
|
+
module Mutex
|
5
|
+
@mutex = ::Mutex.new
|
6
|
+
|
7
|
+
def self.synchronize(&block)
|
8
|
+
@mutex.synchronize(&block)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
2
12
|
module Matchers
|
3
13
|
module ClassMethods
|
4
14
|
def handle_missingly(regular_expression_or_array, options={}, &block)
|
15
|
+
undef_parent_missingly_methods regular_expression_or_array
|
16
|
+
|
5
17
|
case regular_expression_or_array
|
6
18
|
when Array then missingly_matchers[regular_expression_or_array] = ArrayMatcher.new(regular_expression_or_array, options, block)
|
7
19
|
when Regexp then missingly_matchers[regular_expression_or_array] = RegexMatcher.new(regular_expression_or_array, options, block)
|
8
20
|
end
|
9
21
|
end
|
10
22
|
|
23
|
+
def undef_parent_missingly_methods(matcher)
|
24
|
+
return unless superclass.respond_to?(:missingly_methods_for_matcher)
|
25
|
+
|
26
|
+
superclass.missingly_methods_for_matcher(matcher).each do |method|
|
27
|
+
undef_method method
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def missingly_subclasses
|
32
|
+
@missingly_subclasses ||= []
|
33
|
+
end
|
34
|
+
|
11
35
|
def missingly_matchers
|
12
36
|
@missingly_matchers ||= {}
|
13
37
|
end
|
14
38
|
|
39
|
+
def missingly_methods
|
40
|
+
@missingly_methods ||= Hash.new()
|
41
|
+
end
|
42
|
+
|
43
|
+
def missingly_methods_for_matcher(matcher)
|
44
|
+
missingly_methods[matcher] ||= []
|
45
|
+
end
|
46
|
+
|
15
47
|
def _define_method(*args, &block)
|
16
48
|
define_method(*args, &block)
|
17
49
|
end
|
18
50
|
|
19
51
|
def inherited(subclass)
|
20
52
|
matchers = self.missingly_matchers
|
21
|
-
subclass.
|
53
|
+
subclass.class_eval do
|
22
54
|
@missingly_matchers = matchers.clone
|
23
55
|
end
|
56
|
+
missingly_subclasses << subclass
|
24
57
|
end
|
25
58
|
end
|
26
59
|
|
@@ -36,7 +69,17 @@ module Missingly
|
|
36
69
|
self.class.missingly_matchers.values.each do |matcher|
|
37
70
|
next unless matcher.should_respond_to?(method_name)
|
38
71
|
|
39
|
-
|
72
|
+
Missingly::Mutex.synchronize do
|
73
|
+
self.class.missingly_methods_for_matcher(matcher.matchable) << method_name
|
74
|
+
|
75
|
+
returned_value = matcher.handle(self, method_name, *args, &block)
|
76
|
+
|
77
|
+
self.class.missingly_subclasses.each do |subclass|
|
78
|
+
subclass.undef_parent_missingly_methods matcher.matchable
|
79
|
+
end
|
80
|
+
|
81
|
+
return returned_value
|
82
|
+
end
|
40
83
|
end
|
41
84
|
super
|
42
85
|
end
|
data/lib/missingly/version.rb
CHANGED
@@ -16,18 +16,33 @@ describe Missingly::Matchers do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
it "should work
|
19
|
+
it "should work when called before parent" do
|
20
20
|
b = subclass.new
|
21
21
|
b.foo.should eq :foo
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
:
|
24
|
+
describe "overriding methods" do
|
25
|
+
let(:subclass_with_overrides) do
|
26
|
+
Class.new(super_class) do
|
27
|
+
handle_missingly [:foo] do |method|
|
28
|
+
:super_duper
|
29
|
+
end
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
31
|
-
|
33
|
+
it "should work when called before parent" do
|
34
|
+
subclass_with_overrides.new.foo.should eq :super_duper
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should work when called after parent" do
|
38
|
+
super_class.new.foo
|
39
|
+
subclass_with_overrides.new.foo.should eq :super_duper
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should work when subclass initiated before parent method defined" do
|
43
|
+
subclass_with_overrides
|
44
|
+
super_class.new.foo
|
45
|
+
subclass_with_overrides.new.foo.should eq :super_duper
|
46
|
+
end
|
32
47
|
end
|
33
48
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: missingly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thijs de Vries
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|