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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e73ef19743d5de0709594ea3d67b44efd9133b1b
4
- data.tar.gz: 2222043ca754451ccce39090da828b563222cbbe
3
+ metadata.gz: 242b35ec57e5447c85e8ae0e22a9aac429e7eda5
4
+ data.tar.gz: c866e36784eada8f7b9fac4c598045fd5391b78c
5
5
  SHA512:
6
- metadata.gz: e07f530971e2cb1dab180932d9d2c4e9ce00d367d0816c73ab03cbc15f066e439e42c056aba4a482495b04fb7c02ce2181c1fb222c5bc1cc3fcb29885a9593f2
7
- data.tar.gz: 06e156883e936d8fe6963c71e9f40aea08ff3f2ea476b1cc7bb7786ef21e2e6d2695bedba308e72f2f64c6478b56443249bb3c0ad85d0c7cbe8f6385a3f26abd
6
+ metadata.gz: c74940512b88e2413720f84eeef11b59c5d39c0885fc8b87d175b26f20a2ff8b4b2e469caba81624b01187b72fe4715e3b62c0901a5d2777578fb99248e4ede4
7
+ data.tar.gz: a7da137a6fa783261daf4411d63012057baf08d304b78fda09a3d70b14e5a8c552d1c3f7b4ed764b30dcb8f1040607ff8116132658affa6088b8db40e27cb8c8
@@ -6,6 +6,10 @@ module Missingly
6
6
  @array, @options, @method_block = array, options, method_block
7
7
  end
8
8
 
9
+ def matchable
10
+ array
11
+ end
12
+
9
13
  def should_respond_to?(name)
10
14
  array.include?(name)
11
15
  end
@@ -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.module_eval do
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
- return matcher.handle(self, method_name, *args, &block)
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
@@ -6,6 +6,10 @@ module Missingly
6
6
  @regex, @options, @method_block = regex, options, method_block
7
7
  end
8
8
 
9
+ def matchable
10
+ regex
11
+ end
12
+
9
13
  def should_respond_to?(name)
10
14
  regex.match(name)
11
15
  end
@@ -1,3 +1,3 @@
1
1
  module Missingly
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -16,18 +16,33 @@ describe Missingly::Matchers do
16
16
  end
17
17
  end
18
18
 
19
- it "should work with an inherited class" do
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
- it "should allow override of parent class" do
25
- subclass.module_eval do
26
- handle_missingly [:foo] do |method|
27
- :super_duper
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
- subclass.new.foo.should eq :super_duper
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.3
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-16 00:00:00.000000000 Z
11
+ date: 2013-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler