visitor 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/MIT-LICENCE +19 -0
- data/README.md +3 -0
- data/Rakefile +10 -0
- data/lib/visitor.rb +7 -0
- data/lib/visitor/base.rb +40 -0
- data/lib/visitor/version.rb +3 -0
- data/spec/lib/visitor/base_spec.rb +159 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/.gitkeep +0 -0
- data/visitor.gemspec +25 -0
- metadata +115 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@visitor --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENCE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (C) 2012 by Dmitriy Kiriyenko.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
data/lib/visitor.rb
ADDED
data/lib/visitor/base.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
class Visitor::Base
|
2
|
+
class << self
|
3
|
+
def visitor_for *klasses, &block
|
4
|
+
klasses.each do |klass|
|
5
|
+
define_method(:"visit_#{to_class_name(klass)}", block)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_accept_method! *args
|
10
|
+
name, options = parse_accept_args(args)
|
11
|
+
klasses = Array(options[:to] || Object)
|
12
|
+
visitor_klass = self
|
13
|
+
klasses.each do |klass|
|
14
|
+
klass.send :define_method, name do
|
15
|
+
visitor_klass.new.visit(self)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_class_name(duck)
|
21
|
+
if duck.respond_to? :name then duck.name else duck end
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_accept_args(args)
|
25
|
+
options = if args.last.is_a? Hash then args.pop else {} end
|
26
|
+
name = args.first || self.name.gsub(/Visitor$/, '').gsub(/.+([A-Z])/, "_\1").downcase
|
27
|
+
return name, options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def visit thing
|
32
|
+
thing.class.ancestors.each do |ancestor|
|
33
|
+
method_name = :"visit_#{ancestor.name}"
|
34
|
+
next unless respond_to? method_name
|
35
|
+
return send method_name, thing
|
36
|
+
end
|
37
|
+
|
38
|
+
raise "Can't handle #{thing.class}"
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,159 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Unknown
|
4
|
+
end
|
5
|
+
|
6
|
+
class TestRoot
|
7
|
+
end
|
8
|
+
|
9
|
+
module TestNesting
|
10
|
+
end
|
11
|
+
|
12
|
+
module TestMixin
|
13
|
+
end
|
14
|
+
|
15
|
+
class TestDescendant < TestRoot
|
16
|
+
end
|
17
|
+
|
18
|
+
class TestDescendantWithMixin < TestRoot
|
19
|
+
include TestMixin
|
20
|
+
end
|
21
|
+
|
22
|
+
class TestUnlistedDescendant < TestRoot
|
23
|
+
end
|
24
|
+
|
25
|
+
class TestNesting::Object < TestRoot
|
26
|
+
end
|
27
|
+
|
28
|
+
class TestNesting::UnlistedObject < TestRoot
|
29
|
+
end
|
30
|
+
|
31
|
+
class First < TestRoot
|
32
|
+
end
|
33
|
+
|
34
|
+
class Second < TestRoot
|
35
|
+
end
|
36
|
+
|
37
|
+
class TestingVisitor < Visitor::Base
|
38
|
+
add_accept_method! :test_method, :to => [First, Second]
|
39
|
+
add_accept_method! :test_method_singular, :to => TestRoot
|
40
|
+
add_accept_method! :to => String
|
41
|
+
add_accept_method! :test_default_method
|
42
|
+
|
43
|
+
visitor_for String do |s|
|
44
|
+
"It's a string"
|
45
|
+
end
|
46
|
+
|
47
|
+
visitor_for Object do |o|
|
48
|
+
"It's an object"
|
49
|
+
end
|
50
|
+
|
51
|
+
visitor_for TestRoot do |o|
|
52
|
+
"It's a test root"
|
53
|
+
end
|
54
|
+
|
55
|
+
visitor_for TestDescendant do |o|
|
56
|
+
"It's a test descendant"
|
57
|
+
end
|
58
|
+
|
59
|
+
visitor_for TestNesting::Object do |o|
|
60
|
+
"It's a test nested object"
|
61
|
+
end
|
62
|
+
|
63
|
+
visitor_for TestMixin do |o|
|
64
|
+
"It's a test mixin"
|
65
|
+
end
|
66
|
+
|
67
|
+
visitor_for First, Second do |o|
|
68
|
+
"It's one of the test pair"
|
69
|
+
end
|
70
|
+
|
71
|
+
visitor_for Fixnum do |x|
|
72
|
+
x * 2
|
73
|
+
end
|
74
|
+
|
75
|
+
visitor_for "StillUndeclaredClass" do |o|
|
76
|
+
"It's a still undeclared class"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class StillUndeclaredClass
|
81
|
+
end
|
82
|
+
|
83
|
+
describe Visitor::Base do
|
84
|
+
subject { TestingVisitor.new }
|
85
|
+
|
86
|
+
describe "#visit" do
|
87
|
+
it "should call correct method for core class" do
|
88
|
+
subject.visit("hello").should == "It's a string"
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should call correct method for object" do
|
92
|
+
subject.visit(Object.new).should == "It's an object"
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should call object method for unknown class" do
|
96
|
+
subject.visit(Unknown.new).should == "It's an object"
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should call correct method for test root class" do
|
100
|
+
subject.visit(TestRoot.new).should == "It's a test root"
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should call correct method for test descendant class" do
|
104
|
+
subject.visit(TestDescendant.new).should == "It's a test descendant"
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should call correct method for test descendant class with mixin" do
|
108
|
+
subject.visit(TestDescendantWithMixin.new).should == "It's a test mixin"
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should call parent method for test unlisted descentant class" do
|
112
|
+
subject.visit(TestUnlistedDescendant.new).should == "It's a test root"
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should call correct method for test nesting object class" do
|
116
|
+
subject.visit(TestNesting::Object.new).should == "It's a test nested object"
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should call parent method for test nesting unlisted object class" do
|
120
|
+
subject.visit(TestNesting::UnlistedObject.new).should == "It's a test root"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should call correct method for still undeclared class using string" do
|
124
|
+
subject.visit(StillUndeclaredClass.new).should == "It's a still undeclared class"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "should handle several classes in declaration" do
|
128
|
+
subject.visit(First.new).should == subject.visit(Second.new)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should receive an argument in a block" do
|
132
|
+
subject.visit(5).should == 10
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe ".add_accept_method!" do
|
137
|
+
it "should add accept method with given name to given classes" do
|
138
|
+
first, second = First.new, Second.new
|
139
|
+
first.test_method.should == subject.visit(first)
|
140
|
+
second.test_method.should == subject.visit(second)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should add accept method with given name to given class without array literal" do
|
144
|
+
object = TestRoot.new
|
145
|
+
object.test_method_singular.should == subject.visit(object)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should determine a default method name from class name" do
|
149
|
+
string = "Hello world!"
|
150
|
+
string.testing.should == subject.visit(string)
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should determine a default targets as Object" do
|
154
|
+
object = Object.new
|
155
|
+
object.test_default_method.should == subject.visit(object)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
Bundler.require(:default, :development)
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir[File.join(File.dirname(__FILE__), "support", "**", "*.rb")].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
# some (optional) config here
|
12
|
+
end
|
File without changes
|
data/visitor.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "visitor/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "visitor"
|
7
|
+
s.version = Visitor::VERSION
|
8
|
+
s.authors = ["Dmitriy Kiriyenko, Maxim Tsaplin"]
|
9
|
+
s.email = ["dmitriy.kiriyenko@anahoret.com, maxim.tsaplin@anahoret.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Just an implementation of Visitor design patter in Ruby.}
|
12
|
+
s.description = %q{Just an implementation of Visitor design patter in Ruby. As far as double dispatch is applicable for a language with dynamic typing.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "visitor"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency "pry"
|
23
|
+
s.add_development_dependency "pry-doc"
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: visitor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Dmitriy Kiriyenko, Maxim Tsaplin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &2165992680 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2165992680
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: pry
|
27
|
+
requirement: &2165992260 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2165992260
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: pry-doc
|
38
|
+
requirement: &2165991740 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2165991740
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &2165991320 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2165991320
|
58
|
+
description: Just an implementation of Visitor design patter in Ruby. As far as double
|
59
|
+
dispatch is applicable for a language with dynamic typing.
|
60
|
+
email:
|
61
|
+
- dmitriy.kiriyenko@anahoret.com, maxim.tsaplin@anahoret.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- .rspec
|
68
|
+
- .rvmrc
|
69
|
+
- .travis.yml
|
70
|
+
- Gemfile
|
71
|
+
- MIT-LICENCE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/visitor.rb
|
75
|
+
- lib/visitor/base.rb
|
76
|
+
- lib/visitor/version.rb
|
77
|
+
- spec/lib/visitor/base_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/support/.gitkeep
|
80
|
+
- visitor.gemspec
|
81
|
+
homepage: ''
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
segments:
|
94
|
+
- 0
|
95
|
+
hash: -1500006790367024403
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
hash: -1500006790367024403
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project: visitor
|
107
|
+
rubygems_version: 1.8.15
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Just an implementation of Visitor design patter in Ruby.
|
111
|
+
test_files:
|
112
|
+
- spec/lib/visitor/base_spec.rb
|
113
|
+
- spec/spec_helper.rb
|
114
|
+
- spec/support/.gitkeep
|
115
|
+
has_rdoc:
|