expects 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b70f9a3cc6b4a1f40da2d606c1b8b2ccc22c3f0c
4
- data.tar.gz: 91118b1e631cdda1be9e8c7c0c98dc17f58dadc1
3
+ metadata.gz: b50c094da2c7d2537282c168b355c2b43ed2c429
4
+ data.tar.gz: 769948b439dff5937954fd746270da88113420d4
5
5
  SHA512:
6
- metadata.gz: c37b6a3a73079fdf23cc6cea703a9bc01241652e1be1cd81bcd4612c81a0a199b3a39e90dafffcf6f4bb2b1b79d6d9a0c11a71825642899b792da7c248198687
7
- data.tar.gz: f3f3c461634ecc8f0752dcb47e149236e6570488b2fe22492225a0dbce6280c9c0412378872c376a67ff80fbf01f05c220fbc8e74fbc85adc5d60643f576edb5
6
+ metadata.gz: 7444dcdeb60ab0d81d0d42d8a63ada0fa90753d668725a0eb0cbe608a934c09f4c466829aac5d963d9c9cab41f6af2b6d2fe14f98c1b755bffc8cf62daba7a21
7
+ data.tar.gz: 03a368aa04c7e2446fc885edd972cc2cfd79cbc65508a414b1759360daa4fa43b064140ab18cfb93daa17cd80f7d27f300cfaa9cd9c37f8488108a5fa1446cea
data/README.md CHANGED
@@ -29,6 +29,25 @@ class YourClass
29
29
  end
30
30
  end
31
31
  ```
32
+ You can pass an array instead of one class to accept multiple types e.g.
33
+
34
+ ```ruby
35
+ expects number, [Fixnum, Float]
36
+ ```
37
+
38
+ If you try to pass anything other than a string to YourClass.new expects will raise an exception which you can catch like so:
39
+
40
+ ```ruby
41
+ begin
42
+ YourClass.new(1234)
43
+ rescue UnexpectedInput => e
44
+ e.message # Expected 1234 to be String
45
+ e.subject # 1234
46
+ e.expected # [String]
47
+ end
48
+ ```
49
+
50
+ All exceptions thrown by expects are instances of `UnexpectedInput`
32
51
 
33
52
  ## Contributing
34
53
 
@@ -1,3 +1,3 @@
1
1
  module Expects
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/expects.rb CHANGED
@@ -4,10 +4,29 @@ require 'expects/version'
4
4
  require 'expects/error'
5
5
 
6
6
  module Expects
7
- def expects(subject, objects)
8
- objects = [objects] unless objects.is_a? Array
9
- raise UnexpectedInput.new(subject, objects) unless objects.include? subject.class
7
+ module ClassMethods
8
+ def expects(subject, objects)
9
+ objects = [objects] unless objects.is_a? Array
10
+ raise UnexpectedInput.new(subject, objects) unless objects.include? subject.class
11
+ end
12
+
13
+ def reject(subject, objects)
14
+ objects = [objects] unless objects.is_a? Array
15
+ raise UnexpectedInput.new(subject, objects) if objects.include? subject.class
16
+ end
10
17
  end
11
18
 
12
- private :expects
19
+ def self.included(klass)
20
+ klass.extend ClassMethods
21
+ end
22
+
23
+ def expects(*args)
24
+ self.class.expects(*args)
25
+ end
26
+
27
+ def reject(*args)
28
+ self.class.reject(*args)
29
+ end
30
+
31
+ private :expects, :reject
13
32
  end
data/spec/expects_spec.rb CHANGED
@@ -5,10 +5,18 @@ describe Expects do
5
5
  Class.new do
6
6
  include Expects
7
7
 
8
+ def self.class_method(input)
9
+ expects input, Fixnum
10
+ end
11
+
8
12
  def initialize(message)
9
13
  expects message, String
10
14
  end
11
15
 
16
+ def reject_test(input)
17
+ reject input, String
18
+ end
19
+
12
20
  def a_method
13
21
  true
14
22
  end
@@ -36,4 +44,30 @@ describe Expects do
36
44
  lambda { array_class.new(["foo"]) }.should raise_exception
37
45
  array_class.new("This").a_method.should be_true
38
46
  end
47
+
48
+ it "should be usable in class methods" do
49
+ begin
50
+ test_class.class_method("foo")
51
+ rescue UnexpectedInput => e
52
+ e.subject.should eq "foo"
53
+ e.expected.should eq [Fixnum]
54
+ end
55
+ end
56
+
57
+ it "should reject items aswell" do
58
+ begin
59
+ test_class.new("bar").reject_test("foo")
60
+ rescue UnexpectedInput => e
61
+ e.subject.should eq "foo"
62
+ e.expected.should eq [String]
63
+ end
64
+ end
65
+
66
+ it "should have made expects a private method" do
67
+ begin
68
+ test_class.new("bar").expects
69
+ rescue => e
70
+ e.message.should =~ /private method/
71
+ end
72
+ end
39
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: expects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Laycock
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-17 00:00:00.000000000 Z
11
+ date: 2013-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler