safe-me 0.9.0 → 0.9.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/README.rdoc CHANGED
@@ -40,7 +40,7 @@ The DSL ist very simple. Let me explain on an example:
40
40
  end
41
41
  end
42
42
 
43
- For now there are 4 kinds of possible argument types.
43
+ For now there are 5 kinds of possible argument types.
44
44
 
45
45
  - Classes
46
46
 
@@ -52,18 +52,21 @@ Hash: for hashes you can pass Hash.ofType(Class, Class) where the first class is
52
52
 
53
53
  - Nilable
54
54
 
55
- If you also want to be able to pass nil to a method, if the argument is optional for example, you can pass nilable(Class) as type
55
+ If you also want to be able to pass nil to a method, if the argument is optional for example, you can pass nilable(Class) as type.
56
56
 
57
57
  - RespondsTo
58
58
 
59
- If you only want to be sure, that an argument responds to some methods you can pass responds_to(*methods) where *methods should be the method names as symbols
59
+ If you only want to be sure, that an argument responds to some methods you can pass responds_to(*methods) where *methods should be the method names as symbols.
60
60
 
61
61
 
62
62
  - QuacksLike
63
63
 
64
64
  If you need the same responds_to on multiple methods it can be useful to create a clean room class, that contains those methods and call quacks_like(Class) instead.
65
- quacks_like ensures, that every public instance method of this class is also available on the object, that gets passed to the method
65
+ quacks_like ensures, that every public instance method of this class is also available on the object, that gets passed to the method.
66
66
 
67
+ - Unchecked
68
+
69
+ If you have methods where some arguments should not be checked for their type, you can pass unchecked as type.
67
70
 
68
71
  == License:
69
72
 
@@ -1,4 +1,4 @@
1
- class Class
1
+ Class.class_eval do
2
2
  def type_of? obj
3
3
  obj.kind_of? self
4
4
  end
@@ -1,6 +1,6 @@
1
1
  require 'safe-me/var_args'
2
2
 
3
- class Module
3
+ Module.class_eval do
4
4
  attr_accessor :method_types
5
5
  def safe_method(name, safer)
6
6
  alias_method "__typeunsafe_#{name}__", name
data/lib/safe-me.rb CHANGED
@@ -16,7 +16,7 @@ end
16
16
 
17
17
  module SafeMe
18
18
  def self.init
19
- Dir.glob("safe/*_safe.rb").each do |f|
19
+ Dir.glob("safe/**/*_safe.rb").each do |f|
20
20
  SafeLoader.instance_eval File.read(f)
21
21
  end
22
22
  end
@@ -1,6 +1,7 @@
1
1
  require 'safe-me/duck_type'
2
2
  require 'safe-me/nilable_type'
3
3
  require 'safe-me/responds_to'
4
+ require 'safe-me/unchecked_type'
4
5
 
5
6
  module SafeMe
6
7
  class TypeSafer
@@ -35,6 +36,10 @@ module SafeMe
35
36
  def responds_to *methods
36
37
  RespondsTo.new *methods
37
38
  end
39
+
40
+ def unchecked
41
+ UncheckedType.instance
42
+ end
38
43
 
39
44
  private :quacks_like, :argument
40
45
  end
@@ -0,0 +1,15 @@
1
+ require 'singleton'
2
+
3
+ module SafeMe
4
+ class UncheckedType
5
+ include Singleton
6
+
7
+ def type_of? obj
8
+ true
9
+ end
10
+
11
+ def to_s
12
+ "unchecked"
13
+ end
14
+ end
15
+ end
@@ -1,6 +1,6 @@
1
1
  module SafeMe
2
2
  unless defined?(SafeMe::VERSION)
3
- VERSION = '0.9.0'
3
+ VERSION = '0.9.1'
4
4
  LIBDIR = File.expand_path(File.dirname(__FILE__) + '/../../lib')
5
5
  end
6
6
  end
@@ -1,4 +1,5 @@
1
- require 'spec/classes/foo'
1
+ require File.expand_path(File.dirname(__FILE__)) + '/spec/classes/foo'
2
+
2
3
 
3
4
  safe Foo do
4
5
  for_method :a_method do
@@ -17,4 +18,9 @@ safe Foo do
17
18
  for_method :baz do
18
19
  argument responds_to :bar
19
20
  end
21
+
22
+ for_method :unchecked_method do
23
+ argument Integer
24
+ argument unchecked
25
+ end
20
26
  end
@@ -14,4 +14,8 @@ class Foo
14
14
  def baz a
15
15
  "#{a} should respond to :bar"
16
16
  end
17
+
18
+ def unchecked_method a,b
19
+ "#{b} should be unchecked"
20
+ end
17
21
  end
@@ -0,0 +1,21 @@
1
+ require 'safe-me'
2
+
3
+ describe Array do
4
+ describe "an array filled with integers" do
5
+ it "should be kind_of? Array.ofType(Integer)" do
6
+ [1,2,3].kind_of?(Array.ofType(Integer)).should be_true
7
+ end
8
+ end
9
+
10
+ describe "an array filled with string" do
11
+ it "should not be kind_of? Array.ofType(Integer)" do
12
+ ["a", "b", "c"].kind_of?(Array.ofType(Integer)).should be_false
13
+ end
14
+ end
15
+
16
+ describe "an array filled with objects of different types" do
17
+ it "should not be kind_of? Array.ofType(Integer)" do
18
+ [1,2,"a"].kind_of?(Array.ofType(Integer)).should be_false
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'safe-me'
2
+
3
+ describe Hash do
4
+ describe "a hash filled with keys of String and objects of type Integer" do
5
+ it "should be kind_of? Hash.ofType(String, Integer)" do
6
+ {"a" => 1, "b" => 2, "c" => 3}.kind_of?(Hash.ofType(String, Integer)).should be_true
7
+ end
8
+ end
9
+
10
+ describe "a hash filled with keys of String and objects of type String" do
11
+ it "should not be kind_of? Hash.ofType(String, Integer)" do
12
+ {"a" => "1", "b" => "2", "c" => "3"}.kind_of?(Hash.ofType(String, Integer)).should be_false
13
+ end
14
+ end
15
+
16
+ describe "a hash filled with keys and objects of different types" do
17
+ it "should not be kind_of? Hash.ofType(String, Integer)" do
18
+ {"a" => 1, "b" => 2, 3 => "3"}.kind_of?(Hash.ofType(String, Integer)).should be_false
19
+ end
20
+ end
21
+ end
@@ -1,4 +1,4 @@
1
- require 'spec/classes/foo'
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../classes/foo')
2
2
  require 'safe-me'
3
3
 
4
4
  SafeMe.init
@@ -35,5 +35,14 @@ describe SafeMe do
35
35
  lambda{Foo.new.baz 1}.should raise_error(ArgumentError, "for argument 1 expected type RespondsTo(bar)")
36
36
  end
37
37
  end
38
+
39
+ describe "a method with an argument that should be unchecked" do
40
+ it "should allow arguments of any type for that argument" do
41
+ lambda{Foo.new.unchecked_method 1, "a"}.should_not raise_error(ArgumentError)
42
+ lambda{Foo.new.unchecked_method 1, 1}.should_not raise_error(ArgumentError)
43
+ lambda{Foo.new.unchecked_method 1, nil}.should_not raise_error(ArgumentError)
44
+ lambda{Foo.new.unchecked_method 1, [1,2,3,"a"]}.should_not raise_error(ArgumentError)
45
+ end
46
+ end
38
47
  end
39
48
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: safe-me
3
3
  version: !ruby/object:Gem::Version
4
- hash: 59
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 9
9
- - 0
10
- version: 0.9.0
8
+ - 1
9
+ version: 0.9.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Dario Rexin
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2010-10-22 00:00:00 +02:00
17
+ date: 2010-10-23 00:00:00 +02:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -54,12 +52,15 @@ files:
54
52
  - lib/safe-me/responds_to.rb
55
53
  - lib/safe-me/safe_loader.rb
56
54
  - lib/safe-me/type_safer.rb
55
+ - lib/safe-me/unchecked_type.rb
57
56
  - lib/safe-me/var_args.rb
58
57
  - lib/safe-me/version.rb
59
58
  - lib/safe-me.rb
60
- - spec/classes/Foo.rb
59
+ - spec/classes/foo.rb
60
+ - spec/extensions/array_spec.rb
61
+ - spec/extensions/hash_spec.rb
61
62
  - spec/safe-me/safe-me_spec.rb
62
- - safe/Foo_safe.rb
63
+ - safe/foo_safe.rb
63
64
  - README.rdoc
64
65
  has_rdoc: true
65
66
  homepage: http://github.com/drexin/safe-me
@@ -75,7 +76,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
76
  requirements:
76
77
  - - ">="
77
78
  - !ruby/object:Gem::Version
78
- hash: 3
79
79
  segments:
80
80
  - 0
81
81
  version: "0"
@@ -84,7 +84,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
84
  requirements:
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- hash: 3
88
87
  segments:
89
88
  - 0
90
89
  version: "0"