ov 0.0.4 → 0.1.0
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 +4 -4
- data/README.md +125 -0
- data/lib/ov.rb +24 -3
- data/lib/ov/ext/matching.rb +38 -0
- data/lib/ov/version.rb +1 -1
- data/samples/pm.rb +1 -21
- data/spec/fixtures/ext_spec.rb +19 -0
- data/spec/fixtures/fixture_classes.rb +16 -1
- data/spec/override_spec.rb +20 -0
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a8c79de0b03e449c81d57dd343ca495ea2ce6de
|
4
|
+
data.tar.gz: ee617eb2f68fdaa704ccda36f462f393c9a7955c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc98e314109927c96c1bf543491448c8a027cc0ad081d593072ae85e8e3f3d2d2b5c792adaf014099ed3d25a3353ce81996b8005df6972927b5a8b9ad77df6cd
|
7
|
+
data.tar.gz: c4a1bd66e76174296c7bbd03a1753607b5606ec6f242b71219b1727fa20661fa8df35a98acdb709d539fcc495be58619ebba520ef1fe72e5d2f33cbc86a409e6
|
data/README.md
CHANGED
@@ -16,6 +16,72 @@ Or install it yourself as:
|
|
16
16
|
|
17
17
|
$ gem install ov
|
18
18
|
|
19
|
+
|
20
|
+
## Some examples:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'ov'
|
24
|
+
|
25
|
+
# ====== Define methods
|
26
|
+
|
27
|
+
class Foo
|
28
|
+
include Ov
|
29
|
+
|
30
|
+
let :foo, String do |str|
|
31
|
+
# some code
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
foo = Foo.new
|
36
|
+
foo.foo("foo") # => ok
|
37
|
+
foo.foo(123) # => Method `foo` in `#<Foo:0x007fe423c19dc8>` class with types `Fixnum` not implemented. (Ov::NotImplementError)
|
38
|
+
|
39
|
+
# ====== Custom Equality for Ruby Types
|
40
|
+
|
41
|
+
|
42
|
+
class String
|
43
|
+
include Ov
|
44
|
+
let :is_equal, String do |str|
|
45
|
+
self == str
|
46
|
+
end
|
47
|
+
|
48
|
+
let :is_equal, Any do |other|
|
49
|
+
raise TypeError.new("Only for string")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
str = "foo"
|
54
|
+
|
55
|
+
p str == 123 # false, but this different types
|
56
|
+
p str.is_equal("bar") # false
|
57
|
+
p str.is_equal("foo") # true
|
58
|
+
p str.is_equal(123) # TypeError
|
59
|
+
|
60
|
+
|
61
|
+
# ====== Use Ov::Ext for matching
|
62
|
+
|
63
|
+
require 'kleisli'
|
64
|
+
|
65
|
+
include Ov::Ext
|
66
|
+
|
67
|
+
match(Try{1/0}) {
|
68
|
+
try(Kleisli::Try::Success) { puts "ok" }
|
69
|
+
try(Kleisli::Try::Failure) { puts "error" }
|
70
|
+
}
|
71
|
+
|
72
|
+
result = match(Net::HTTP.get_response(URI("http://google.com"))) do
|
73
|
+
try(Net::HTTPOK) {|r| r.header }
|
74
|
+
try(Net::HTTPMovedPermanently) {|r| r.header }
|
75
|
+
otherwise { "error" }
|
76
|
+
end
|
77
|
+
|
78
|
+
puts result
|
79
|
+
|
80
|
+
```
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
19
85
|
## Usage
|
20
86
|
|
21
87
|
Firstly include `Ov` in you class
|
@@ -109,10 +175,69 @@ b.test(123) # => only for fixnum
|
|
109
175
|
|
110
176
|
```
|
111
177
|
|
178
|
+
Call method:
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
|
182
|
+
class A
|
183
|
+
include Ov
|
184
|
+
|
185
|
+
let :test, Fixnum do |num|
|
186
|
+
test("#{num}") # call with string argument
|
187
|
+
end
|
188
|
+
|
189
|
+
let :test, String do |str|
|
190
|
+
p str
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
a = A.new
|
195
|
+
a.test(123) # => "123"
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
|
200
|
+
Work with blocks
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
class MyClass
|
204
|
+
include Ov
|
205
|
+
let :my_method, Fixnum do |num, block| # instead of |num, &block|
|
206
|
+
p num
|
207
|
+
block.call
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
MyClass.new.my_method(1) do
|
212
|
+
p "123"
|
213
|
+
end
|
214
|
+
# => 1
|
215
|
+
# => 123
|
216
|
+
```
|
217
|
+
|
112
218
|
Examples
|
113
219
|
--------
|
114
220
|
see [link](https://github.com/fntzr/ov/blob/master/samples)
|
115
221
|
|
222
|
+
## TODO
|
223
|
+
|
224
|
+
1. work with symbol method names: `+, -, *, etc`
|
225
|
+
2. some ideas
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
# multiple arguments
|
229
|
+
let :test, Fixnum, [String] # signature: Fixnum, and others must be String
|
230
|
+
|
231
|
+
let :test, Multiple #any types
|
232
|
+
|
233
|
+
let :test, :foo # Type must have :foo method
|
234
|
+
|
235
|
+
let :test, Or[Fixnum, String] # work for String or Fixnum
|
236
|
+
|
237
|
+
```
|
238
|
+
|
239
|
+
|
240
|
+
|
116
241
|
## Contributing
|
117
242
|
|
118
243
|
1. Fork it
|
data/lib/ov.rb
CHANGED
@@ -3,7 +3,7 @@ require "ov/ov_array"
|
|
3
3
|
require "ov/ov_method"
|
4
4
|
require "ov/ov_any"
|
5
5
|
require "ov/exception"
|
6
|
-
|
6
|
+
require 'ov/ext/matching'
|
7
7
|
|
8
8
|
##
|
9
9
|
# `Ov` module provides functional for creating methods
|
@@ -74,6 +74,25 @@ require "ov/exception"
|
|
74
74
|
# MyClass.cool_method(1) #=> 2
|
75
75
|
# MyClass.cool_method("test") #=> "test"
|
76
76
|
#
|
77
|
+
#
|
78
|
+
# == Work with blocks
|
79
|
+
#
|
80
|
+
# Blocks, should be pass given without ampersand (&)
|
81
|
+
#
|
82
|
+
# class MyClass
|
83
|
+
#
|
84
|
+
# let :my_method, Fixnum do |num, block| # instead of |num, &block|
|
85
|
+
# p num
|
86
|
+
# block.call
|
87
|
+
# end
|
88
|
+
#
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# MyClass.new.my_method(1) do
|
92
|
+
# p "123"
|
93
|
+
# end
|
94
|
+
# # => 1
|
95
|
+
# # => 123
|
77
96
|
module Ov
|
78
97
|
|
79
98
|
def self.included(base) # :nodoc:
|
@@ -98,7 +117,7 @@ module Ov
|
|
98
117
|
#
|
99
118
|
def let(name, *types, &block)
|
100
119
|
included = false
|
101
|
-
|
120
|
+
|
102
121
|
if self.instance_methods.include?(name)
|
103
122
|
included = true
|
104
123
|
class_eval "alias :ov_old_#{name.to_s} #{name}"
|
@@ -129,7 +148,9 @@ module Ov
|
|
129
148
|
raise Ov::NotImplementError.new("Method `#{name}` in `#{self}` class with types `#{types.join(', ')}` not implemented.")
|
130
149
|
end
|
131
150
|
else
|
132
|
-
|
151
|
+
k = *args
|
152
|
+
k << block if !block.nil?
|
153
|
+
instance_exec(*k, &z.body)
|
133
154
|
end
|
134
155
|
end
|
135
156
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Ov
|
2
|
+
module Ext
|
3
|
+
#
|
4
|
+
# Add `match` method, which work like `case` statement but for types
|
5
|
+
#
|
6
|
+
# == Usage
|
7
|
+
#
|
8
|
+
# include Ov::Ext
|
9
|
+
#
|
10
|
+
# match("String", "dsa") do
|
11
|
+
# try(String, Array) {|str, arr| "#{str} #{arr}" }
|
12
|
+
# try(String) {|str| "#{str}" }
|
13
|
+
# otherwise { "none" }
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
#
|
17
|
+
def match(*args, &block)
|
18
|
+
z = Module.new do
|
19
|
+
include Ov
|
20
|
+
extend self
|
21
|
+
def try(*args, &block)
|
22
|
+
let :anon_method, *args, &block
|
23
|
+
end
|
24
|
+
def otherwise(&block)
|
25
|
+
let :otherwise, &block
|
26
|
+
end
|
27
|
+
instance_eval &block
|
28
|
+
end
|
29
|
+
begin
|
30
|
+
z.anon_method(*args)
|
31
|
+
rescue Ov::NotImplementError => e
|
32
|
+
z.otherwise
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
data/lib/ov/version.rb
CHANGED
data/samples/pm.rb
CHANGED
@@ -1,28 +1,8 @@
|
|
1
1
|
require "ov"
|
2
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 Ov::NotImplementError => e
|
19
|
-
z.otherwise
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
3
|
|
24
4
|
|
25
|
-
include
|
5
|
+
include Ov::Ext
|
26
6
|
|
27
7
|
|
28
8
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ov::Ext do
|
4
|
+
|
5
|
+
include Ov::Ext
|
6
|
+
|
7
|
+
it "#match method" do
|
8
|
+
result = match("String", [123]) do
|
9
|
+
try(String, Array) {|str, arr| 1 }
|
10
|
+
try(String) {|str| 2 }
|
11
|
+
otherwise { 3 }
|
12
|
+
end
|
13
|
+
|
14
|
+
expect(result).to eq(1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
|
@@ -80,7 +80,7 @@ end
|
|
80
80
|
|
81
81
|
module TestModule
|
82
82
|
include Ov
|
83
|
-
|
83
|
+
|
84
84
|
|
85
85
|
let :my_instance_method, Array, String do |arr, str|
|
86
86
|
arr << str
|
@@ -96,3 +96,18 @@ module TestModule
|
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|
99
|
+
class ClassWithBlock
|
100
|
+
include Ov
|
101
|
+
|
102
|
+
|
103
|
+
let :test, Fixnum do |num, block|
|
104
|
+
num + block.call
|
105
|
+
end
|
106
|
+
|
107
|
+
class << self
|
108
|
+
include Ov
|
109
|
+
let :test, String do |str, block|
|
110
|
+
str << block.call
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
data/spec/override_spec.rb
CHANGED
@@ -152,6 +152,26 @@ describe Ov do
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
end
|
155
|
+
|
156
|
+
describe "work with blocks" do
|
157
|
+
let(:instance){ClassWithBlock.new}
|
158
|
+
let(:klass){ClassWithBlock}
|
159
|
+
|
160
|
+
it "should pass block in instance method" do
|
161
|
+
result = instance.test(3) do
|
162
|
+
3
|
163
|
+
end
|
164
|
+
expect(result).to eq(6)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should pass block in class method" do
|
168
|
+
result = klass.test("foo") do
|
169
|
+
"bar"
|
170
|
+
end
|
171
|
+
expect(result).to eq("foobar")
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
155
175
|
end
|
156
176
|
|
157
177
|
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- fntzr
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,7 @@ files:
|
|
66
66
|
- Rakefile
|
67
67
|
- lib/ov.rb
|
68
68
|
- lib/ov/exception.rb
|
69
|
+
- lib/ov/ext/matching.rb
|
69
70
|
- lib/ov/ov_any.rb
|
70
71
|
- lib/ov/ov_array.rb
|
71
72
|
- lib/ov/ov_method.rb
|
@@ -77,6 +78,7 @@ files:
|
|
77
78
|
- samples/pm.rb
|
78
79
|
- samples/rack.rb
|
79
80
|
- samples/service.rb
|
81
|
+
- spec/fixtures/ext_spec.rb
|
80
82
|
- spec/fixtures/fixture_classes.rb
|
81
83
|
- spec/override_spec.rb
|
82
84
|
- spec/spec_helper.rb
|
@@ -100,12 +102,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
102
|
version: '0'
|
101
103
|
requirements: []
|
102
104
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.
|
105
|
+
rubygems_version: 2.4.5
|
104
106
|
signing_key:
|
105
107
|
specification_version: 4
|
106
108
|
summary: ov gem provides hacks for write multimemethods in Ruby.
|
107
109
|
test_files:
|
110
|
+
- spec/fixtures/ext_spec.rb
|
108
111
|
- spec/fixtures/fixture_classes.rb
|
109
112
|
- spec/override_spec.rb
|
110
113
|
- spec/spec_helper.rb
|
111
|
-
has_rdoc:
|