izzy 3.1.0 → 3.2.0

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: 3b1d3b907301db63a45af7c7a18a9aebd0158c12
4
- data.tar.gz: ceff0672f2abc1784bd6516e274bec640ebf6748
3
+ metadata.gz: f887dc8ba689628a9a7fa1998d390283069082f3
4
+ data.tar.gz: afc2eb39a69dc0e1e7f426dd6103ab19947381fc
5
5
  SHA512:
6
- metadata.gz: 586b68549ebc90aeee1448e544e35825130c01320361612361abb925e41d434a75bed7d31173b2b7bd14d910c9ca5d54e83448c52be13581b7433860a7e46fd9
7
- data.tar.gz: 216cfa83cfce99dbf5c41d032ce90c2e10b8b39159e6b9a2946a9edf2a3c58422fabb3ab64a564b133db80515562090fc8e3b41bfdd258444d02d60e4c9409c2
6
+ metadata.gz: 451277cec47478a5b83a416d075eab35ce3a483e5d0567fcdc44915b53562ac25655d4f39e1e3966936dc85ea3634b89c5604ff752eebf455e01ffc9d76176bd
7
+ data.tar.gz: fe4923ee0732d862bed2f8b06bc10b267c7def3ae6de02dbafe03f33c6f9904dfddd1ed7ad7138547121ce49d01eee41046437df569e8e6dfbe24506a6f654a8
data/CHANGELOG.md CHANGED
@@ -44,4 +44,13 @@ Added a module to be mixed into Array, allowing for encapsulation of function ev
44
44
  Methods added:
45
45
  * all_are
46
46
  * any_are
47
- * none_are
47
+ * none_are
48
+
49
+ ## v 3.2.0 - Merge QEnumerable
50
+
51
+ Merged the QEnumerable gem into Izzy because of similarities. Removed respond_to? on checks (not including arrays) in order to skim down the time.
52
+
53
+ Methods added:
54
+ * select_where
55
+ * reject_where
56
+ * find_where
data/README.md CHANGED
@@ -6,58 +6,31 @@
6
6
 
7
7
  ## Izzy Module
8
8
 
9
- To demo Izzy, let's take our class, Person:
9
+ To demo Izzy, let's make a Person:
10
10
 
11
- ```ruby
12
- class Person
13
- include Izzy # Make sure to include Izzy!
14
- attr_reader :name, :age, :sex
15
-
16
- def initialize(name, age, sex)
17
- @name = name
18
- @age = age
19
- @sex = sex
20
- end
21
-
22
- def older_than_18?; @age > 18 end
23
-
24
- def younger_than_18?; @age < 18 end
25
-
26
- def male?; @sex == 'm'; end
27
-
28
- def female?; @sex == 'f' end
29
-
30
- def me?; @name == 'brandon' && @sex == 'm' end
31
-
32
- def geek?; @name == 'brandon' end
33
- end
34
- ```
35
-
36
- So we make our person:
37
11
  ```ruby
38
12
  brandon = Person.new('brandon', 23, 'm')
39
13
  ```
40
14
 
41
- ...and do some comparisons!
42
- ```ruby
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
46
- ```
15
+ ### Matchers
47
16
 
48
- Maybe boolean comparisons aren't your cup of tea. Izzy has you covered my friend:
17
+ Let's do some matching against it:
49
18
  ```ruby
50
19
  brandon.matches_all? name: /^br/, age: (20..30) # => true
51
20
  brandon.matches_any? name: /br$/, age: (20..30) # => true
52
21
  brandon.matches_none? name: /br&/, age: (30..40) # => true
53
22
  ```
54
- 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.
55
23
 
24
+ #### Type Checking
25
+
26
+ Perhaps you want to type check:
56
27
  ```ruby
57
28
  brandon.matches_all? name: String, age: Integer
58
29
  ```
59
30
 
60
- Why stop there? Need a bit more power? We have Lambdas for that!
31
+ #### Lambdas
32
+
33
+ Need a bit more power? We have Lambdas for that!
61
34
 
62
35
  ```ruby
63
36
  longer_than_3 = -> n { n.length > 3 }
@@ -66,6 +39,8 @@ is_odd = -> a { a.odd? }
66
39
  brandon.matches_all? name: longer_than_3, age: is_odd # => true
67
40
  ```
68
41
 
42
+ ### Multi Matchers
43
+
69
44
  ....or let's push it further for some interesting results:
70
45
 
71
46
  ```ruby
@@ -81,11 +56,18 @@ brandon.matches_all?(
81
56
 
82
57
  ```
83
58
 
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!
59
+ #### Boolean Matchers
60
+
61
+ ...and do some comparisons on boolean methods!
62
+ ```ruby
63
+ brandon.all_of? :older_than_18?, :male?, :me?, :geek? # => true
64
+ brandon.none_of? :younger_than_18?, :female? # => true
65
+ brandon.any_of? :male?, :female?, :geek? # => true
66
+ ```
85
67
 
86
68
  ## Izzy Array Module
87
69
 
88
- But wait! There's more! IzzyArray allows you a few more tricks on top of that!
70
+ IzzyArray allows you a few more tricks on top of that!
89
71
  ```ruby
90
72
  class Array; include IzzyArray end
91
73
 
@@ -96,6 +78,61 @@ class Array; include IzzyArray end
96
78
 
97
79
  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
80
 
81
+ ## Izzy Enumerable
82
+
83
+ Let's get ourselves a collection to use:
84
+ ```ruby
85
+ class Object; include Izzy end
86
+
87
+ brandon = Person.new('brandon', 23, 'm')
88
+ john = Person.new('john', 42, 'm')
89
+ jill = Person.new('jill', 31, 'f')
90
+ alice = Person.new('alice', 50, 'f')
91
+ zeke = Person.new('zeke', 18, 'm')
92
+
93
+ people = [brandon, john, jill, alice, zeke]
94
+ ```
95
+
96
+ ### Select Where
97
+
98
+ Select all objects where params match:
99
+ ```ruby
100
+ people.select_where name: /^j/
101
+ # => [john, jill]
102
+
103
+ people.select_where(
104
+ age: [
105
+ (20..35),
106
+ -> a { a.odd? }
107
+ ]
108
+ )
109
+ # => [brandon]
110
+ ```
111
+
112
+ ### Reject Where
113
+
114
+ Reject all objects where params match:
115
+ ```ruby
116
+ people.reject_where name: /^j/
117
+ # => [brandon, alice, zeke]
118
+
119
+ people.reject_where(
120
+ age: [
121
+ (20..35),
122
+ -> a { a.odd? }
123
+ ]
124
+ )
125
+ # => [john, jill, alice, zeke]
126
+ ```
127
+
128
+ ### Find Where
129
+
130
+ Finds the first object where params match:
131
+ ```ruby
132
+ people.find_where name: /^j/, gender: 'm'
133
+ # => john
134
+ ```
135
+
99
136
  ## Installation
100
137
 
101
138
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -3,4 +3,4 @@ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task :default => :spec
data/lib/izzy.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'izzy/version'
2
2
 
3
3
  module Izzy
4
+
5
+ # Boolean Predicate
6
+
4
7
  def all_of?(*methods)
5
8
  methods.all? &method_check
6
9
  end
@@ -13,6 +16,8 @@ module Izzy
13
16
  methods.none? &method_check
14
17
  end
15
18
 
19
+ # Matchers
20
+
16
21
  def matches_all?(matchers = {})
17
22
  matchers.all? &matcher_check(:all?)
18
23
  end
@@ -25,32 +30,46 @@ module Izzy
25
30
  matchers.none? &matcher_check(:any?)
26
31
  end
27
32
 
33
+ # Enumerable Matchers
34
+
35
+ def select_where(matchers = {})
36
+ self.select { |s| s.matches_all? matchers }
37
+ end
38
+
39
+ def reject_where(matchers = {})
40
+ self.reject { |s| s.matches_all? matchers }
41
+ end
42
+
43
+ def find_where(matchers = {})
44
+ self.find { |s| s.matches_all? matchers }
45
+ end
46
+
28
47
  private
29
48
 
30
49
  def method_check
31
- -> m { self.respond_to?(m) && self.send(m) }
50
+ -> m { self.send(m) }
32
51
  end
33
52
 
34
53
  def matcher_check(type = :all?)
35
54
  -> matcher {
36
55
  m, val = *matcher
37
56
  values = val.is_a?(Array) ? val : Array[val]
38
- values.send(type) { |v| self.respond_to?(m) && v === self.send(m) }
57
+ values.send(type) { |v| v === self.send(m) }
39
58
  }
40
59
  end
41
60
  end
42
61
 
43
62
  module IzzyArray
44
63
  def all_are(m)
45
- self.all?(&object_block(m))
64
+ self.all? &object_block(m)
46
65
  end
47
66
 
48
67
  def any_are(m)
49
- self.any?(&object_block(m))
68
+ self.any? &object_block(m)
50
69
  end
51
70
 
52
71
  def none_are(m)
53
- self.none?(&object_block(m))
72
+ self.none? &object_block(m)
54
73
  end
55
74
 
56
75
  private
data/lib/izzy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Izzy
2
- VERSION = '3.1.0'
2
+ VERSION = '3.2.0'
3
3
  end
data/spec/izzy_spec.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Object; include Izzy end
4
+
3
5
  class Person
4
6
  include Izzy
5
7
 
@@ -11,131 +13,168 @@ class Person
11
13
  @sex = sex
12
14
  end
13
15
 
14
- def older_than_18?
15
- @age > 18
16
- end
17
-
18
- def younger_than_18?
19
- @age < 18
20
- end
21
-
22
- def male?
23
- @sex.eql? 'm'
24
- end
25
-
26
- def female?
27
- @sex.eql? 'f'
28
- end
29
-
30
- def me?
31
- @name.eql?('brandon') && @sex.eql?('m')
32
- end
33
-
34
- def geek?
35
- @name.eql? 'brandon'
36
- end
16
+ def older_than_18?; @age > 18 end
17
+ def younger_than_18?; @age < 18 end
18
+ def male?; @sex.eql? 'm' end
19
+ def female?; @sex.eql? 'f' end
20
+ def me?; @name.eql?('brandon') && @sex.eql?('m') end
21
+ def geek?; @name.eql? 'brandon' end
37
22
  end
38
23
 
39
- describe 'Person' do
40
- before(:all) { @person = Person.new('brandon', 23, 'm') }
24
+ brandon = Person.new('brandon', 23, 'm')
25
+ john = Person.new('john', 42, 'm')
26
+ jill = Person.new('jill', 31, 'f')
27
+ alice = Person.new('alice', 50, 'f')
28
+ zeke = Person.new('zeke', 18, 'm')
41
29
 
42
- describe '#all_of?' do
43
- it 'returns true if Person matches all of the conditions' do
44
- expect(@person.all_of? :older_than_18?, :male?, :geek?).to be_true
45
- end
30
+ people = [brandon, john, jill, alice, zeke]
46
31
 
47
- it 'returns false if Person does not match all of the conditions' do
48
- expect(@person.all_of? :younger_than_18, :male?).to be_false
32
+ describe 'Person' do
33
+ context 'Boolean Predicates' do
34
+ describe '#all_of?' do
35
+ it 'returns true if Person matches all of the conditions' do
36
+ expect(brandon.all_of? :older_than_18?, :male?, :geek?).to be_true
37
+ end
38
+
39
+ it 'returns false if Person does not match all of the conditions' do
40
+ expect(brandon.all_of? :younger_than_18?, :male?).to be_false
41
+ end
49
42
  end
50
- end
51
43
 
52
- describe '#any_of?' do
53
- it 'returns true if Person matches any of the conditions' do
54
- expect(@person.any_of? :older_than_18?, :female?, :geek?).to be_true
55
- end
44
+ describe '#any_of?' do
45
+ it 'returns true if Person matches any of the conditions' do
46
+ expect(brandon.any_of? :older_than_18?, :female?, :geek?).to be_true
47
+ end
56
48
 
57
- it 'returns false if Person matches none of the conditions' do
58
- expect(@person.any_of? :younger_than_18, :female?).to be_false
49
+ it 'returns false if Person matches none of the conditions' do
50
+ expect(brandon.any_of? :younger_than_18?, :female?).to be_false
51
+ end
59
52
  end
60
- end
61
53
 
62
- describe '#none_of?' do
63
- it 'returns true if Person matches none of the conditions' do
64
- expect(@person.none_of? :younger_than_18, :female?).to be_true
65
- end
54
+ describe '#none_of?' do
55
+ it 'returns true if Person matches none of the conditions' do
56
+ expect(brandon.none_of? :younger_than_18?, :female?).to be_true
57
+ end
66
58
 
67
- it 'returns false if Person matches any of the conditions' do
68
- expect(@person.none_of? :older_than_18?, :female?, :geek?).to be_false
59
+ it 'returns false if Person matches any of the conditions' do
60
+ expect(brandon.none_of? :older_than_18?, :female?, :geek?).to be_false
61
+ end
69
62
  end
70
63
  end
71
64
 
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
65
+ context 'Matchers' do
66
+ describe '#matches_all?' do
67
+ it 'returns true if Person matches all of the conditions' do
68
+ expect(
69
+ brandon.matches_all?(name: /br/, age: (20..30))
70
+ ).to be_true
71
+ end
72
+
73
+ it 'returns true for multiple conditionals' do
74
+ expect(
75
+ brandon.matches_all?(
76
+ name: [/br/, /an/],
77
+ age: [(20..30), -> a { a < 30 }]
78
+ )
79
+ ).to be_true
80
+ end
81
+
82
+ it 'returns false if Person does not match all conditions' do
83
+ expect(
84
+ brandon.matches_all?(name: /br/, age: (25..30))
85
+ ).to be_false
86
+ end
77
87
  end
78
88
 
79
- it 'returns true for multiple conditionals' do
80
- expect(
81
- @person.matches_all?(
82
- name: [/br/, /an/],
83
- age: [(20..30), -> a { a < 30 }]
84
- )
85
- ).to be_true
89
+ describe '#matches_any?' do
90
+ it 'returns true if Person matches all of the conditions' do
91
+ expect(
92
+ brandon.matches_any?(name: /br/, age: (25..30))
93
+ ).to be_true
94
+ end
95
+
96
+ it 'returns true for multiple conditionals' do
97
+ expect(
98
+ brandon.matches_any?(
99
+ name: [/br/, /an/],
100
+ age: [(30..40), -> a { a > 30 }]
101
+ )
102
+ ).to be_true
103
+ end
104
+
105
+
106
+ it 'returns false if Person does not match any conditions' do
107
+ expect(
108
+ brandon.matches_any?(name: /br$/, age: (25..30))
109
+ ).to be_false
110
+ end
86
111
  end
87
112
 
88
- it 'returns false if Person does not match all conditions' do
89
- expect(
90
- @person.matches_all?(name: /br/, age: (25..30))
91
- ).to be_false
113
+ describe '#matches_none?' do
114
+ it 'returns true if Person matches none of the conditions' do
115
+ expect(
116
+ brandon.matches_none?(name: /br$/, age: (25..30))
117
+ ).to be_true
118
+ end
119
+
120
+ it 'returns true for multiple conditionals' do
121
+ expect(
122
+ brandon.matches_none?(
123
+ name: [/br$/, /foo/],
124
+ age: [(30..40), -> a { a > 30 }]
125
+ )
126
+ ).to be_true
127
+ end
128
+
129
+ it 'returns false if Person matches any of the conditions' do
130
+ expect(
131
+ brandon.matches_none?(name: /br/, age: (25..30))
132
+ ).to be_false
133
+ end
92
134
  end
93
135
  end
94
136
 
95
- describe '#matches_any?' do
96
- it 'returns true if Person matches all of the conditions' do
97
- expect(
98
- @person.matches_any?(name: /br/, age: (25..30))
99
- ).to be_true
100
- end
137
+ context 'Enumerable Matchers' do
138
+ describe '#select_where' do
139
+ it 'returns people whose names start with j' do
140
+ expect(people.select_where(name: /^j/)).to eq([john, jill])
141
+ end
142
+
143
+ it 'returns people whose ages are above 30' do
144
+ expect(people.select_where(age: -> a { a > 30 })).to eq([john, jill, alice])
145
+ end
101
146
 
102
- it 'returns true for multiple conditionals' do
103
- expect(
104
- @person.matches_any?(
105
- name: [/br/, /an/],
106
- age: [(30..40), -> a { a > 30 }]
107
- )
108
- ).to be_true
147
+ it 'returns people who are male' do
148
+ expect(people.select_where(sex: 'm')).to eq([brandon, john, zeke])
149
+ end
109
150
  end
110
151
 
152
+ describe '#reject_where' do
153
+ it 'returns people whose names do not start with j' do
154
+ expect(people.reject_where(name: /^j/)).not_to eq([john, jill])
155
+ end
111
156
 
112
- it 'returns false if Person does not match any conditions' do
113
- expect(
114
- @person.matches_any?(name: /br$/, age: (25..30))
115
- ).to be_false
116
- end
117
- end
157
+ it 'returns people whose ages are not above 30' do
158
+ expect(people.reject_where(age: -> a { a > 30 })).not_to eq([john, jill, alice])
159
+ end
118
160
 
119
- describe '#matches_none?' do
120
- it 'returns true if Person matches none of the conditions' do
121
- expect(
122
- @person.matches_none?(name: /br$/, age: (25..30))
123
- ).to be_true
161
+ it 'returns people who are not male' do
162
+ expect(people.reject_where(sex: 'm')).not_to eq([brandon, john, zeke])
163
+ end
124
164
  end
125
165
 
126
- it 'returns true for multiple conditionals' do
127
- expect(
128
- @person.matches_none?(
129
- name: [/br$/, /foo/],
130
- age: [(30..40), -> a { a > 30 }]
131
- )
132
- ).to be_true
133
- end
166
+ describe '#find_where' do
167
+ it 'returns the first person whose name starts with j' do
168
+ expect(people.find_where(name: /^j/)).to eq(john)
169
+ end
170
+
171
+ it 'returns the first person whose age is above 30' do
172
+ expect(people.find_where(age: -> a { a > 30 })).to eq(john)
173
+ end
134
174
 
135
- it 'returns false if Person matches any of the conditions' do
136
- expect(
137
- @person.matches_none?(name: /br/, age: (25..30))
138
- ).to be_false
175
+ it 'returns the first person who is male' do
176
+ expect(people.find_where(sex: 'm')).to eq(brandon)
177
+ end
139
178
  end
140
179
  end
141
180
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: izzy
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.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-06 00:00:00.000000000 Z
11
+ date: 2014-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.5'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description:
@@ -59,10 +59,10 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .coveralls.yml
63
- - .gitignore
64
- - .rspec
65
- - .travis.yml
62
+ - ".coveralls.yml"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
66
  - CHANGELOG.md
67
67
  - Gemfile
68
68
  - LICENSE.txt
@@ -84,12 +84,12 @@ require_paths:
84
84
  - lib
85
85
  required_ruby_version: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - '>='
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []