isy 0.1.2 → 0.2.0
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.
- checksums.yaml +4 -4
- data/README.md +24 -4
- data/lib/isy/methods.rb +37 -2
- data/lib/isy/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3b170f040c49a3262f8f3138321752e89f22dcf
|
4
|
+
data.tar.gz: 9e08893688c801d61ccac0de3ac2c8b6c61d81ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 874b089ca5916fbbfaca5d55fcd20225890fefc2ccf97368f36f511658ad9eb19f491d06c55edf599fbe5dbae5903e6c0a14fad731a4536e63be807e9dcfcc4b
|
7
|
+
data.tar.gz: f5178fa77edda673fb4a6cae89cfa8105407900bc31b4a86bcc4bc3d70f3493e60fac6a8191ac3233164170c2b5fce9334a4679165850fd9109934bbd5837ff8
|
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
# Isy
|
2
2
|
|
3
|
+
[](https://travis-ci.org/acuppy/isy)
|
4
|
+
[](https://codeclimate.com/github/acuppy/isy)
|
5
|
+
|
3
6
|
Guard clauses for argument type assertions can be messy:
|
4
7
|
|
5
8
|
```ruby
|
6
9
|
def fullname segments=[]
|
7
|
-
raise 'User#fullname expects 'segments' to be an Array' if segments.is_a? Array
|
10
|
+
raise ArgumentError, 'User#fullname expects 'segments' to be an Array' if segments.is_a? Array
|
8
11
|
# ...
|
9
12
|
end
|
10
13
|
```
|
@@ -35,20 +38,37 @@ The second argument is the type.
|
|
35
38
|
If the subject doesn't match the provided type, then it raises a formatted exception describing
|
36
39
|
what argument *value* was a type mismatch (as an `Isy::ArgumentTypeMismatch` exception).
|
37
40
|
|
41
|
+
Alternatively, you can use the predicate version for type assertion:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
def fullname segments=[]
|
45
|
+
if isy? segments, Array
|
46
|
+
# passes
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
In the previous example, the interface is the same: subject, arguments,
|
52
|
+
optional block. The difference is that if the assertion fails, it will
|
53
|
+
catch the `Isy::ArgumentTypeMismatch` exception and return `false`.
|
54
|
+
|
38
55
|
## Usage with an operation
|
39
56
|
|
40
57
|
Optionally, in place of a type as the second argument, you can pass a block, and perform
|
41
58
|
a more complex comparison operation:
|
42
59
|
|
43
60
|
```ruby
|
44
|
-
|
45
|
-
|
46
|
-
|
61
|
+
def fullname segments
|
62
|
+
isy segments { |seg| seg.length == 3 }
|
63
|
+
# ...
|
64
|
+
end
|
47
65
|
```
|
48
66
|
|
49
67
|
As illustrated above, `isy` yields to the operation the first argument (segments). The expectation
|
50
68
|
is that the value returned by the operation (block) is a boolen (true => passes, false => failed).
|
51
69
|
|
70
|
+
*Note: predicate version has the same support*
|
71
|
+
|
52
72
|
## Performance
|
53
73
|
|
54
74
|
It's not great against a more "native" implementation; but, where you lose a little
|
data/lib/isy/methods.rb
CHANGED
@@ -40,14 +40,49 @@ module Isy
|
|
40
40
|
'Object#isy requires either a type or evaluation block'
|
41
41
|
end
|
42
42
|
|
43
|
-
evaluation ||=
|
43
|
+
evaluation ||= ->(s) { s.is_a? args[0] }
|
44
|
+
is_valid = !!(evaluation.call subject)
|
44
45
|
|
45
|
-
unless
|
46
|
+
unless is_valid
|
46
47
|
raise Isy::ArgumentTypeMismatch.new(
|
47
48
|
subject: subject,
|
48
49
|
caller_method: caller_locations(1,1)[0].label
|
49
50
|
)
|
50
51
|
end
|
52
|
+
|
53
|
+
is_valid
|
54
|
+
end
|
55
|
+
|
56
|
+
# Isy::Methods#isy?
|
57
|
+
#
|
58
|
+
# == Usage
|
59
|
+
#
|
60
|
+
# The implementation follows the same workflow as `isy` with one exception (pun intended): returns a boolen.
|
61
|
+
#
|
62
|
+
# def fullname segments
|
63
|
+
# if isy? segments, Array
|
64
|
+
# # passes
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
#
|
68
|
+
# If the subject doesn't match the provided type, then it returns false
|
69
|
+
#
|
70
|
+
# == Usage with an operation
|
71
|
+
#
|
72
|
+
# Optionally, in place of a type as the second argument, you can pass a block, and perform
|
73
|
+
# a more complex comparison operation:
|
74
|
+
#
|
75
|
+
# def fullname segments
|
76
|
+
# isy? segments { |seg| seg.length == 3 }
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# As illustrated above, `isy?` yields to the operation the first argument (segments). The expectation
|
80
|
+
# is that the value returned by the operation (block) is a boolen (true => passes, false => failed).
|
81
|
+
#
|
82
|
+
def isy? subject, *args, &evaluation
|
83
|
+
isy subject, *args, &evaluation
|
84
|
+
rescue ArgumentTypeMismatch
|
85
|
+
false
|
51
86
|
end
|
52
87
|
end
|
53
88
|
end
|
data/lib/isy/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: isy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Cuppy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|