visitor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@visitor --create
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in visitor.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,3 @@
1
+ [![Build Status](https://secure.travis-ci.org/dmitriy-kiriyenko/Visitor.png)](http://travis-ci.org/dmitriy-kiriyenko/Visitor)
2
+
3
+ Just an implementation of Visitor design patter in Ruby. As far as double dispatch is applicable for a language with dynamic typing.
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ task :default => :spec
8
+
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ end
@@ -0,0 +1,7 @@
1
+ require "visitor/version"
2
+
3
+ require "visitor/base"
4
+
5
+ module Visitor
6
+ # Your code goes here...
7
+ end
@@ -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,3 @@
1
+ module Visitor
2
+ VERSION = "0.0.1"
3
+ 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
@@ -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
@@ -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: