safety_check 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 768cb88cef157bf5b487f5f988cf3ce0adfccfb7
4
+ data.tar.gz: c0b8b0dfd8ac6c2f3fd7ad6cde6ffd39246fd362
5
+ SHA512:
6
+ metadata.gz: a2af22acea0fe6318a8daa3751b894e1818e1eb6387dd804d06ce0bd4b5938aa6c7303b879056ca194f7bdbc8f30d8cee7413f8946b0647b1241222e81b1483a
7
+ data.tar.gz: 8dab9529587167d1738ec89a2c07b045e54d0ac236563b5fae2f1587576a86e27df14ddecc59c7b64d4ddc8737d51aeb9f6d907b0fc82e336fe33df2cd4d344e
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
6
+ ._*
7
+ .redcar/
8
+ .idea/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --tty
2
+ --colour
3
+ --format p
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ safety_check
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ #Safety Check
2
+
3
+ Selectively add type safety to your methods.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ desc 'Open an irb session preloaded with this library'
9
+ task :console do
10
+ sh 'irb -rubygems -I lib -r safety_check.rb'
11
+ end
@@ -0,0 +1,39 @@
1
+ module SafetyCheck
2
+ def self.included(base)
3
+ base.send :extend, ClassMethods
4
+ end
5
+
6
+ module ClassMethods
7
+ def safety_check(method_name, *arg_types)
8
+ if self.methods.include?(method_name)
9
+ receiver = self.singleton_class
10
+ elsif self.instance_methods.include?(method_name)
11
+ receiver = self
12
+ else
13
+ raise NoMethodError, "undefined method '#{method_name}' for #{receiver.inspect}"
14
+ end
15
+
16
+ unsafe_method_name = "#{method_name.to_s}_without_type_safety".to_sym
17
+
18
+ if arg_types.length != receiver.instance_method(method_name).arity
19
+ raise ArgumentError, "wrong number of arguments for #{method_name.inspect} (#{arg_types.length} for #{receiver.instance_method(method_name).arity})"
20
+ end
21
+
22
+ receiver.send(:alias_method, unsafe_method_name, method_name)
23
+
24
+ receiver.send(:define_method, method_name) do |*args|
25
+ if args.length != arg_types.length
26
+ raise ArgumentError, "wrong number of arguments (#{args.length} for #{arg_types.length}) when calling #{method_name.inspect}"
27
+ end
28
+
29
+ arg_types.zip(args).each do |arg_type, arg|
30
+ unless arg.is_a? arg_type
31
+ raise ArgumentError, "expected #{arg.inspect} to be a #{arg_type} when calling #{method_name.inspect}"
32
+ end
33
+ end
34
+
35
+ self.send(unsafe_method_name, *args)
36
+ end
37
+ end
38
+ end
39
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module SafetyCheck
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'safety_check'
7
+ s.version = SafetyCheck::VERSION
8
+ s.authors = ['Sean Eshbaugh']
9
+ s.email = ['seaneshbaugh@gmail.com']
10
+ s.homepage = 'https://github.com/seaneshbaugh/safety_check'
11
+ s.summary = %q{Selectively add type safety to your methods.}
12
+ s.description = %q{Selectively add type safety to your methods.}
13
+
14
+ s.rubyforge_project = 'safety_check'
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_development_dependency 'rspec'
22
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe SafetyCheck do
4
+ describe 'safety_check' do
5
+ it 'instance methods should not raise an error when the correct type of argument is used' do
6
+ hi = Hello.new
7
+
8
+ expect(hi.greetings('world')).to eq("Hello, world!")
9
+ end
10
+
11
+ it 'instance methods should raise an error when the wrong type of argument is used' do
12
+ hi = Hello.new
13
+
14
+ expect { hi.greetings(5) }.to raise_error
15
+ end
16
+
17
+ it 'instance methods should raise an error when the wrong number of argument is used' do
18
+ hi = Hello.new
19
+
20
+ expect { hi.greetings('world', 'lol') }.to raise_error
21
+ end
22
+
23
+ it 'class methods should not raise an error when the correct type of argument is used' do
24
+ expect(Hello.salutations('world')).to eq("Good day, world!")
25
+ end
26
+
27
+ it 'class methods should raise an error when the wrong type of argument is used' do
28
+ expect { Hello.salutations(5) }.to raise_error
29
+ end
30
+
31
+ it 'class methods should raise an error when the wrong number of argument is used' do
32
+ expect { Hello.salutations('world', 'lol') }.to raise_error
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,5 @@
1
+ require 'safety_check'
2
+
3
+ Dir["#{File.dirname(__FILE__)}/test_models/*.rb"].sort.each do |path|
4
+ require "test_models/#{File.basename(path, '.rb')}"
5
+ end
@@ -0,0 +1,13 @@
1
+ class Hello
2
+ include SafetyCheck
3
+
4
+ def greetings(subject)
5
+ "Hello, #{subject}!"
6
+ end
7
+ safety_check :greetings, String
8
+
9
+ def self.salutations(subject)
10
+ "Good day, #{subject}!"
11
+ end
12
+ safety_check :salutations, String
13
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safety_check
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean Eshbaugh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Selectively add type safety to your methods.
28
+ email:
29
+ - seaneshbaugh@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".ruby-gemset"
37
+ - ".ruby-version"
38
+ - Gemfile
39
+ - README.md
40
+ - Rakefile
41
+ - lib/safety_check.rb
42
+ - lib/version.rb
43
+ - safety_check.gemspec
44
+ - spec/safety_check/safety_check_spec.rb
45
+ - spec/spec_helper.rb
46
+ - spec/test_models/hello.rb
47
+ homepage: https://github.com/seaneshbaugh/safety_check
48
+ licenses: []
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project: safety_check
66
+ rubygems_version: 2.2.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Selectively add type safety to your methods.
70
+ test_files:
71
+ - spec/safety_check/safety_check_spec.rb
72
+ - spec/spec_helper.rb
73
+ - spec/test_models/hello.rb