interrogate 0.0.1 → 0.0.2a
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +7 -0
- data/README.md +9 -0
- data/interrogate.gemspec +1 -1
- data/lib/interrogate/version.rb +1 -1
- data/lib/interrogate.rb +11 -12
- data/spec/interrogate/interrogate_spec.rb +12 -0
- metadata +15 -11
data/MIT-LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2012 Morgan Brown
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# Interrogate [![Build Status](https://secure.travis-ci.org/mhgbrown/interrogate.png)](http://travis-ci.org/mhgbrown/interrogate)
|
2
2
|
Interrogate attempts to bring Scheme-like class predication to Ruby. It provides an alternate syntax using `Module#===`.
|
3
3
|
|
4
|
+
>> require "interrogate"
|
4
5
|
>> String?("Hello")
|
5
6
|
>> trues
|
6
7
|
>> Symbol?(:World)
|
@@ -15,5 +16,13 @@ You can "interrogate" multiple objects as well:
|
|
15
16
|
>> String?("Hello", "World", "1.0")
|
16
17
|
>> true
|
17
18
|
|
19
|
+
And you can "interrogate" the return value of a block:
|
20
|
+
|
21
|
+
>> num = 1
|
22
|
+
>> String?("Hello") { num }
|
23
|
+
>> false
|
24
|
+
>> String?("Hello") { num.to_s }
|
25
|
+
>> true
|
26
|
+
|
18
27
|
## Feedback
|
19
28
|
Use that Github [issue tracker](https://github.com/mhgbrown/interrogate/issues)!
|
data/interrogate.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Interrogate::VERSION
|
8
8
|
s.authors = ["Morgan Brown"]
|
9
9
|
s.email = ["brown.mhg@gmail.com"]
|
10
|
-
s.homepage = "https://github.com/
|
10
|
+
s.homepage = "https://github.com/discom4rt/interrogate"
|
11
11
|
s.summary = "Scheme-like class predication for Ruby"
|
12
12
|
s.description = "String?(\"Hello\") Interrogate attempts to bring Scheme-like class predication to Ruby. It provides an alternate syntax using Module#==="
|
13
13
|
|
data/lib/interrogate/version.rb
CHANGED
data/lib/interrogate.rb
CHANGED
@@ -8,13 +8,20 @@ module Interrogate
|
|
8
8
|
# Given an interrogatory method name and arguments,
|
9
9
|
# determine which class we are asking about and
|
10
10
|
# ask each arg if it is an instance of this class.
|
11
|
-
def self.interrogate(meth, *args)
|
12
|
-
raise ArgumentError, "#{meth}:
|
11
|
+
def self.interrogate(meth, *args, &block)
|
12
|
+
raise ArgumentError, "#{meth}: requires at least one argument and/or a block" if args.length < 1 && !block
|
13
13
|
klass_name = meth.to_s.tr("?", "")
|
14
14
|
klass = Object.const_get(klass_name)
|
15
|
+
args << yield if block_given?
|
15
16
|
args.inject(true){|bool, obj| bool && klass === obj}
|
16
17
|
end
|
17
18
|
|
19
|
+
# Determine if the given method matches an interrogatory
|
20
|
+
# method name.
|
21
|
+
def self.is_interrogatory?(meth)
|
22
|
+
/^[A-Z].*\?$/ === meth.to_s
|
23
|
+
end
|
24
|
+
|
18
25
|
end
|
19
26
|
|
20
27
|
class Object
|
@@ -24,11 +31,7 @@ class Object
|
|
24
31
|
# Otherwise, perform the default method missing
|
25
32
|
# behavior.
|
26
33
|
def self.method_missing(meth, *args, &block)
|
27
|
-
|
28
|
-
Interrogate.interrogate(meth, *args)
|
29
|
-
else
|
30
|
-
super
|
31
|
-
end
|
34
|
+
Interrogate.is_interrogatory?(meth) ? Interrogate.interrogate(meth, *args, &block) : super
|
32
35
|
end
|
33
36
|
|
34
37
|
# If the method name is interrogatory, then
|
@@ -36,11 +39,7 @@ class Object
|
|
36
39
|
# Otherwise, perform the default method missing
|
37
40
|
# behavior.
|
38
41
|
def method_missing(meth, *args, &block)
|
39
|
-
|
40
|
-
Interrogate.interrogate(meth, *args)
|
41
|
-
else
|
42
|
-
super
|
43
|
-
end
|
42
|
+
Interrogate.is_interrogatory?(meth) ? Interrogate.interrogate(meth, *args, &block) : super
|
44
43
|
end
|
45
44
|
|
46
45
|
end
|
@@ -11,6 +11,18 @@ describe Interrogate do
|
|
11
11
|
String?("Hello", "World", "1.0").should == true
|
12
12
|
end
|
13
13
|
|
14
|
+
it "should allow interrogation of the return value of a block" do
|
15
|
+
num = 2
|
16
|
+
String? { num.to_s }.should == true
|
17
|
+
String? { num }.should == false
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should allow interrogation of the return value of a block and arguments" do
|
21
|
+
num = 2
|
22
|
+
String?("Hello", "World") { num.to_s }.should == true
|
23
|
+
String?("Hello", "World") { num }.should == false
|
24
|
+
end
|
25
|
+
|
14
26
|
it "should raise an error when there are are no arguments provided" do
|
15
27
|
lambda { String?() }.should raise_error(ArgumentError)
|
16
28
|
end
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interrogate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 34
|
5
|
+
prerelease: 5
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
|
9
|
+
- 2
|
10
|
+
- a
|
11
|
+
version: 0.0.2a
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Morgan Brown
|
@@ -15,7 +16,7 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2012-
|
19
|
+
date: 2012-03-29 00:00:00 Z
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rake
|
@@ -58,6 +59,7 @@ files:
|
|
58
59
|
- .gitignore
|
59
60
|
- .travis.yml
|
60
61
|
- Gemfile
|
62
|
+
- MIT-LICENSE
|
61
63
|
- README.md
|
62
64
|
- Rakefile
|
63
65
|
- interrogate.gemspec
|
@@ -65,7 +67,7 @@ files:
|
|
65
67
|
- lib/interrogate/version.rb
|
66
68
|
- spec/interrogate/interrogate_spec.rb
|
67
69
|
- spec/spec_helper.rb
|
68
|
-
homepage: https://github.com/
|
70
|
+
homepage: https://github.com/discom4rt/interrogate
|
69
71
|
licenses: []
|
70
72
|
|
71
73
|
post_install_message:
|
@@ -85,16 +87,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
87
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
88
|
none: false
|
87
89
|
requirements:
|
88
|
-
- - "
|
90
|
+
- - ">"
|
89
91
|
- !ruby/object:Gem::Version
|
90
|
-
hash:
|
92
|
+
hash: 25
|
91
93
|
segments:
|
92
|
-
-
|
93
|
-
|
94
|
+
- 1
|
95
|
+
- 3
|
96
|
+
- 1
|
97
|
+
version: 1.3.1
|
94
98
|
requirements: []
|
95
99
|
|
96
100
|
rubyforge_project: interrogate
|
97
|
-
rubygems_version: 1.8.
|
101
|
+
rubygems_version: 1.8.21
|
98
102
|
signing_key:
|
99
103
|
specification_version: 3
|
100
104
|
summary: Scheme-like class predication for Ruby
|