generic_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 ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in generic_visitor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gabriel Naiman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # GenericVisitor
2
+
3
+ Ruby visitor pattern implementation
4
+
5
+ Based on Aaron Patterson [post](http://blog.rubybestpractices.com/posts/aaronp/001_double_dispatch_dance.html)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'generic_visitor'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install generic_visitor
20
+
21
+ ## Usage
22
+
23
+ require 'generic_visitor'
24
+
25
+ class Country < Struct.new(:name)
26
+ def as_hash
27
+ accept AsHash.new
28
+ end
29
+ end
30
+
31
+ class City < Struct.new(:name, :country)
32
+ def as_hash
33
+ accept AsHash.new
34
+ end
35
+ end
36
+
37
+ class AsHash < GenericVisitor::Visitor
38
+ visitor_for Country do |country|
39
+ {name: country.name}
40
+ end
41
+
42
+ visitor_for City do |city|
43
+ {
44
+ name: city.name,
45
+ country: city.country.as_hash
46
+ }
47
+ end
48
+ end
49
+
50
+ city = City.new('Bs.As.', Country.new('Argentina'))
51
+ city.as_hash => {:name=>"Bs.As.", :country=>{:name=>"Argentina"}}
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/generic_visitor/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "generic_visitor"
6
+ s.version = GenericVisitor::VERSION
7
+ s.authors = ['Gabriel Naiman']
8
+ s.email = ['gabynaiman@gmail.com']
9
+ s.description = 'Ruby visitor pattern implementation'
10
+ s.summary = 'Ruby visitor pattern implementation'
11
+ s.homepage = 'https://github.com/gabynaiman/generic_visitor'
12
+
13
+ s.files = `git ls-files`.split($/)
14
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
16
+ s.require_paths = ["lib"]
17
+
18
+ s.add_development_dependency 'rspec'
19
+ end
@@ -0,0 +1,3 @@
1
+ require "generic_visitor/version"
2
+ require 'generic_visitor/visitable'
3
+ require 'generic_visitor/visitor'
@@ -0,0 +1,3 @@
1
+ module GenericVisitor
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ module GenericVisitor
2
+ module Visitable
3
+ def accept(visitor)
4
+ visitor.visit self
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,22 @@
1
+ module GenericVisitor
2
+ class Visitor
3
+
4
+ def self.visitor_for(*klasses, &block)
5
+ klasses.each do |klass|
6
+ klass.send :include, Visitable unless klass.instance_methods.include? :accept
7
+ define_method(:"visit_#{klass.name}", block)
8
+ end
9
+ end
10
+
11
+ def visit(thing)
12
+ thing.class.ancestors.each do |ancestor|
13
+ method_name = :"visit_#{ancestor.name}"
14
+ next unless respond_to? method_name
15
+ return send method_name, thing
16
+ end
17
+
18
+ raise "Can't handle #{thing.class}"
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require 'generic_visitor'
2
+
3
+ class Country < Struct.new(:name)
4
+ def as_hash
5
+ accept AsHash.new
6
+ end
7
+ end
8
+
9
+ class City < Struct.new(:name, :country)
10
+ def as_hash
11
+ accept AsHash.new
12
+ end
13
+ end
14
+
15
+ class AsHash < GenericVisitor::Visitor
16
+
17
+ visitor_for Country do |country|
18
+ {name: country.name}
19
+ end
20
+
21
+ visitor_for City do |city|
22
+ {
23
+ name: city.name,
24
+ country: city.country.as_hash
25
+ }
26
+ end
27
+
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe GenericVisitor::Visitor do
4
+
5
+ it 'Visit' do
6
+ country = Country.new('Argentina')
7
+ city = City.new('Bs.As.', country)
8
+
9
+ hash = city.as_hash
10
+
11
+ hash[:name].should eq 'Bs.As.'
12
+ hash[:country][:name].should eq 'Argentina'
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: generic_visitor
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Gabriel Naiman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: !binary |-
21
+ MA==
22
+ none: false
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: !binary |-
28
+ MA==
29
+ none: false
30
+ prerelease: false
31
+ type: :development
32
+ description: Ruby visitor pattern implementation
33
+ email:
34
+ - gabynaiman@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - generic_visitor.gemspec
45
+ - lib/generic_visitor.rb
46
+ - lib/generic_visitor/version.rb
47
+ - lib/generic_visitor/visitable.rb
48
+ - lib/generic_visitor/visitor.rb
49
+ - spec/spec_helper.rb
50
+ - spec/visitor_spec.rb
51
+ homepage: https://github.com/gabynaiman/generic_visitor
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: !binary |-
62
+ MA==
63
+ none: false
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: !binary |-
69
+ MA==
70
+ none: false
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Ruby visitor pattern implementation
77
+ test_files:
78
+ - spec/spec_helper.rb
79
+ - spec/visitor_spec.rb