type_hinting 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.
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +70 -0
- data/Rakefile +2 -0
- data/lib/type_hinting/version.rb +3 -0
- data/lib/type_hinting.rb +40 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/type_hinting_spec.rb +49 -0
- data/type_hinting.gemspec +25 -0
- metadata +139 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Brian Zeligson
|
|
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,70 @@
|
|
|
1
|
+
# TypeHinting
|
|
2
|
+
|
|
3
|
+
I sometimes find myself writing code like this:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
def do_something_with_argument(argument)
|
|
7
|
+
unless argument.kind_of?(SomeClassImExpecting)
|
|
8
|
+
raise Exception, 'Whatever'
|
|
9
|
+
end
|
|
10
|
+
do_more_stuff
|
|
11
|
+
end
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
This gem provides a mixin which add two class methods, return\_type and
|
|
15
|
+
param\_types, that are a bit nicer to work with.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
Add this line to your application's Gemfile:
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
gem 'type_hinting'
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
And then execute:
|
|
26
|
+
|
|
27
|
+
$ bundle
|
|
28
|
+
|
|
29
|
+
Or install it yourself as:
|
|
30
|
+
|
|
31
|
+
$ gem install type_hinting
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
Dead simple, here's the example used in the tests:
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
|
|
39
|
+
class TypeHinted
|
|
40
|
+
include TypeHinting
|
|
41
|
+
|
|
42
|
+
def return_should_work
|
|
43
|
+
[]
|
|
44
|
+
end
|
|
45
|
+
return_type(:return_should_work, Array)
|
|
46
|
+
# raise if method returns anything other than array
|
|
47
|
+
|
|
48
|
+
def return_should_raise
|
|
49
|
+
nil
|
|
50
|
+
end
|
|
51
|
+
return_type(:return_should_raise, Array)
|
|
52
|
+
# same as above
|
|
53
|
+
|
|
54
|
+
def hinted_params(a, b = 4)
|
|
55
|
+
[a, b]
|
|
56
|
+
end
|
|
57
|
+
param_types(:hinted_params, Array, Fixnum)
|
|
58
|
+
# raise if passed anything other than Array for arg1,
|
|
59
|
+
# raise if passed anything other than Fixnum for arg2
|
|
60
|
+
# nil is allowed for arg2 since there is a default value specified
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Contributing
|
|
65
|
+
|
|
66
|
+
1. Fork it ( https://github.com/[my-github-username]/type_hinting/fork )
|
|
67
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
68
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
69
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
70
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/type_hinting.rb
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require "type_hinting/version"
|
|
2
|
+
|
|
3
|
+
module TypeHinting
|
|
4
|
+
|
|
5
|
+
def self.included(base)
|
|
6
|
+
base.extend ClassMethods
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def skipped_optional_param(param_type, value)
|
|
10
|
+
param_type != :req && value.nil?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
|
|
15
|
+
def return_type(name, type)
|
|
16
|
+
method = instance_method(name)
|
|
17
|
+
define_method(name) do |*args|
|
|
18
|
+
r = method.bind(self).call(*args)
|
|
19
|
+
unless r.kind_of?(type)
|
|
20
|
+
raise Exception, "Invalid return type of #{r.class}, expecting #{type}"
|
|
21
|
+
end
|
|
22
|
+
r
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def param_types(name, *arg_types)
|
|
27
|
+
method = instance_method(name)
|
|
28
|
+
sig_args = method.parameters
|
|
29
|
+
define_method(name) do |*args|
|
|
30
|
+
arg_types.zip(sig_args, args).each do |(type, (required, name), arg)|
|
|
31
|
+
if !arg.kind_of?(type) && !skipped_optional_param(required, arg)
|
|
32
|
+
raise Exception, "Invalid type #{arg.class} for " <<
|
|
33
|
+
"parameter #{name}, expecting #{type}"
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
method.bind(self).call(*args)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
class TypeHinted
|
|
4
|
+
include TypeHinting
|
|
5
|
+
|
|
6
|
+
def return_should_work
|
|
7
|
+
[]
|
|
8
|
+
end
|
|
9
|
+
return_type(:return_should_work, Array)
|
|
10
|
+
|
|
11
|
+
def return_should_raise
|
|
12
|
+
nil
|
|
13
|
+
end
|
|
14
|
+
return_type(:return_should_raise, Array)
|
|
15
|
+
|
|
16
|
+
def hinted_params(a, b = 4)
|
|
17
|
+
[a, b]
|
|
18
|
+
end
|
|
19
|
+
param_types(:hinted_params, Array, Fixnum)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe TypeHinting do
|
|
23
|
+
let(:h) { TypeHinted.new }
|
|
24
|
+
|
|
25
|
+
describe '.return_type' do
|
|
26
|
+
|
|
27
|
+
it 'raises when method returns other than specified return type' do
|
|
28
|
+
expect{h.return_should_work}.to_not raise_error
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'doesnt raise when method returns the specified return type' do
|
|
32
|
+
expect{h.return_should_raise}.to raise_error
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe '.param_types' do
|
|
37
|
+
|
|
38
|
+
it 'raises on param passed of incorrect type' do
|
|
39
|
+
expect{h.hinted_params('b', 'd')}.to raise_error
|
|
40
|
+
expect{h.hinted_params('b', 3)}.to raise_error
|
|
41
|
+
expect{h.hinted_params(['b'], :d)}.to raise_error
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it 'does not raise when all passed params are correct type' do
|
|
45
|
+
expect{h.hinted_params([:c, :d], 5)}.to_not raise_error
|
|
46
|
+
expect{h.hinted_params(['foo'])}.to_not raise_error
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'type_hinting/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "type_hinting"
|
|
8
|
+
spec.version = TypeHinting::VERSION
|
|
9
|
+
spec.authors = ["Brian Zeligson"]
|
|
10
|
+
spec.email = ["bzeligson@localytics.com"]
|
|
11
|
+
spec.summary = %q{Get type safety in your Ruby code}
|
|
12
|
+
spec.homepage = ""
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
|
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
18
|
+
spec.require_paths = ["lib"]
|
|
19
|
+
|
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
22
|
+
spec.add_development_dependency "rspec"
|
|
23
|
+
spec.add_development_dependency "pry"
|
|
24
|
+
spec.add_development_dependency "pry-debugger"
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: type_hinting
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Brian Zeligson
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2014-11-17 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.7'
|
|
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.7'
|
|
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: '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: '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'
|
|
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'
|
|
78
|
+
- !ruby/object:Gem::Dependency
|
|
79
|
+
name: pry-debugger
|
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
|
81
|
+
none: false
|
|
82
|
+
requirements:
|
|
83
|
+
- - ! '>='
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
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'
|
|
94
|
+
description:
|
|
95
|
+
email:
|
|
96
|
+
- bzeligson@localytics.com
|
|
97
|
+
executables: []
|
|
98
|
+
extensions: []
|
|
99
|
+
extra_rdoc_files: []
|
|
100
|
+
files:
|
|
101
|
+
- .gitignore
|
|
102
|
+
- Gemfile
|
|
103
|
+
- LICENSE.txt
|
|
104
|
+
- README.md
|
|
105
|
+
- Rakefile
|
|
106
|
+
- lib/type_hinting.rb
|
|
107
|
+
- lib/type_hinting/version.rb
|
|
108
|
+
- spec/spec_helper.rb
|
|
109
|
+
- spec/type_hinting_spec.rb
|
|
110
|
+
- type_hinting.gemspec
|
|
111
|
+
homepage: ''
|
|
112
|
+
licenses:
|
|
113
|
+
- MIT
|
|
114
|
+
post_install_message:
|
|
115
|
+
rdoc_options: []
|
|
116
|
+
require_paths:
|
|
117
|
+
- lib
|
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
119
|
+
none: false
|
|
120
|
+
requirements:
|
|
121
|
+
- - ! '>='
|
|
122
|
+
- !ruby/object:Gem::Version
|
|
123
|
+
version: '0'
|
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
|
+
none: false
|
|
126
|
+
requirements:
|
|
127
|
+
- - ! '>='
|
|
128
|
+
- !ruby/object:Gem::Version
|
|
129
|
+
version: '0'
|
|
130
|
+
requirements: []
|
|
131
|
+
rubyforge_project:
|
|
132
|
+
rubygems_version: 1.8.24
|
|
133
|
+
signing_key:
|
|
134
|
+
specification_version: 3
|
|
135
|
+
summary: Get type safety in your Ruby code
|
|
136
|
+
test_files:
|
|
137
|
+
- spec/spec_helper.rb
|
|
138
|
+
- spec/type_hinting_spec.rb
|
|
139
|
+
has_rdoc:
|