monkey_type 0.0.4
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 +7 -0
- data/lib/monkey_type.rb +71 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 45353da6ea03574b9449b6528d1cb2b17a36a82b
|
4
|
+
data.tar.gz: 182c5e58931b9034cf2d6c19bdf2c91acc993200
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c47abf0aeddc4084eb4dda9c07c93d5d56e48e6f80ff89ddc8008ea2b9eafaed8ed2ec05ad784c5b26e426b1ee1669255123e726bc0d948f400f395ebdadd28a
|
7
|
+
data.tar.gz: cd77a49238afa1b2e7fe7f7ce6253a8e4d0550040a46856e0e4e920a71f5970fd4b232291c685ef8e8b244dca2ae09f9ed2b2bb13791faac4a043d17e34c4e10
|
data/lib/monkey_type.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
module MonkeyType
|
2
|
+
|
3
|
+
refine Class do
|
4
|
+
def contract(method, type)
|
5
|
+
alias_method "#{method}_typeless" , "#{method}"
|
6
|
+
|
7
|
+
define_method "#{method}" do |*args|
|
8
|
+
returned = send("#{method}_typeless",*args)
|
9
|
+
unless returned.is_duck? type
|
10
|
+
raise TypeError, "returned value #{returned} is not #{type}"
|
11
|
+
end
|
12
|
+
returned
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
refine Object do
|
18
|
+
def is (type)
|
19
|
+
unless is_duck? type
|
20
|
+
raise TypeError, "error: #{self.class} is not an #{type}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def is_duck?(type)
|
25
|
+
|
26
|
+
if type.is_a? Option
|
27
|
+
if self == nil then return true end
|
28
|
+
type = type.get_type
|
29
|
+
end
|
30
|
+
|
31
|
+
if type.is_a? Boolean
|
32
|
+
return self == true || self == false
|
33
|
+
end
|
34
|
+
|
35
|
+
if self.is_a? type then return true end
|
36
|
+
|
37
|
+
self_methods = self.methods
|
38
|
+
type.instance_methods.each do |method|
|
39
|
+
unless self_methods.include? method
|
40
|
+
return false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
return true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
refine Object do
|
49
|
+
def option(type)
|
50
|
+
MonkeyType::Option.new(type)
|
51
|
+
end
|
52
|
+
|
53
|
+
def boolean
|
54
|
+
MonkeyType::Boolean.new
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class Option
|
59
|
+
def initialize(type)
|
60
|
+
@type = type
|
61
|
+
end
|
62
|
+
|
63
|
+
def get_type
|
64
|
+
@type
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class Boolean
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: monkey_type
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Felipe Vieira
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |-
|
14
|
+
Only 70 lines. Add a optional type system to ruby using runtime checks and contracts . make it crash early, know exactly what a function (or method)
|
15
|
+
returns and specify the 'type' of the parameters using good old ducktyping
|
16
|
+
email: felipetavres@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/monkey_type.rb
|
22
|
+
homepage: https://github.com/nemoNoboru/monkey_type
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.5.1
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: A microgem thats adds a micro type system to ruby
|
46
|
+
test_files: []
|