upcastable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at takeshi.arabiki@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 abicky
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.
@@ -0,0 +1,92 @@
1
+ # Upcastable [![Build Status](https://travis-ci.org/abicky/upcastable.svg?branch=master)](https://travis-ci.org/abicky/upcastable)
2
+
3
+ Upcastable provides the feature to emulate upcasting in Ruby.
4
+
5
+ Duck typing sometimes results in `NoMethodError` unexpectedly by calling methods some classes don't have even if the code pass a test using other classes which have the methods. We can avoid such situations by upcasting. All we have to do is implementing methods defined in the super class or module and we don't have to care about whether or not methods defined only in some subclasses are called.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'upcastable'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install upcastable
23
+
24
+ ## Usage
25
+
26
+ We define `Animal` module and `Cat` class to explain the usage like below:
27
+
28
+ ```ruby
29
+ module Animal
30
+ def talk; end
31
+ end
32
+
33
+ class Cat
34
+ include Animal
35
+
36
+ def talk
37
+ 'Meow!'
38
+ end
39
+
40
+ def run
41
+ 'Running...'
42
+ end
43
+ end
44
+ ```
45
+
46
+ Upcastable defines `Object#upcast_to` and you can use `Animal` like a interface by upcasting `Cat` object to `Animal`.
47
+
48
+ ```ruby
49
+ cat = Cat.new
50
+ animal = cat.upcast_to(Animal)
51
+ animal.class #=> Cat
52
+ animal.talk #=> "Meow!"
53
+ animal.run #=> NoMethodError: `run' is not defined in Animal
54
+ ```
55
+
56
+ Internally, the upcasted object is a `Upcastable::UpcastedObject` instance which delegates almost all methods to the original object. Therefore the value of `#object_id` is not changed.
57
+
58
+ ```ruby
59
+ cat.object_id == animal.object_id #=> true
60
+ ```
61
+
62
+
63
+ You can also downcast the upcasted object to the original class by calling `Object#downcast`.
64
+
65
+ ```ruby
66
+ animal.downcast.run #=> "Running..."
67
+ ```
68
+
69
+ There are two additional methods, `Object#upcasting` and `Object#upcasted?`. `Object#upcasting` returns the ancestor to which the object have been upcasted and `Object#upcasted?` returns `true` if the object is upcasted.
70
+
71
+ ```ruby
72
+ cat.upcasted? #=> false
73
+ cat.upcasting #=> nil
74
+ animal.upcasted? #=> true
75
+ animal.upcasting #=> Animal
76
+ ```
77
+
78
+
79
+ ## Development
80
+
81
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
82
+
83
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
84
+
85
+ ## Contributing
86
+
87
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abicky/upcastable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
88
+
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :benchmark do
9
+ ruby 'benchmark/upcasting.rb'
10
+ end
@@ -0,0 +1,35 @@
1
+ require 'upcastable'
2
+ require 'benchmark/ips'
3
+
4
+ module Animal
5
+ def talk; end
6
+ end
7
+
8
+ class Cat
9
+ include Animal
10
+
11
+ def talk
12
+ 'Meow!'
13
+ end
14
+
15
+ def run
16
+ 'Running...'
17
+ end
18
+ end
19
+
20
+ cat = Cat.new
21
+ animal = cat.upcast_to(Animal)
22
+
23
+ puts 'Object instance methods (#object_id)'
24
+ Benchmark.ips do |x|
25
+ x.report('baseline') { cat.object_id }
26
+ x.report('upcasting') { animal.object_id }
27
+ x.compare!
28
+ end
29
+
30
+ puts 'Animal instance methods (#talk)'
31
+ Benchmark.ips do |x|
32
+ x.report('baseline') { cat.talk }
33
+ x.report('upcasting') { animal.talk }
34
+ x.compare!
35
+ end
@@ -0,0 +1,30 @@
1
+ module Animal
2
+ def talk; end
3
+ end
4
+
5
+ class Cat
6
+ include Animal
7
+
8
+ def talk
9
+ 'Meow!'
10
+ end
11
+
12
+ def run
13
+ 'Running...'
14
+ end
15
+ end
16
+
17
+ cat = Cat.new
18
+ cat.class #=> Cat
19
+ cat.upcasted? #=> false
20
+ cat.upcasting #=> nil
21
+ cat.talk #=> "Meow!"
22
+ cat.run #=> "Running..."
23
+
24
+ animal = cat.upcast_to(Animal)
25
+ animal.class #=> Cat
26
+ animal.upcasted? #=> true
27
+ animal.upcasting #=> Animal
28
+ animal.talk #=> "Meow!"
29
+ animal.run #=> NoMethodError: `run' is not defined in Animal
30
+ animal.downcast.run #=> "Running..."
@@ -0,0 +1,9 @@
1
+ require 'upcastable/version'
2
+ require 'upcastable/upcasted_object'
3
+ require 'upcastable/hooks'
4
+ require 'upcastable/core_ext/object/upcasting'
5
+ require 'upcastable/core_ext/basic_object/upcasting'
6
+ require 'upcastable/core_ext/kernel/upcasting'
7
+
8
+ module Upcastable
9
+ end
@@ -0,0 +1,3 @@
1
+ class BasicObject
2
+ extend ::Upcastable::Hooks
3
+ end
@@ -0,0 +1,3 @@
1
+ module Kernel
2
+ extend ::Upcastable::Hooks
3
+ end
@@ -0,0 +1,56 @@
1
+ class Object
2
+
3
+ # Upcasts the object to the specified class or module.
4
+ # Internally, the upcasted object is a Upcastable::UpcastedObject instance which
5
+ # delegates almost all methods to the original object.
6
+ #
7
+ # @param [Class, Module] ancestor the ancestor to which the object is upcasted
8
+ # @return the upcasted object
9
+ # @raise [ArgumentError] if +ancestor+ is not an ancestor
10
+ #
11
+ # @example
12
+ #
13
+ # module Animal
14
+ # def talk; end
15
+ # end
16
+ #
17
+ # class Cat
18
+ # include Animal
19
+ #
20
+ # def talk
21
+ # 'Meow!'
22
+ # end
23
+ #
24
+ # def run
25
+ # 'Running...'
26
+ # end
27
+ # end
28
+ #
29
+ # animal = Cat.new.upcast_to(Animal)
30
+ # animal.class #=> Cat
31
+ # animal.talk #=> "Meow!"
32
+ # animal.run #=> NoMethodError: `run' is not defined in Animal
33
+ #
34
+ def upcast_to(ancestor)
35
+ ::Upcastable::UpcastedObject.new(self, ancestor)
36
+ end
37
+
38
+ # @return [Class, Module, nil] the ancestor to which the object have been upcasted
39
+ def upcasting
40
+ nil
41
+ end
42
+
43
+ # Returns true if the object is upcasted
44
+ #
45
+ # @return [true, false]
46
+ def upcasted?
47
+ false
48
+ end
49
+
50
+ # Downcasts the object to the original class
51
+ #
52
+ # @return the downcasted object (the original object)
53
+ def downcast
54
+ self
55
+ end
56
+ end
@@ -0,0 +1,23 @@
1
+ module Upcastable
2
+ module Hooks
3
+ def self.extended(base)
4
+ if base < Object
5
+ raise ArgumentError, 'extended class or module must be an ancestor of Object'
6
+ end
7
+
8
+ base.class_eval do
9
+ class << self
10
+ alias_method :__upcastable_orig_method_added, :method_added
11
+
12
+ private
13
+
14
+ def method_added(name)
15
+ return unless Object <= self
16
+ ::Upcastable::UpcastedObject.define_delegate_method(name)
17
+ __upcastable_orig_method_added(name)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,64 @@
1
+ module Upcastable
2
+ class UpcastedObject
3
+ def self.define_delegate_method(m)
4
+ define_method(m) do |*args, &block|
5
+ @object.send(m, *args, &block)
6
+ end
7
+ end
8
+
9
+ def initialize(object, ancestor, base = object.class)
10
+ unless base <= ancestor
11
+ raise ArgumentError, "#{ancestor} is not an ancestor of #{base}"
12
+ end
13
+ @object = object
14
+ @ancestor = ancestor
15
+ end
16
+
17
+ (instance_methods - [:public_send, :clone]).each do |m|
18
+ define_delegate_method(m)
19
+ end
20
+
21
+ def initialize_clone(other)
22
+ @object = @object.clone
23
+ end
24
+
25
+ def send(m, *args, &block)
26
+ unless @ancestor.method_defined?(m) && @ancestor.private_method_defined?(m)
27
+ raise NoMethodError, "`#{m}' is not defined in #{@ancestor}"
28
+ end
29
+ @object.send(m, *args, &block)
30
+ end
31
+
32
+ def respond_to?(m, private = false)
33
+ if private
34
+ @ancestor.private_method_defined?(m) || @ancestor.method_defined?(m)
35
+ else
36
+ @ancestor.method_defined?(m)
37
+ end
38
+ end
39
+
40
+ def upcast_to(ancestor)
41
+ return self if ancestor == @ancestor
42
+ UpcastedObject.new(@object, ancestor, @ancestor)
43
+ end
44
+
45
+ def upcasting
46
+ @ancestor
47
+ end
48
+
49
+ def upcasted?
50
+ true
51
+ end
52
+
53
+ def downcast
54
+ @object
55
+ end
56
+
57
+ def method_missing(m, *args, &block)
58
+ unless @ancestor.method_defined?(m)
59
+ raise NoMethodError, "`#{m}' is not defined in #{@ancestor}"
60
+ end
61
+ @object.send(m, *args, &block)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module Upcastable
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'upcastable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'upcastable'
8
+ spec.version = Upcastable::VERSION
9
+ spec.authors = ['abicky']
10
+ spec.email = ['takeshi.arabiki@gmail.com']
11
+
12
+ spec.summary = %q{Upcastable provides the feature to emulate upcasting in Ruby}
13
+ spec.description = <<-DESC
14
+ Duck typing sometimes results in `NoMethodError` unexpectedly by calling methods
15
+ some classes don't have even if the code pass a test using other classes which
16
+ have the methods. We can avoid such situations by upcasting.
17
+ All we have to do is implementing methods defined in the super class or module
18
+ and we don't have to care about whether or not methods defined only in some
19
+ subclasses are called.
20
+ DESC
21
+ spec.homepage = 'https://github.com/abicky/upcastable'
22
+ spec.license = 'MIT'
23
+ spec.required_ruby_version = '>= 1.9'
24
+
25
+ all_files = `git ls-files -z`.split("\x0")
26
+ spec.files = all_files.reject { |f| f.match(%r{^\.|(bin|spec)/}) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.11'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'pry', '~> 0.10'
33
+ spec.add_development_dependency 'pry-doc', '~> 0.8'
34
+ spec.add_development_dependency 'benchmark-ips', '~> 2.3'
35
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upcastable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - abicky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-01-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.11'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.11'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '10.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.10'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.10'
78
+ - !ruby/object:Gem::Dependency
79
+ name: pry-doc
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '0.8'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '0.8'
94
+ - !ruby/object:Gem::Dependency
95
+ name: benchmark-ips
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '2.3'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '2.3'
110
+ description: ! 'Duck typing sometimes results in `NoMethodError` unexpectedly by calling
111
+ methods
112
+
113
+ some classes don''t have even if the code pass a test using other classes which
114
+
115
+ have the methods. We can avoid such situations by upcasting.
116
+
117
+ All we have to do is implementing methods defined in the super class or module
118
+
119
+ and we don''t have to care about whether or not methods defined only in some
120
+
121
+ subclasses are called.
122
+
123
+ '
124
+ email:
125
+ - takeshi.arabiki@gmail.com
126
+ executables: []
127
+ extensions: []
128
+ extra_rdoc_files: []
129
+ files:
130
+ - CODE_OF_CONDUCT.md
131
+ - Gemfile
132
+ - LICENSE.txt
133
+ - README.md
134
+ - Rakefile
135
+ - benchmark/upcasting.rb
136
+ - examples/upcasting.rb
137
+ - lib/upcastable.rb
138
+ - lib/upcastable/core_ext/basic_object/upcasting.rb
139
+ - lib/upcastable/core_ext/kernel/upcasting.rb
140
+ - lib/upcastable/core_ext/object/upcasting.rb
141
+ - lib/upcastable/hooks.rb
142
+ - lib/upcastable/upcasted_object.rb
143
+ - lib/upcastable/version.rb
144
+ - upcastable.gemspec
145
+ homepage: https://github.com/abicky/upcastable
146
+ licenses:
147
+ - MIT
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '1.9'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ none: false
160
+ requirements:
161
+ - - ! '>='
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 1.8.23.2
167
+ signing_key:
168
+ specification_version: 3
169
+ summary: Upcastable provides the feature to emulate upcasting in Ruby
170
+ test_files: []
171
+ has_rdoc: