izzy 2.0.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -1
- data/lib/izzy.rb +25 -3
- data/lib/izzy/version.rb +1 -1
- data/spec/izzy_spec.rb +44 -0
- 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: bc28143ae2179e8bd4ff348fa131d241535184fc
|
4
|
+
data.tar.gz: ae6e5e1126ff074a2b0644472f3fd66e645cfa1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49dc4b9dab2d3f6e0c523311f7e25783280e3eb8f4c3f4f629197ccd5d2ab019364f7377cae2cd478434f6a72a0ff9e8fb3b1598499120abae9a81c7347d2e4f
|
7
|
+
data.tar.gz: c294e594ed285753a459817c5f0495b43b16febe9b30354e1324b4b6cc08fbf4d84cf817a1ef74b25196bcd964e278131ab6af661f666a8d96d05f1c5ec24db9
|
data/README.md
CHANGED
@@ -10,6 +10,9 @@ Let's take our class, Person:
|
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
class Person
|
13
|
+
include Izzy # Make sure to include Izzy!
|
14
|
+
attr_reader :name, :age, :sex
|
15
|
+
|
13
16
|
def initialize(name, age, sex)
|
14
17
|
@name = name
|
15
18
|
@age = age
|
@@ -54,7 +57,15 @@ brandon.none_of?(:younger_than_18, :female) # => true
|
|
54
57
|
brandon.any_of?(:male, :female, :geek) # => true
|
55
58
|
```
|
56
59
|
|
57
|
-
|
60
|
+
Maybe boolean comparisons aren't your cup of tea. Izzy has you covered my friend:
|
61
|
+
```ruby
|
62
|
+
brandon.matches_all? name: /^br/, age: (20..30) # => true
|
63
|
+
brandon.matches_any? name: /br$/, age: (20..30) # => true
|
64
|
+
brandon.matches_none? name: /br&/, age: (30..40) # => true
|
65
|
+
```
|
66
|
+
Izzy compares on === much like a case statement, allowing you to regex and range away! You can even do type checks while you're at it.
|
67
|
+
|
68
|
+
Simple to the point, no more mess of && or || checks for the same object. All you have to do is include Izzy in your object and you're ready to go!
|
58
69
|
|
59
70
|
## Installation
|
60
71
|
|
data/lib/izzy.rb
CHANGED
@@ -2,14 +2,36 @@ require 'izzy/version'
|
|
2
2
|
|
3
3
|
module Izzy
|
4
4
|
def all_of?(*methods)
|
5
|
-
methods.all?
|
5
|
+
methods.all? &method_check
|
6
6
|
end
|
7
7
|
|
8
8
|
def any_of?(*methods)
|
9
|
-
methods.any?
|
9
|
+
methods.any? &method_check
|
10
10
|
end
|
11
11
|
|
12
12
|
def none_of?(*methods)
|
13
|
-
methods.none?
|
13
|
+
methods.none? &method_check
|
14
|
+
end
|
15
|
+
|
16
|
+
def matches_all?(matchers = {})
|
17
|
+
matchers.all? &matcher_check
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches_any?(matchers = {})
|
21
|
+
matchers.any? &matcher_check
|
22
|
+
end
|
23
|
+
|
24
|
+
def matches_none?(matchers = {})
|
25
|
+
matchers.none? &matcher_check
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def method_check
|
31
|
+
-> method { (m = "#{method}?".to_sym) && self.respond_to?(m) && self.send(m) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def matcher_check
|
35
|
+
-> m { (m, val = *m) && self.respond_to?(m) && val === self.send(m) }
|
14
36
|
end
|
15
37
|
end
|
data/lib/izzy/version.rb
CHANGED
data/spec/izzy_spec.rb
CHANGED
@@ -3,6 +3,8 @@ require 'spec_helper'
|
|
3
3
|
class Person
|
4
4
|
include Izzy
|
5
5
|
|
6
|
+
attr_reader :name, :age, :sex
|
7
|
+
|
6
8
|
def initialize(name, age, sex)
|
7
9
|
@name = name
|
8
10
|
@age = age
|
@@ -66,4 +68,46 @@ describe 'Person' do
|
|
66
68
|
expect(@person.none_of? :older_than_18, :female, :geek).to be_false
|
67
69
|
end
|
68
70
|
end
|
71
|
+
|
72
|
+
describe '#matches_all?' do
|
73
|
+
it 'returns true if Person matches all of the conditions' do
|
74
|
+
expect(
|
75
|
+
@person.matches_all?(name: /br/, age: (20..30))
|
76
|
+
).to be_true
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'returns false if Person does not match all conditions' do
|
80
|
+
expect(
|
81
|
+
@person.matches_all?(name: /br/, age: (25..30))
|
82
|
+
).to be_false
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '#matches_any?' do
|
87
|
+
it 'returns true if Person matches all of the conditions' do
|
88
|
+
expect(
|
89
|
+
@person.matches_any?(name: /br/, age: (25..30))
|
90
|
+
).to be_true
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'returns false if Person does not match any conditions' do
|
94
|
+
expect(
|
95
|
+
@person.matches_any?(name: /br$/, age: (25..30))
|
96
|
+
).to be_false
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#matches_none?' do
|
101
|
+
it 'returns true if Person matches all of the conditions' do
|
102
|
+
expect(
|
103
|
+
@person.matches_none?(name: /br$/, age: (25..30))
|
104
|
+
).to be_true
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'returns false if Person does not match any conditions' do
|
108
|
+
expect(
|
109
|
+
@person.matches_none?(name: /br/, age: (25..30))
|
110
|
+
).to be_false
|
111
|
+
end
|
112
|
+
end
|
69
113
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: izzy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandon Weaver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01
|
11
|
+
date: 2014-02-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|