ov 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.
- checksums.yaml +15 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +11 -0
- data/lib/ov/exception.rb +5 -0
- data/lib/ov/ov_any.rb +4 -0
- data/lib/ov/ov_array.rb +44 -0
- data/lib/ov/ov_method.rb +50 -0
- data/lib/ov/version.rb +3 -0
- data/lib/ov.rb +110 -0
- data/ov.gemspec +24 -0
- data/samples/myarray.rb +32 -0
- data/samples/pm.rb +51 -0
- data/samples/rack.rb +47 -0
- data/samples/service.rb +36 -0
- data/spec/fixtures/fixture_classes.rb +55 -0
- data/spec/override_spec.rb +81 -0
- data/spec/spec_helper.rb +3 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YzE1ZGI3NDVhMmZjZjYzOGM0MmMxNzQyZDc5OTA2NWNjN2E2NDk1Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjI0YTcwZDQ5MWZlYmRhZjhmOGE5OTY5YTk0YjhmZTgxYWMwODczMA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZTBhZGEwMmJhNjM3NTE4ZTczMmY4ZmY3MDdjY2I1NzAxMTE2MTkzMjk2M2M5
|
10
|
+
MjllZjU3NjA0Y2Q3YzEwZWI5OGE4ODgxNDNlZDg0MDBiNzc3ZGUzZjc1ODQ5
|
11
|
+
NzgyNGU1ZWNiNDRlNTQzMjk4YTQ2YzZmY2U5M2I2MzY1NGU1ZmI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODUzZTZmOWZjMDlkNGIyMjIzODM4YzY1MzMyMjVmY2M3YTcwZTc3NjQxYjBi
|
14
|
+
MDMyYzI2NzY3N2I2MWYxMjZkZGUxZGQ2YjFmNDVlZGU2NTQ3MTEzODFiZmJm
|
15
|
+
NGIyYzhhZDQxYTNlZmI4ZDQ3ZjE0ODBkYzE4MzA4ZDhkODg1Y2M=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 fntzr
|
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,78 @@
|
|
1
|
+
# Ov
|
2
|
+
|
3
|
+
Create a multimethods in Ruby
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'ov'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install ov
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Firstly include `Ov` in you class
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class MyClass
|
25
|
+
include Ov
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
After define method with types:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class MyClass
|
33
|
+
include Ov
|
34
|
+
|
35
|
+
#with Fixnum type
|
36
|
+
let :cool_method, Fixnum do |num|
|
37
|
+
num * 100
|
38
|
+
end
|
39
|
+
|
40
|
+
#with String type
|
41
|
+
let :cool_method, String do |str|
|
42
|
+
str << "!"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
#And now
|
47
|
+
my_class = MyClass.new
|
48
|
+
my_class.cool_method(3) # => 300
|
49
|
+
my_class.cool_method("foo") # => foo!
|
50
|
+
```
|
51
|
+
|
52
|
+
Any Type
|
53
|
+
----------
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class MyClass
|
57
|
+
include Ov
|
58
|
+
let :cool_method, Any do |any|
|
59
|
+
any
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
my_class = MyClass.new
|
64
|
+
my_class.cool_method(1) => 1
|
65
|
+
my_class.cool_method("foo") => "foo"
|
66
|
+
```
|
67
|
+
|
68
|
+
Examples
|
69
|
+
--------
|
70
|
+
see [link](https://github.com/fntzr/ov/blob/master/samples/example.rb)
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/ov/exception.rb
ADDED
data/lib/ov/ov_any.rb
ADDED
data/lib/ov/ov_array.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Ov
|
2
|
+
class OA < Array
|
3
|
+
attr_accessor :complete, :result
|
4
|
+
#find in self
|
5
|
+
#find in ancestors
|
6
|
+
#find types
|
7
|
+
#find any types
|
8
|
+
def where(method)
|
9
|
+
@complete, @result = nil, nil
|
10
|
+
z = find_or_next(method) { |method|
|
11
|
+
self.find{|m| m.eql?(method) }
|
12
|
+
}.find_or_next(method) { |method|
|
13
|
+
self.find{|m| m.eql0?(method) }
|
14
|
+
}.find_or_next(method) { |method|
|
15
|
+
self.find{|m| m.like?(method) }
|
16
|
+
}.find_or_next(method) {|method|
|
17
|
+
self.find{|m| m.like0?(method) }
|
18
|
+
}.get
|
19
|
+
end
|
20
|
+
|
21
|
+
def find_or_next(method, &block)
|
22
|
+
return self if @complete
|
23
|
+
ev_block = yield(method)
|
24
|
+
@complete, @result = true, ev_block if ev_block
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
alias :get :result
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def compare(a, b)
|
33
|
+
return false if a.size != b.size
|
34
|
+
!a.zip(b).map do |arr|
|
35
|
+
first, last = arr
|
36
|
+
true if (first == Ov::Any) || (last == Ov::Any) || (first == last)
|
37
|
+
end.include?(nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
def compare0(a, b)
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/ov/ov_method.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Ov
|
2
|
+
class OverrideMethod # :nodoc:
|
3
|
+
attr_accessor :types, :body, :name, :owner, :ancestors
|
4
|
+
|
5
|
+
# +name+ (Symbol) : name for method
|
6
|
+
# +types+ (Array) : array with types, for method arguments
|
7
|
+
# +body+ (Proc) : block called with method
|
8
|
+
def initialize(name, types, owner, body = proc{})
|
9
|
+
@name, @types, @owner, @body = name, types, owner, body
|
10
|
+
@ancestors = owner.ancestors.find_all do |ancestor|
|
11
|
+
ancestor.method_defined?(:__overridable_methods) && ancestor.class == Class
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def eql?(other)
|
16
|
+
return true if @name == other.name && @types == other.types && @owner == other.owner
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
def eql0?(other)
|
21
|
+
@ancestors.find{|a|
|
22
|
+
a.__overridable_methods.find{|m|
|
23
|
+
m.name == other.name && m.types == other.types
|
24
|
+
}
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def like?(other)
|
29
|
+
return compare?(types, other.types) if @name == other.name
|
30
|
+
false
|
31
|
+
end
|
32
|
+
|
33
|
+
def like0?(other)
|
34
|
+
@ancestors.find{|a|
|
35
|
+
a.__overridable_methods.find{|m|
|
36
|
+
m.like?(other)
|
37
|
+
}
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def compare?(a, b)
|
43
|
+
return false if a.size != b.size
|
44
|
+
!a.zip(b).map { |arr|
|
45
|
+
first, last = arr
|
46
|
+
true if (first == Ov::Any) || (last == Ov::Any) || (first == last)
|
47
|
+
}.include?(nil)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/ov/version.rb
ADDED
data/lib/ov.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require "ov/version"
|
2
|
+
require "ov/ov_array"
|
3
|
+
require "ov/ov_method"
|
4
|
+
require "ov/ov_any"
|
5
|
+
require "ov/exception"
|
6
|
+
|
7
|
+
|
8
|
+
##
|
9
|
+
# `Ov` module provides functional for creating methods
|
10
|
+
# with types in Ruby.
|
11
|
+
#
|
12
|
+
#
|
13
|
+
# == Usage
|
14
|
+
#
|
15
|
+
# Firstly include `Ov` in you class
|
16
|
+
#
|
17
|
+
# class MyClass
|
18
|
+
# include Ov
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# After define method with types:
|
22
|
+
#
|
23
|
+
# class MyClass
|
24
|
+
# include Ov
|
25
|
+
#
|
26
|
+
# # with Fixnum type
|
27
|
+
# let :cool_method, Fixnum do |num|
|
28
|
+
# num * 100
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# # with String type
|
32
|
+
# let :cool_method, String do |str|
|
33
|
+
# str << "!"
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# And now
|
38
|
+
#
|
39
|
+
# my_class = MyClass.new
|
40
|
+
# my_class.cool_method(3) # => 300
|
41
|
+
# my_class.cool_method("foo") # => foo!
|
42
|
+
#
|
43
|
+
# == Any Type
|
44
|
+
#
|
45
|
+
# class MyClass
|
46
|
+
# include Ov
|
47
|
+
# let :cool_method, Any do |any|
|
48
|
+
# any
|
49
|
+
# end
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# my_class = MyClass.new
|
53
|
+
# my_class.cool_method(1) # => 1
|
54
|
+
# my_class.cool_method("foo") # => "foo"
|
55
|
+
#
|
56
|
+
# Is the same so ruby `def`
|
57
|
+
#
|
58
|
+
#
|
59
|
+
module Ov
|
60
|
+
def self.included(base) # :nodoc:
|
61
|
+
base.extend(self)
|
62
|
+
base.class_eval do
|
63
|
+
class_variable_set(:@@__overridable_methods, OA.new)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def __overridable_methods # :nodoc:
|
68
|
+
send(:class_variable_get, :@@__overridable_methods)
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Create new method with +name+ and +types+
|
73
|
+
# When method called +block+ will be executed
|
74
|
+
#
|
75
|
+
# +name+ is symbol
|
76
|
+
#
|
77
|
+
# +types+ types for method
|
78
|
+
#
|
79
|
+
def let(name, *types, &block)
|
80
|
+
|
81
|
+
__overridable_methods << OverrideMethod.new(name, types, self, block)
|
82
|
+
|
83
|
+
if !self.method_defined?(name)
|
84
|
+
self.instance_exec do
|
85
|
+
message = if self.class == Module
|
86
|
+
:define_singleton_method
|
87
|
+
else
|
88
|
+
:define_method
|
89
|
+
end
|
90
|
+
self.send(message, name) do |*args, &block|
|
91
|
+
types = *args.map(&:class)
|
92
|
+
owner = if self.class == Module
|
93
|
+
self
|
94
|
+
else
|
95
|
+
self.class
|
96
|
+
end
|
97
|
+
|
98
|
+
method = OverrideMethod.new(name, types, owner)
|
99
|
+
|
100
|
+
z = owner.__overridable_methods.where(method)
|
101
|
+
raise NotImplementError.new("Method `#{name}` in `#{self}` class with types `#{types}` not implemented.") if z.nil?
|
102
|
+
instance_exec(*args, &z.body)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
|
data/ov.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ov/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ov"
|
8
|
+
spec.version = Ov::VERSION
|
9
|
+
spec.authors = ["fntzr"]
|
10
|
+
spec.email = ["fantazuor@gmail.com"]
|
11
|
+
spec.description = %q{write overridable methods for fun :)}
|
12
|
+
spec.summary = %q{ov gem provides hacks for write polymorphic methods in Ruby.}
|
13
|
+
spec.homepage = "https://github.com/fntzr/ov"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14.0"
|
24
|
+
end
|
data/samples/myarray.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'ov'
|
2
|
+
|
3
|
+
# Array
|
4
|
+
class MyArray
|
5
|
+
include Ov
|
6
|
+
attr_accessor :arr
|
7
|
+
|
8
|
+
|
9
|
+
let :initialize do
|
10
|
+
@arr = []
|
11
|
+
end
|
12
|
+
|
13
|
+
let :initialize, Fixnum do |fx|
|
14
|
+
@arr = fx.times.map { nil }
|
15
|
+
end
|
16
|
+
|
17
|
+
let :initialize, Fixnum, Any do |fx, any|
|
18
|
+
@arr = fx.times.map { any }
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
p MyArray.new()
|
24
|
+
p MyArray.new(3)
|
25
|
+
p MyArray.new(3, true)
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
data/samples/pm.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "ov"
|
2
|
+
|
3
|
+
module Matching
|
4
|
+
def match(*args, &block)
|
5
|
+
z = Module.new do
|
6
|
+
include Ov
|
7
|
+
extend self
|
8
|
+
def try(*args, &block)
|
9
|
+
let :anon_method, *args, &block
|
10
|
+
end
|
11
|
+
def otherwise(&block)
|
12
|
+
let :otherwise, &block
|
13
|
+
end
|
14
|
+
instance_eval &block
|
15
|
+
end
|
16
|
+
begin
|
17
|
+
z.anon_method(*args)
|
18
|
+
rescue NotImplementError => e
|
19
|
+
z.otherwise
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
include Matching
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
match("String", [123]) do
|
30
|
+
try(String, Array) {|str, arr| p "#{str} #{arr}" }
|
31
|
+
try(String) {|str| p "#{str}" }
|
32
|
+
otherwise { p "none" }
|
33
|
+
end
|
34
|
+
|
35
|
+
require "net/http"
|
36
|
+
|
37
|
+
match(Net::HTTP.get_response(URI("http://google.com"))) do
|
38
|
+
try(Net::HTTPOK) {|r| p r.header }
|
39
|
+
try(Net::HTTPMovedPermanently) {|r| p r.header }
|
40
|
+
otherwise { p "error" }
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
data/samples/rack.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "ov"
|
2
|
+
require "rack"
|
3
|
+
|
4
|
+
#curl -X POST http://localhost:9292
|
5
|
+
#curl -X GET http://localhost:9292
|
6
|
+
|
7
|
+
class Post ; end
|
8
|
+
class Get ; end
|
9
|
+
|
10
|
+
class Rack::Request
|
11
|
+
def req_method
|
12
|
+
return Post.new if post?
|
13
|
+
Get.new
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class RackExample
|
18
|
+
include Ov
|
19
|
+
def call(env)
|
20
|
+
response(Rack::Request.new(env).req_method)
|
21
|
+
#[200, {"Content-Type" => "text/html"}, ["POST"]]
|
22
|
+
end
|
23
|
+
|
24
|
+
let :response, Get do |r|
|
25
|
+
# processing get
|
26
|
+
[200, {"Content-Type" => "text/html"}, ["GET"]]
|
27
|
+
end
|
28
|
+
|
29
|
+
let :response, Post do |r|
|
30
|
+
# processing post
|
31
|
+
[200, {"Content-Type" => "text/html"}, ["POST"]]
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
if __FILE__ == $0
|
37
|
+
Rack::Handler::Thin.run RackExample.new, :Port => 9292
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
data/samples/service.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'ov'
|
2
|
+
|
3
|
+
|
4
|
+
module SO
|
5
|
+
def self.get(request)
|
6
|
+
if Random.rand(10) > 6
|
7
|
+
Result.new("#{request}")
|
8
|
+
else
|
9
|
+
Fail.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class Result
|
15
|
+
attr_accessor :string
|
16
|
+
def initialize(string)
|
17
|
+
@string = string
|
18
|
+
end
|
19
|
+
|
20
|
+
alias :[] :string
|
21
|
+
end
|
22
|
+
class Fail
|
23
|
+
end
|
24
|
+
|
25
|
+
class M
|
26
|
+
include Ov
|
27
|
+
|
28
|
+
let :process, Fail do ; [] end
|
29
|
+
let :process, Result do |r| ; r[] ; end
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
m = M.new
|
34
|
+
10.times { |i|
|
35
|
+
p m.process(SO.get("#{i}"))
|
36
|
+
}
|
@@ -0,0 +1,55 @@
|
|
1
|
+
class Test
|
2
|
+
include Ov
|
3
|
+
|
4
|
+
let :my_instance_method, Array, String do |arr, str|
|
5
|
+
arr << str
|
6
|
+
arr
|
7
|
+
end
|
8
|
+
|
9
|
+
let :my_instance_method, String do |str|
|
10
|
+
str
|
11
|
+
end
|
12
|
+
|
13
|
+
let :my_instance_method, Fixnum do |fixnum|
|
14
|
+
my_instance_method([], my_instance_method("bar"))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class TestInitialize
|
19
|
+
include Ov
|
20
|
+
attr_reader :arg
|
21
|
+
let :initialize, String do |str|
|
22
|
+
str << "foo"
|
23
|
+
@arg = str
|
24
|
+
end
|
25
|
+
let :initialize, Fixnum do |fx|
|
26
|
+
@arg = fx*100
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
class TestException
|
32
|
+
include Ov
|
33
|
+
let :some_method, Fixnum do |fx|
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Test0 < Test
|
38
|
+
let :my_instance_method, String do |str|
|
39
|
+
"foo"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class TestWithoutArguments
|
44
|
+
include Ov
|
45
|
+
let :my_instance_method do
|
46
|
+
"foo"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class TestAny
|
51
|
+
include Ov
|
52
|
+
let :my_instance_method, Any do |any|
|
53
|
+
any
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ov do
|
4
|
+
context "instance methods" do
|
5
|
+
context "#my_instance_method" do
|
6
|
+
let(:test){Test.new}
|
7
|
+
|
8
|
+
it "return array" do
|
9
|
+
result = test.my_instance_method(["foo", "bar"], "baz")
|
10
|
+
result.should eq ["foo", "bar", "baz"]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "return string" do
|
14
|
+
result = test.my_instance_method("baz")
|
15
|
+
result.should eq "baz"
|
16
|
+
end
|
17
|
+
|
18
|
+
it "return overridable method" do
|
19
|
+
result = test.my_instance_method(1)
|
20
|
+
result.should eq ["bar"]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "override initialize method" do
|
26
|
+
it "return new instance with #this method" do
|
27
|
+
test = TestInitialize.new("bar")
|
28
|
+
test.arg.should eq "barfoo"
|
29
|
+
test.class.should eq TestInitialize
|
30
|
+
|
31
|
+
test0 = TestInitialize.new(1)
|
32
|
+
test0.arg.should eq 100
|
33
|
+
test0.class.should eq TestInitialize
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "exceptions" do
|
38
|
+
let(:test){TestException.new}
|
39
|
+
it "throw NotImplementError when method not defined" do
|
40
|
+
expect { test.some_method("foo") }.to raise_error(NotImplementError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "inheritance" do
|
45
|
+
let(:test){Test0.new}
|
46
|
+
it "should call parent method" do
|
47
|
+
result = test.my_instance_method(["foo", "bar"], "baz")
|
48
|
+
result.should eq ["foo", "bar", "baz"]
|
49
|
+
end
|
50
|
+
it "should call own method" do
|
51
|
+
result = test.my_instance_method("baz")
|
52
|
+
result.should eq "foo"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
context "call without argument" do
|
58
|
+
let(:test) {TestWithoutArguments.new}
|
59
|
+
it do
|
60
|
+
result = test.my_instance_method
|
61
|
+
result.should eq "foo"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "call with Any argument" do
|
66
|
+
let(:test) {TestAny.new}
|
67
|
+
it do
|
68
|
+
test.my_instance_method(1).should eq 1
|
69
|
+
test.my_instance_method("foo").should eq "foo"
|
70
|
+
test.my_instance_method(:foo).should eq :foo
|
71
|
+
end
|
72
|
+
|
73
|
+
it "exception when many arguments or without arguments" do
|
74
|
+
expect{ test.my_instance_method(1,2,3)}.to raise_error(NotImplementError)
|
75
|
+
expect{ test.my_instance_method()}.to raise_error(NotImplementError)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ov
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- fntzr
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.0
|
55
|
+
description: write overridable methods for fun :)
|
56
|
+
email:
|
57
|
+
- fantazuor@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/ov.rb
|
68
|
+
- lib/ov/exception.rb
|
69
|
+
- lib/ov/ov_any.rb
|
70
|
+
- lib/ov/ov_array.rb
|
71
|
+
- lib/ov/ov_method.rb
|
72
|
+
- lib/ov/version.rb
|
73
|
+
- ov.gemspec
|
74
|
+
- samples/myarray.rb
|
75
|
+
- samples/pm.rb
|
76
|
+
- samples/rack.rb
|
77
|
+
- samples/service.rb
|
78
|
+
- spec/fixtures/fixture_classes.rb
|
79
|
+
- spec/override_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
homepage: https://github.com/fntzr/ov
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.1.5
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: ov gem provides hacks for write polymorphic methods in Ruby.
|
105
|
+
test_files:
|
106
|
+
- spec/fixtures/fixture_classes.rb
|
107
|
+
- spec/override_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
has_rdoc:
|