izzy 3.0.0 → 3.1.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/CHANGELOG.md +10 -1
- data/README.md +33 -31
- data/lib/izzy/version.rb +1 -1
- data/lib/izzy.rb +20 -0
- data/spec/izzy_array_spec.rb +37 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b1d3b907301db63a45af7c7a18a9aebd0158c12
|
4
|
+
data.tar.gz: ceff0672f2abc1784bd6516e274bec640ebf6748
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 586b68549ebc90aeee1448e544e35825130c01320361612361abb925e41d434a75bed7d31173b2b7bd14d910c9ca5d54e83448c52be13581b7433860a7e46fd9
|
7
|
+
data.tar.gz: 216cfa83cfce99dbf5c41d032ce90c2e10b8b39159e6b9a2946a9edf2a3c58422fabb3ab64a564b133db80515562090fc8e3b41bfdd258444d02d60e4c9409c2
|
data/CHANGELOG.md
CHANGED
@@ -35,4 +35,13 @@ Methods changed:
|
|
35
35
|
* matches_all?
|
36
36
|
* matches_any?
|
37
37
|
* matches_none?
|
38
|
-
* [private] matcher_check
|
38
|
+
* [private] matcher_check
|
39
|
+
|
40
|
+
## v 3.1.0 - Multi Object Matchers via Array
|
41
|
+
|
42
|
+
Added a module to be mixed into Array, allowing for encapsulation of function evaluation on all objects in an Array.
|
43
|
+
|
44
|
+
Methods added:
|
45
|
+
* all_are
|
46
|
+
* any_are
|
47
|
+
* none_are
|
data/README.md
CHANGED
@@ -4,10 +4,9 @@
|
|
4
4
|
[](https://codeclimate.com/github/baweaver/izzy)
|
5
5
|
[](https://coveralls.io/r/baweaver/izzy?branch=master)
|
6
6
|
|
7
|
-
|
7
|
+
## Izzy Module
|
8
8
|
|
9
|
-
|
10
|
-
Let's take our class, Person:
|
9
|
+
To demo Izzy, let's take our class, Person:
|
11
10
|
|
12
11
|
```ruby
|
13
12
|
class Person
|
@@ -20,29 +19,17 @@ class Person
|
|
20
19
|
@sex = sex
|
21
20
|
end
|
22
21
|
|
23
|
-
def older_than_18
|
24
|
-
@age > 18
|
25
|
-
end
|
22
|
+
def older_than_18?; @age > 18 end
|
26
23
|
|
27
|
-
def younger_than_18
|
28
|
-
@age < 18
|
29
|
-
end
|
24
|
+
def younger_than_18?; @age < 18 end
|
30
25
|
|
31
|
-
def male
|
32
|
-
@sex == 'm'
|
33
|
-
end
|
26
|
+
def male?; @sex == 'm'; end
|
34
27
|
|
35
|
-
def female
|
36
|
-
@sex == 'f'
|
37
|
-
end
|
28
|
+
def female?; @sex == 'f' end
|
38
29
|
|
39
|
-
def me
|
40
|
-
@name == 'brandon' && @sex == 'm'
|
41
|
-
end
|
30
|
+
def me?; @name == 'brandon' && @sex == 'm' end
|
42
31
|
|
43
|
-
def geek
|
44
|
-
@name == 'brandon'
|
45
|
-
end
|
32
|
+
def geek?; @name == 'brandon' end
|
46
33
|
end
|
47
34
|
```
|
48
35
|
|
@@ -53,9 +40,9 @@ brandon = Person.new('brandon', 23, 'm')
|
|
53
40
|
|
54
41
|
...and do some comparisons!
|
55
42
|
```ruby
|
56
|
-
brandon.all_of? :older_than_18
|
57
|
-
brandon.none_of? :younger_than_18
|
58
|
-
brandon.any_of? :male
|
43
|
+
brandon.all_of? :older_than_18?, :male?, :me?, :geek? # => true
|
44
|
+
brandon.none_of? :younger_than_18?, :female? # => true
|
45
|
+
brandon.any_of? :male?, :female?, :geek? # => true
|
59
46
|
```
|
60
47
|
|
61
48
|
Maybe boolean comparisons aren't your cup of tea. Izzy has you covered my friend:
|
@@ -73,19 +60,21 @@ brandon.matches_all? name: String, age: Integer
|
|
73
60
|
Why stop there? Need a bit more power? We have Lambdas for that!
|
74
61
|
|
75
62
|
```ruby
|
76
|
-
|
63
|
+
longer_than_3 = -> n { n.length > 3 }
|
64
|
+
is_odd = -> a { a.odd? }
|
65
|
+
|
66
|
+
brandon.matches_all? name: longer_than_3, age: is_odd # => true
|
77
67
|
```
|
78
68
|
|
79
69
|
....or let's push it further for some interesting results:
|
80
70
|
|
81
71
|
```ruby
|
72
|
+
longer_than_5 = -> n { n.length > 5 }
|
73
|
+
greater_than_20 = -> a { a > 20 }
|
74
|
+
|
82
75
|
brandon.matches_all?(
|
83
|
-
name: [
|
84
|
-
|
85
|
-
],
|
86
|
-
age: [
|
87
|
-
(20..30), -> a { a > 20 }
|
88
|
-
]
|
76
|
+
name: [/br/, /an/, longer_than_5],
|
77
|
+
age: [(20..30), greater_than_20]
|
89
78
|
)
|
90
79
|
|
91
80
|
# => true
|
@@ -94,6 +83,19 @@ brandon.matches_all?(
|
|
94
83
|
|
95
84
|
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!
|
96
85
|
|
86
|
+
## Izzy Array Module
|
87
|
+
|
88
|
+
But wait! There's more! IzzyArray allows you a few more tricks on top of that!
|
89
|
+
```ruby
|
90
|
+
class Array; include IzzyArray end
|
91
|
+
|
92
|
+
[0,0,0].all_are :zero? # => true
|
93
|
+
[0,nil,'foo'].any_are :zero? # => true
|
94
|
+
[0,0,0].none_are :nil? # => true
|
95
|
+
```
|
96
|
+
|
97
|
+
Combine with rails :present?, :empty?, and various other methods and you have some interesting results! This one will get some more power with experimentation.
|
98
|
+
|
97
99
|
## Installation
|
98
100
|
|
99
101
|
Add this line to your application's Gemfile:
|
data/lib/izzy/version.rb
CHANGED
data/lib/izzy.rb
CHANGED
@@ -39,3 +39,23 @@ module Izzy
|
|
39
39
|
}
|
40
40
|
end
|
41
41
|
end
|
42
|
+
|
43
|
+
module IzzyArray
|
44
|
+
def all_are(m)
|
45
|
+
self.all?(&object_block(m))
|
46
|
+
end
|
47
|
+
|
48
|
+
def any_are(m)
|
49
|
+
self.any?(&object_block(m))
|
50
|
+
end
|
51
|
+
|
52
|
+
def none_are(m)
|
53
|
+
self.none?(&object_block(m))
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def object_block(m)
|
59
|
+
-> o { o.respond_to?(m) && o.send(m) }
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
include IzzyArray
|
5
|
+
end
|
6
|
+
|
7
|
+
describe 'IzzyArray' do
|
8
|
+
describe '#all_are' do
|
9
|
+
it 'returns true if all objects match the method' do
|
10
|
+
[0,0,0,0].all_are :zero?
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns false if any objects do not match the method' do
|
14
|
+
[0,1,'foo', nil].all_are :zero?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#any_are' do
|
19
|
+
it 'returns true if any objects match the method' do
|
20
|
+
[0,1,'foo', nil].any_are :zero?
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'returns false if no objects match the method' do
|
24
|
+
[nil, nil, nil].any_are :zero?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#none_are' do
|
29
|
+
it 'returns true if no objects match the method' do
|
30
|
+
[nil, nil, nil].none_are :zero?
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'returns false if any objects match the method' do
|
34
|
+
[nil, nil, nil].none_are :nil?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
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: 3.
|
4
|
+
version: 3.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-02-
|
11
|
+
date: 2014-02-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- izzy.gemspec
|
72
72
|
- lib/izzy.rb
|
73
73
|
- lib/izzy/version.rb
|
74
|
+
- spec/izzy_array_spec.rb
|
74
75
|
- spec/izzy_spec.rb
|
75
76
|
- spec/spec_helper.rb
|
76
77
|
homepage: http://www.github.com/baweaver/izzy
|
@@ -98,5 +99,6 @@ signing_key:
|
|
98
99
|
specification_version: 4
|
99
100
|
summary: Gives you methods to mitigate long conditionals
|
100
101
|
test_files:
|
102
|
+
- spec/izzy_array_spec.rb
|
101
103
|
- spec/izzy_spec.rb
|
102
104
|
- spec/spec_helper.rb
|