type_tempest 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 +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +23 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +5 -0
- data/lib/type_tempest/version.rb +3 -0
- data/lib/type_tempest.rb +31 -0
- data/type_tempest.gemspec +23 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 18afd85a6340b560400256c512637d298e785d49
|
4
|
+
data.tar.gz: 66deffe30f0c1fe96beb7f90fab1223f93c86e8e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2167a1a5300a7d58183500be4977df73e0143338bbb26b62a8492ec8be27e891a327ab7b952cd16e0f97f205a1da751c6de86ca80f2ab995188a76c9f0985942
|
7
|
+
data.tar.gz: 6b1bd173226c45d8c2fdb153d01c97b91923cd1a4a571891a40974aa34614ad3e0ef57ffe18743f1af8fae8134f41208ff876c42e1e70ea2333a6578c5b30b39
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Copyright (c) 2014, Tom C
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
15
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
16
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
17
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
18
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
19
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
20
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
21
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
22
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
23
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 tom's laptop (magnusson)
|
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,69 @@
|
|
1
|
+
# TypeTempest
|
2
|
+
|
3
|
+
Type Tempest is a type-checking gem for Ruby methods, which automates the tedious pattern of `raise ArgumentError, "Blah is ivalid" unless blah.is_a?(String)`
|
4
|
+
|
5
|
+
Say what you will about dynamic typing -- this is for those times where one does not want a million possibilities in their inputs.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'type_tempest'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install type_tempest
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Type Tempest adds two similar, top-level, and syntactically-odd methods to Ruby: `check()` and `strict()`.
|
24
|
+
|
25
|
+
`check()` passes if the given variable matches the given class, or one of its subclasses. (`var.is_a?(Klass)`).
|
26
|
+
|
27
|
+
`strict()` passes if the given variable matches the given class. (`var.instance_of?(Klass)`)
|
28
|
+
|
29
|
+
**Use these shortcuts as the first line in a method whose parameters you would like to check, as such:**
|
30
|
+
|
31
|
+
|
32
|
+
def checkme(a_string, a_int, another_string)
|
33
|
+
check (String) {:a_string}
|
34
|
+
check (Fixnum) {:a_int}
|
35
|
+
strict (String) {:another_string}
|
36
|
+
|
37
|
+
# some code here...
|
38
|
+
|
39
|
+
"Done!"
|
40
|
+
end
|
41
|
+
|
42
|
+
Behind the scenes, TypeTempest eval()s the result of the given block and throws `TypeMismatchError` if the result's type differs from what is expected (as in, it fails `is_a?()` or `instance_of?()`). In this example, the first call to `checkme()` would pass, but the second would throw `TypeMismatchError`:
|
43
|
+
|
44
|
+
|
45
|
+
checkme("Hi", 2, "ada") # => "Done!"
|
46
|
+
checkme(0,0,0) # => Parameter a_string should be of type String, but was instead Fixnum (TypeMismatchError)
|
47
|
+
|
48
|
+
## Comments
|
49
|
+
|
50
|
+
The `check (String) {:a_string}` syntax is awkward and not very Ruby-like. Using blocks to access the caller's binding is a bit of a hack, and if there were an easier way to walk the call-stack and derive method parameters then the syntax would be different.
|
51
|
+
|
52
|
+
An ideal version of this feature would look like this: `check String a_string` or `strict Geometry::Box box_obj`. Unfortunately...
|
53
|
+
|
54
|
+
* I am not aware of any way to cheaply retreive both the contents (variable names, etc) and values of a block.
|
55
|
+
* I am not aware of any way to cheaply walk the call-stack in a way that allows one to retreive a caller's parameter names and values.
|
56
|
+
* I am not aware of any other way to evaluate code in the context of the caller.
|
57
|
+
* Block syntax seems to only work when method parameters are wrapped in `(` and `)`.
|
58
|
+
* Passing `local_variables`, `_`, or a self-reference to `check()` would further clutter syntax.
|
59
|
+
* Blocks must be designated with `{}` or `do..end`
|
60
|
+
|
61
|
+
For these reasons, syntax is presented as-is, with possible tweaks towards the stated goal saved for major revisions.
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/type_tempest.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "type_tempest/version"
|
2
|
+
|
3
|
+
class TypeMismatchError < TypeError
|
4
|
+
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
def check(type, &block)
|
9
|
+
|
10
|
+
type = type.to_s.constantize if defined?(ActiveSupport) && (type.instance_of?(Symbol) || type.instance_of?(String))
|
11
|
+
raise TypeError, "`type` must be an object of type Class" unless type.instance_of?(Class)
|
12
|
+
|
13
|
+
var = yield
|
14
|
+
vartyp = eval("#{var.to_s}", block.binding)
|
15
|
+
|
16
|
+
raise TypeMismatchError, "Parameter #{var} should be of type #{type}, but was instead #{vartyp.class}" unless vartyp.is_a?(type)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def strict(type, &block)
|
22
|
+
|
23
|
+
type = type.to_s.constantize if defined?(ActiveSupport) && (type.instance_of?(Symbol) || type.instance_of?(String))
|
24
|
+
raise TypeError, "`type` must be an object of type Class" unless type.instance_of?(Class)
|
25
|
+
|
26
|
+
var = yield
|
27
|
+
vartyp = eval("#{var.to_s}", block.binding)
|
28
|
+
|
29
|
+
raise TypeMismatchError, "Parameter #{var} should be of type #{type}, but was instead #{vartyp.class}" unless vartyp.instance_of?(type)
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'type_tempest/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "type_tempest"
|
8
|
+
spec.version = TypeTempest::VERSION
|
9
|
+
spec.authors = ["Lyjia / Tom Corelis"]
|
10
|
+
spec.email = ["tom@tomcorelis.com"]
|
11
|
+
spec.description = %q{Simple type-checking for Ruby}
|
12
|
+
spec.summary = %q{Simple type-checking for Ruby}
|
13
|
+
spec.homepage = "http://www.lyjia.us"
|
14
|
+
spec.license = "BSD 2-Clause"
|
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: type_tempest
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lyjia / Tom Corelis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-05 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
|
+
description: Simple type-checking for Ruby
|
42
|
+
email:
|
43
|
+
- tom@tomcorelis.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/type_tempest.rb
|
55
|
+
- lib/type_tempest/version.rb
|
56
|
+
- type_tempest.gemspec
|
57
|
+
homepage: http://www.lyjia.us
|
58
|
+
licenses:
|
59
|
+
- BSD 2-Clause
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.0.3
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Simple type-checking for Ruby
|
81
|
+
test_files: []
|