ov 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +2 -0
- data/README.md +27 -6
- data/lib/ov.rb +33 -9
- data/lib/ov/ov_array.rb +0 -14
- data/lib/ov/ov_method.rb +6 -6
- data/lib/ov/version.rb +1 -1
- data/samples/class_methods.rb +23 -0
- data/samples/myarray.rb +0 -1
- data/spec/fixtures/fixture_classes.rb +44 -1
- data/spec/override_spec.rb +60 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NmM0ZWU4ODJhZGE2NjA2ZjE4ZGU3YWQ5NWJlOGUwOWRmNWU2OWYwNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2UxYWIzMGI2ZGVmMTU4NjM4MDJiMDBiNWYyMjcyZTcxNjU2OGI3Mw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZDA1ZTBkNGQ3OWMzMTVkNDFkZWFkNGI5OTc3MGM1Mjg1ZTI2OTIyZWIwOTJk
|
10
|
+
MWNhMWExM2ZkMWNhODAxZjllZTJkNGZjOTEyZmQxOGQ1YTRjYjc1NzIwNmQ3
|
11
|
+
OTBjMjhhNzljMmI5OWFmOWE2OWEzMTVkNDBiOGQ1YjNkOTQyNmY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
OWI2OTcyOWZkZmMzM2JkMGFkMGU2M2I1ZTBkZmVmNjZjYTQ2OGE5NWJiNzY3
|
14
|
+
MWQ0NjMxNzZjMDY1ODhhYTkzYjAyODI1MTZlN2NhZDU5ZmQxNDJkYjgzZWYy
|
15
|
+
NWQ0MmM1YzVlOWIyY2U5ZTRmMThjNDM3M2Q2N2E3ZWRhZjNjNGM=
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -21,16 +21,16 @@ Or install it yourself as:
|
|
21
21
|
Firstly include `Ov` in you class
|
22
22
|
|
23
23
|
```ruby
|
24
|
-
class MyClass
|
24
|
+
class MyClass
|
25
25
|
include Ov
|
26
26
|
end
|
27
|
-
```
|
27
|
+
```
|
28
28
|
|
29
29
|
After define method with types:
|
30
30
|
|
31
|
-
```ruby
|
31
|
+
```ruby
|
32
32
|
class MyClass
|
33
|
-
include Ov
|
33
|
+
include Ov
|
34
34
|
|
35
35
|
#with Fixnum type
|
36
36
|
let :cool_method, Fixnum do |num|
|
@@ -46,13 +46,34 @@ end
|
|
46
46
|
#And now
|
47
47
|
my_class = MyClass.new
|
48
48
|
my_class.cool_method(3) # => 300
|
49
|
-
my_class.cool_method("foo") # => foo!
|
49
|
+
my_class.cool_method("foo") # => foo!
|
50
|
+
```
|
51
|
+
|
52
|
+
Class Methods
|
53
|
+
--------------
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class MyClass
|
57
|
+
self << class
|
58
|
+
let :cool_method, Fixnum do |f|
|
59
|
+
f + 1
|
60
|
+
end
|
61
|
+
let :cool_method, String do |s|
|
62
|
+
"{s}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
MyClass.cool_method(1) #=> 2
|
69
|
+
MyClass.cool_method("test") #=> "test"
|
50
70
|
```
|
51
71
|
|
72
|
+
|
52
73
|
Any Type
|
53
74
|
----------
|
54
75
|
|
55
|
-
```ruby
|
76
|
+
```ruby
|
56
77
|
class MyClass
|
57
78
|
include Ov
|
58
79
|
let :cool_method, Any do |any|
|
data/lib/ov.rb
CHANGED
@@ -56,18 +56,37 @@ require "ov/exception"
|
|
56
56
|
# Is the same so ruby `def`
|
57
57
|
#
|
58
58
|
#
|
59
|
+
# == Define as class methods
|
60
|
+
#
|
61
|
+
# class MyClass
|
62
|
+
# self << class
|
63
|
+
# let :cool_method, Fixnum do |f|
|
64
|
+
# f + 1
|
65
|
+
# end
|
66
|
+
# let :cool_method, String do |s|
|
67
|
+
# "{s}"
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
#
|
73
|
+
# MyClass.cool_method(1) #=> 2
|
74
|
+
# MyClass.cool_method("test") #=> "test"
|
75
|
+
#
|
59
76
|
module Ov
|
77
|
+
|
60
78
|
def self.included(base) # :nodoc:
|
61
79
|
base.extend(self)
|
62
80
|
base.class_eval do
|
63
|
-
class_variable_set(:@@__overridable_methods, OA.new)
|
81
|
+
class_variable_set(:@@__overridable_methods, OA.new) if !class_variable_defined?(:@@__overridable_methods)
|
64
82
|
end
|
65
83
|
end
|
66
|
-
|
84
|
+
|
67
85
|
def __overridable_methods # :nodoc:
|
68
86
|
send(:class_variable_get, :@@__overridable_methods)
|
69
87
|
end
|
70
|
-
|
88
|
+
|
89
|
+
private :__overridable_methods
|
71
90
|
##
|
72
91
|
# Create new method with +name+ and +types+
|
73
92
|
# When method called +block+ will be executed
|
@@ -82,23 +101,28 @@ module Ov
|
|
82
101
|
|
83
102
|
if !self.method_defined?(name)
|
84
103
|
self.instance_exec do
|
85
|
-
message = if self.class == Module
|
104
|
+
message = if self.class == Module
|
86
105
|
:define_singleton_method
|
87
106
|
else
|
88
107
|
:define_method
|
89
|
-
end
|
108
|
+
end
|
109
|
+
|
90
110
|
self.send(message, name) do |*args, &block|
|
91
111
|
types = *args.map(&:class)
|
92
112
|
owner = if self.class == Module
|
93
113
|
self
|
114
|
+
elsif self.respond_to?(:ancestors) && self == ancestors.first
|
115
|
+
self.singleton_class
|
94
116
|
else
|
95
117
|
self.class
|
96
118
|
end
|
97
|
-
|
119
|
+
|
98
120
|
method = OverrideMethod.new(name, types, owner)
|
99
|
-
|
100
|
-
|
101
|
-
|
121
|
+
z = owner.send(:__overridable_methods).where(method)
|
122
|
+
|
123
|
+
if z.nil?
|
124
|
+
raise NotImplementError.new("Method `#{name}` in `#{self}` class with types `#{types.join(', ')}` not implemented.")
|
125
|
+
end
|
102
126
|
instance_exec(*args, &z.body)
|
103
127
|
end
|
104
128
|
end
|
data/lib/ov/ov_array.rb
CHANGED
@@ -26,19 +26,5 @@ module Ov
|
|
26
26
|
end
|
27
27
|
|
28
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
29
|
end
|
44
30
|
end
|
data/lib/ov/ov_method.rb
CHANGED
@@ -12,25 +12,25 @@ module Ov
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def eql?(other)
|
15
|
+
def eql?(other) #:nodoc:
|
16
16
|
return true if @name == other.name && @types == other.types && @owner == other.owner
|
17
17
|
false
|
18
18
|
end
|
19
19
|
|
20
|
-
def eql0?(other)
|
21
|
-
@ancestors.find{|a|
|
20
|
+
def eql0?(other) #:nodoc:
|
21
|
+
@ancestors.find{|a|
|
22
22
|
a.__overridable_methods.find{|m|
|
23
23
|
m.name == other.name && m.types == other.types
|
24
24
|
}
|
25
25
|
}
|
26
26
|
end
|
27
27
|
|
28
|
-
def like?(other)
|
29
|
-
return compare?(types, other.types) if @name == other.name
|
28
|
+
def like?(other) #:nodoc:
|
29
|
+
return compare?(types, other.types) if @name == other.name
|
30
30
|
false
|
31
31
|
end
|
32
32
|
|
33
|
-
def like0?(other)
|
33
|
+
def like0?(other) #:nodoc:
|
34
34
|
@ancestors.find{|a|
|
35
35
|
a.__overridable_methods.find{|m|
|
36
36
|
m.like?(other)
|
data/lib/ov/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'ov'
|
2
|
+
|
3
|
+
|
4
|
+
class A
|
5
|
+
include Ov
|
6
|
+
let :test0, String do |s|
|
7
|
+
s
|
8
|
+
end
|
9
|
+
class << self
|
10
|
+
include Ov
|
11
|
+
|
12
|
+
let :test0, String do |s|
|
13
|
+
s
|
14
|
+
end
|
15
|
+
let :test0, Fixnum do |f|
|
16
|
+
f
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
p A.test0("das")
|
22
|
+
p A.test0(1)
|
23
|
+
p A.new.test0("dsa")
|
data/samples/myarray.rb
CHANGED
@@ -52,4 +52,47 @@ class TestAny
|
|
52
52
|
let :my_instance_method, Any do |any|
|
53
53
|
any
|
54
54
|
end
|
55
|
-
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class TestSingletonClass
|
58
|
+
include Ov
|
59
|
+
let :my_instance_method, String do |str|
|
60
|
+
str
|
61
|
+
end
|
62
|
+
|
63
|
+
class << self
|
64
|
+
include Ov
|
65
|
+
|
66
|
+
let :my_instance_method, Array, String do |arr, str|
|
67
|
+
arr << str
|
68
|
+
arr
|
69
|
+
end
|
70
|
+
|
71
|
+
let :my_instance_method, String do |str|
|
72
|
+
str
|
73
|
+
end
|
74
|
+
|
75
|
+
let :my_instance_method, Fixnum do |fixnum|
|
76
|
+
my_instance_method([], my_instance_method("bar"))
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module TestModule
|
82
|
+
include Ov
|
83
|
+
extend self
|
84
|
+
|
85
|
+
let :my_instance_method, Array, String do |arr, str|
|
86
|
+
arr << str
|
87
|
+
arr
|
88
|
+
end
|
89
|
+
|
90
|
+
let :my_instance_method, String do |str|
|
91
|
+
str
|
92
|
+
end
|
93
|
+
|
94
|
+
let :my_instance_method, Fixnum do |fixnum|
|
95
|
+
my_instance_method([], my_instance_method("bar"))
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
data/spec/override_spec.rb
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
+
class A
|
4
|
+
extend Ov
|
5
|
+
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
|
3
10
|
describe Ov do
|
4
11
|
context "instance methods" do
|
5
12
|
context "#my_instance_method" do
|
@@ -75,7 +82,60 @@ describe Ov do
|
|
75
82
|
expect{ test.my_instance_method()}.to raise_error(NotImplementError)
|
76
83
|
end
|
77
84
|
end
|
85
|
+
|
86
|
+
describe "#for module" do
|
87
|
+
context "#my_instance_method" do
|
88
|
+
let(:test){TestModule}
|
89
|
+
|
90
|
+
it "return array" do
|
91
|
+
result = test.my_instance_method(["foo", "bar"], "baz")
|
92
|
+
result.should eq ["foo", "bar", "baz"]
|
93
|
+
end
|
94
|
+
|
95
|
+
it "return string" do
|
96
|
+
result = test.my_instance_method("baz")
|
97
|
+
result.should eq "baz"
|
98
|
+
end
|
99
|
+
|
100
|
+
it "return overridable method" do
|
101
|
+
result = test.my_instance_method(1)
|
102
|
+
result.should eq ["bar"]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "#let class methods" do
|
108
|
+
context "#my_instance_method" do
|
109
|
+
let(:test){TestSingletonClass}
|
110
|
+
let(:test_ins){TestSingletonClass.new}
|
111
|
+
|
112
|
+
it "return array" do
|
113
|
+
result = test.my_instance_method(["foo", "bar"], "baz")
|
114
|
+
result.should eq ["foo", "bar", "baz"]
|
115
|
+
end
|
116
|
+
|
117
|
+
it "return string" do
|
118
|
+
result = test.my_instance_method("baz")
|
119
|
+
result.should eq "baz"
|
120
|
+
end
|
121
|
+
|
122
|
+
it "return overridable method" do
|
123
|
+
result = test.my_instance_method(1)
|
124
|
+
result.should eq ["bar"]
|
125
|
+
end
|
126
|
+
|
127
|
+
it "instance return string" do
|
128
|
+
result = test_ins.my_instance_method("baz")
|
129
|
+
result.should eq "baz"
|
130
|
+
end
|
131
|
+
|
132
|
+
it "exception when not defined" do
|
133
|
+
expect{ test_ins.my_instance_method(1,2,3)}.to raise_error(NotImplementError)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
78
137
|
end
|
79
138
|
|
80
139
|
|
81
140
|
|
141
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ov
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fntzr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/ov/ov_method.rb
|
72
72
|
- lib/ov/version.rb
|
73
73
|
- ov.gemspec
|
74
|
+
- samples/class_methods.rb
|
74
75
|
- samples/myarray.rb
|
75
76
|
- samples/pm.rb
|
76
77
|
- samples/rack.rb
|