typesafe 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Till Salzer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ = typesafe
2
+
3
+ TypeSafe is a tiny extension to core Ruby to simplify checks for
4
+ kind_of?.
5
+
6
+ == Note on Patches/Pull Requests
7
+
8
+ * Fork the project.
9
+ * Make your feature addition or bug fix.
10
+ * Add tests for it. This is important so I don't break it in a
11
+ future version unintentionally.
12
+ * Commit, do not mess with rakefile, version, or history.
13
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Till Salzer. See LICENSE for details.
@@ -0,0 +1,61 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "typesafe"
8
+ gem.summary = %Q{provides Object#must_be_kind_of for typesafe development}
9
+ gem.description = %Q{Simplifies typesafe development by providing the additional
10
+ methods Object#must_by_kind_of ad Object#must_be_a.}
11
+ gem.email = "till.salzer@googlemail.com"
12
+ gem.homepage = "http://github.com/tsalzer/typesafe"
13
+ gem.authors = ["Till Salzer"]
14
+ gem.add_development_dependency "rspec"
15
+ gem.add_development_dependency "cucumber"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ begin
37
+ require 'cucumber/rake/task'
38
+ Cucumber::Rake::Task.new(:features)
39
+
40
+ task :features => :check_dependencies
41
+ rescue LoadError
42
+ task :features do
43
+ abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
44
+ end
45
+ end
46
+
47
+ task :default => :spec
48
+
49
+ require 'rake/rdoctask'
50
+ Rake::RDocTask.new do |rdoc|
51
+ if File.exist?('VERSION')
52
+ version = File.read('VERSION')
53
+ else
54
+ version = ""
55
+ end
56
+
57
+ rdoc.rdoc_dir = 'rdoc'
58
+ rdoc.title = "typesafe #{version}"
59
+ rdoc.rdoc_files.include('README*')
60
+ rdoc.rdoc_files.include('lib/**/*.rb')
61
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
+ require 'typesafe'
3
+
4
+ require 'spec/expectations'
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
@@ -0,0 +1,76 @@
1
+ # additional features for all objects
2
+
3
+ # TypeSafe module.
4
+ # This module will be included into Object to provide Object#must_be_kind_of.
5
+ #
6
+ # Usage
7
+ #
8
+ # If you need e.g. an argument be of a specific type, you can simply write
9
+ #
10
+ # def myfunc(arg)
11
+ # arg.must_be_kind_of(MyFancyClass)
12
+ # arg.do_fancy_stuff
13
+ # end
14
+ #
15
+ # This would raise a TypeError in case you somehow passed in a type not matching
16
+ # your MyFancyClass, which would stop your app dead at a point you know. This is
17
+ # probably better than chasing the wrong parameter all the way down your application.
18
+ #
19
+ # However, being dynamic, you might just need to apply a special treatment to your
20
+ # argument in case it's not what you would expect:
21
+ #
22
+ # def my2ndfunc(arg)
23
+ # puts arg.must_be_kind_of(String) {|x| x.to_s}
24
+ # end
25
+ #
26
+ # In this second case, no exception is raised, but your block is executed. Most
27
+ # important, the return value of your block is returned as value from the method.
28
+ #
29
+ module TypeSafe
30
+ # this class or object must be kind of a given class.
31
+ # Method returns <tt>self</tt> unless it raises a TypeError.
32
+ def must_be_kind_of(clss, &blk)
33
+ if clss.kind_of?(Class)
34
+ matched = if self.kind_of?(Class)
35
+ # classes are expected to "descend" from comparator
36
+ if self != clss
37
+ self.superclass ? self.superclass.must_be_kind_of(clss) : self.kind_of?(clss)
38
+ else
39
+ true
40
+ end
41
+ else
42
+ # objects are expected to implement comparator
43
+ self.kind_of?(clss)
44
+ end
45
+
46
+ unless matched
47
+ if blk
48
+ return yield self.class
49
+ else
50
+ raise TypeError.new("#{self} must be of type #{clss}")
51
+ end
52
+ end
53
+ elsif clss.kind_of?(Module)
54
+ # check if the given module is included
55
+ unless self.included_modules.include?(clss)
56
+ if blk
57
+ return yield self.class
58
+ else
59
+ raise TypeError.new("#{self} does not include module #{clss}")
60
+ end
61
+ end
62
+ else
63
+ raise "class to check #{self} for must be a Class or Module, but is #{clss}"
64
+ end
65
+ self
66
+ end
67
+ alias must_be_a must_be_kind_of
68
+
69
+ # this class or object must be kind of a given class or nil.
70
+ # This method is probably a bad idea, so it will be removed.
71
+ def must_be_nil_or_kind_of(clss, &blk)
72
+ self.kind_of?(NilClass) ? true : must_be_kind_of(clss, &blk)
73
+ end
74
+ end#TypeSafe
75
+
76
+ Object.send :include, TypeSafe
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'typesafe'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
10
+
11
+ # a class hierarchy to check
12
+
13
+ # plain module
14
+ module ModA
15
+ def moda_method ; end
16
+ end
17
+ # plain class
18
+ class ClassA
19
+ end
20
+ # class derived from ClassA
21
+ class ClassB < ClassA
22
+ end
23
+ # plain class, extends module ModA, thereby descending ModA
24
+ class ClassC
25
+ extend ModA
26
+ end
27
+ # plain class, includes module ModA, thereby not descending ModA
28
+ class ClassD
29
+ include ModA
30
+ end
31
+
@@ -0,0 +1,16 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe TypeSafe, ".must_be_kind_of" do
4
+ it "should recognize Class as kind of Object" do
5
+ lambda{Class.must_be_kind_of(Object)}.should_not raise_error(TypeError)
6
+ end
7
+ it "should recognize Object as kind of Class" do
8
+ lambda{Object.must_be_kind_of(Class)}.should_not raise_error(TypeError)
9
+ end
10
+ it "should recognize Fixnum as kind of Integer" do
11
+ lambda{Fixnum.must_be_kind_of(Integer)}.should_not raise_error(TypeError)
12
+ end
13
+ it "should recognize Bignum as kind of Integer" do
14
+ lambda{Bignum.must_be_kind_of(Integer)}.should_not raise_error(TypeError)
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe TypeSafe, ".must_be_kind_of" do
4
+ it "should recognize ClassA as kind of Object" do
5
+ lambda{ClassA.must_be_kind_of(Object)}.should_not raise_error(TypeError)
6
+ end
7
+ it "should recognize ClassB as kind of ClassA" do
8
+ lambda{ClassB.must_be_kind_of(ClassA)}.should_not raise_error(TypeError)
9
+ end
10
+ it "should not recognize ClassC as kind of ClassA" do
11
+ lambda{ClassC.must_be_kind_of(ClassA)}.should raise_error(TypeError)
12
+ end
13
+ it "should not recognize ClassC as kind of ModA" do
14
+ lambda{ClassC.must_be_kind_of(ModA)}.should raise_error(TypeError)
15
+ end
16
+ it "should recognize ClassD as kind of ModA" do
17
+ lambda{ClassD.must_be_kind_of(ModA)}.should_not raise_error(TypeError)
18
+ end
19
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ {:must_be_kind_of => false, :must_be_a => false, :must_be_nil_or_kind_of => true}.each do |method, nil_allowed|
4
+ describe TypeSafe, ".#{method}" do
5
+ it "should be included in Object class" do
6
+ Object.new.should respond_to(method)
7
+ end
8
+ it "should recognize a simple type" do
9
+ lambda{1.send method, Fixnum}.should_not raise_error(TypeError)
10
+ end
11
+ it "should recognize parent classes" do
12
+ lambda{self.send method, Object}.should_not raise_error(TypeError)
13
+ end
14
+ it "should raise a TypeError for mismatching types" do
15
+ lambda{"1".send method, Fixnum}.should raise_error(TypeError)
16
+ end
17
+ it "should not raise a TypeError for mismatching types if a block was given" do
18
+ lambda{"1".send method, Fixnum do |clss|
19
+ clss.should be_a Class
20
+ clss.should == String
21
+ end}.should_not raise_error(TypeError)
22
+ end
23
+ it "should execute the given block for mismatching types" do
24
+ executed = false
25
+ lambda{"1".send method, Fixnum do |clss|
26
+ clss.should be_a Class
27
+ clss.should == String
28
+ executed = true
29
+ end}.should_not raise_error(TypeError)
30
+ executed.should == true
31
+ end
32
+ it "should not execute the given block for matching types" do
33
+ executed = false
34
+ lambda{1.send method, Fixnum do |clss|
35
+ clss.should be_a Class
36
+ clss.should == Fixnum
37
+ executed = true
38
+ end}.should_not raise_error(TypeError)
39
+ executed.should == false
40
+ end
41
+
42
+ it "should raise a RuntimeError if the given match is not a Class" do
43
+ lambda{"1".send method, "Fixnum"}.should raise_error(RuntimeError)
44
+ lambda{"1".send method, "Fixnum"}.should_not raise_error(TypeError)
45
+ end
46
+
47
+ it "should #{nil_allowed ? 'not ' : ''}raise a TypeError for mismatching types on nil" do
48
+ lambda{nil.send method, Fixnum}.send nil_allowed ? :should_not : :should, raise_error(TypeError)
49
+ end
50
+
51
+ it "should recognize included modules" do
52
+ module TestModule ; end
53
+ class TestClass
54
+ include TestModule
55
+ end
56
+ o = TestClass.new
57
+ lambda{o.send method, TestModule}.should_not raise_error(TypeError)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{typesafe}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Till Salzer"]
12
+ s.date = %q{2009-08-30}
13
+ s.description = %q{Simplifies typesafe development by providing the additional
14
+ methods Object#must_by_kind_of ad Object#must_be_a.}
15
+ s.email = %q{till.salzer@googlemail.com}
16
+ s.extra_rdoc_files = [
17
+ "LICENSE",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "features/step_definitions/typesafe_steps.rb",
28
+ "features/support/env.rb",
29
+ "features/typesafe.feature",
30
+ "lib/typesafe.rb",
31
+ "spec/spec_helper.rb",
32
+ "spec/typesafe_class_coreclasses_spec.rb",
33
+ "spec/typesafe_class_spec.rb",
34
+ "spec/typesafe_spec.rb",
35
+ "typesafe.gemspec"
36
+ ]
37
+ s.homepage = %q{http://github.com/tsalzer/typesafe}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.5}
41
+ s.summary = %q{provides Object#must_be_kind_of for typesafe development}
42
+ s.test_files = [
43
+ "spec/spec_helper.rb",
44
+ "spec/typesafe_class_coreclasses_spec.rb",
45
+ "spec/typesafe_class_spec.rb",
46
+ "spec/typesafe_spec.rb"
47
+ ]
48
+
49
+ if s.respond_to? :specification_version then
50
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
54
+ s.add_development_dependency(%q<rspec>, [">= 0"])
55
+ s.add_development_dependency(%q<cucumber>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 0"])
58
+ s.add_dependency(%q<cucumber>, [">= 0"])
59
+ end
60
+ else
61
+ s.add_dependency(%q<rspec>, [">= 0"])
62
+ s.add_dependency(%q<cucumber>, [">= 0"])
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: typesafe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Till Salzer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-11-03 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: cucumber
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: |-
36
+ Simplifies typesafe development by providing the additional
37
+ methods Object#must_by_kind_of ad Object#must_be_a.
38
+ email: till.salzer@googlemail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - features/step_definitions/typesafe_steps.rb
54
+ - features/support/env.rb
55
+ - features/typesafe.feature
56
+ - lib/typesafe.rb
57
+ - spec/spec_helper.rb
58
+ - spec/typesafe_class_coreclasses_spec.rb
59
+ - spec/typesafe_class_spec.rb
60
+ - spec/typesafe_spec.rb
61
+ - typesafe.gemspec
62
+ has_rdoc: true
63
+ homepage: http://github.com/tsalzer/typesafe
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ version:
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.5
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: provides Object#must_be_kind_of for typesafe development
90
+ test_files:
91
+ - spec/spec_helper.rb
92
+ - spec/typesafe_class_coreclasses_spec.rb
93
+ - spec/typesafe_class_spec.rb
94
+ - spec/typesafe_spec.rb