risitor 1.1.0 → 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/LICENSE +21 -0
- data/lib/risitor/visit_method_helper.rb +3 -3
- data/lib/risitor/visitor.rb +15 -15
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48d2526732ed445769d7f9434204b28ea000a7b5
|
4
|
+
data.tar.gz: db421ebb9278b5e01e7683f46a195a26adae5303
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4fbb4de8d6edcb6d94f0b02bc38f3f69d51eddd04662686a9ee43bdb7ba6ffa994733d4c1169f80c05a8075971d0ddffb177d36ffd60d3cb317add7a1d31c597
|
7
|
+
data.tar.gz: f2d4f209b5faee9957e0917d13b9f08093f298373ae7dc2b9f6f6fdc0ba473a69fe357e901b4cf3575d57d75d92c7de4535d24649a03580d68e0d9338b202caf
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) Pierre Le Gall
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Risitor
|
2
2
|
# Helper for visit methods generation, matching & co.
|
3
3
|
module VisitMethodHelper
|
4
|
-
TEMPLATE =
|
4
|
+
TEMPLATE = 'visit[%s]'.freeze
|
5
5
|
REGEXP = /^visit\[(.*)\]$/
|
6
6
|
|
7
7
|
# Return a visit method name for the type +type+.
|
@@ -12,13 +12,13 @@ module Risitor
|
|
12
12
|
# Return true if the +visit_method_name+ is a well formed
|
13
13
|
# visit method name, else false.
|
14
14
|
def self.match(visit_method_name)
|
15
|
-
return REGEXP.match
|
15
|
+
return REGEXP.match(visit_method_name)
|
16
16
|
end
|
17
17
|
|
18
18
|
# Return the type matching a visit method.
|
19
19
|
def self.get_type(visit_method)
|
20
20
|
type_symbol = match(visit_method).captures[0]
|
21
|
-
return const_get
|
21
|
+
return const_get(type_symbol)
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
data/lib/risitor/visitor.rb
CHANGED
@@ -10,7 +10,7 @@ module Risitor
|
|
10
10
|
def visit(object, *args, as: object.class)
|
11
11
|
as.ancestors.each do |type|
|
12
12
|
visit_method = VisitMethodHelper.gen_name(type)
|
13
|
-
next unless respond_to?
|
13
|
+
next unless respond_to?(visit_method)
|
14
14
|
return send(visit_method, object, *args)
|
15
15
|
end
|
16
16
|
raise NoVisitMethodError.new(self, object, as)
|
@@ -18,13 +18,13 @@ module Risitor
|
|
18
18
|
|
19
19
|
class << self
|
20
20
|
def included(visitor)
|
21
|
-
visitor.extend
|
22
|
-
visitor.extend
|
21
|
+
visitor.extend(ClassMethods)
|
22
|
+
visitor.extend(ClassMethodsWhenIncluded)
|
23
23
|
end
|
24
24
|
|
25
25
|
def extended(visitor)
|
26
|
-
visitor.extend
|
27
|
-
visitor.extend
|
26
|
+
visitor.extend(ClassMethods)
|
27
|
+
visitor.extend(ClassMethodsWhenExtended)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -37,7 +37,7 @@ module Risitor
|
|
37
37
|
|
38
38
|
# Add/override a visit method for the types +types+.
|
39
39
|
def add_visit_method(*types, &block)
|
40
|
-
block = block.curry(1) if block.arity
|
40
|
+
block = block.curry(1) if block.arity.zero?
|
41
41
|
types.each do |type|
|
42
42
|
specialized_add_visit_method(type, &block)
|
43
43
|
end
|
@@ -60,7 +60,7 @@ module Risitor
|
|
60
60
|
# Return a list of the visit method.
|
61
61
|
def visit_methods
|
62
62
|
return methods.select do |method|
|
63
|
-
VisitMethodHelper.match
|
63
|
+
VisitMethodHelper.match(method)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -79,19 +79,19 @@ module Risitor
|
|
79
79
|
private
|
80
80
|
|
81
81
|
def specialized_alias_visit_method(visit_method_alias)
|
82
|
-
define_method
|
82
|
+
define_method(visit_method_alias, instance_method(:visit))
|
83
83
|
end
|
84
84
|
|
85
85
|
def specialized_add_visit_method(klass, &block)
|
86
|
-
define_method
|
86
|
+
define_method(VisitMethodHelper.gen_name(klass), block)
|
87
87
|
end
|
88
88
|
|
89
89
|
def specialized_remove_visit_method(klass)
|
90
|
-
remove_method
|
90
|
+
remove_method(VisitMethodHelper.gen_name(klass))
|
91
91
|
end
|
92
92
|
|
93
93
|
def specialized_remove_method(visit_method)
|
94
|
-
remove_method
|
94
|
+
remove_method(visit_method)
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -100,19 +100,19 @@ module Risitor
|
|
100
100
|
private
|
101
101
|
|
102
102
|
def specialized_alias_visit_method(visit_method_alias)
|
103
|
-
define_singleton_method
|
103
|
+
define_singleton_method(visit_method_alias, method(:visit))
|
104
104
|
end
|
105
105
|
|
106
106
|
def specialized_add_visit_method(klass, &block)
|
107
|
-
define_singleton_method
|
107
|
+
define_singleton_method(VisitMethodHelper.gen_name(klass), block)
|
108
108
|
end
|
109
109
|
|
110
110
|
def specialized_remove_visit_method(klass)
|
111
|
-
|
111
|
+
singleton_class.send(:remove_method, VisitMethodHelper.gen_name(klass))
|
112
112
|
end
|
113
113
|
|
114
114
|
def specialized_remove_method(visit_method)
|
115
|
-
|
115
|
+
singleton_class.send(:remove_method, visit_method)
|
116
116
|
end
|
117
117
|
end
|
118
118
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: risitor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pierre Le Gall
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Make the visitor pattern accessible and flexible in Ruby.
|
14
14
|
email: pierre@legall.im
|
@@ -16,6 +16,7 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
+
- LICENSE
|
19
20
|
- lib/risitor.rb
|
20
21
|
- lib/risitor/no_visit_method_error.rb
|
21
22
|
- lib/risitor/visit_method_helper.rb
|