Izzy
Monkey patch to object to make for a nicer time of conditionals! Just install the gem and you're ready to go!
Let's take our class, Person:
class Person
include Izzy # Make sure to include Izzy!
attr_reader :name, :age, :sex
def initialize(name, age, sex)
@name = name
@age = age
@sex = sex
end
def older_than_18?
@age > 18
end
def younger_than_18?
@age < 18
end
def male?
@sex == 'm'
end
def female?
@sex == 'f'
end
def me?
@name == 'brandon' && @sex == 'm'
end
def geek?
@name == 'brandon'
end
end
So we make our person:
brandon = Person.new('brandon', 23, 'm')
...and do some comparisons!
brandon.all_of? :older_than_18, :male, :me, :geek # => true
brandon.none_of? :younger_than_18, :female # => true
brandon.any_of? :male, :female, :geek # => true
Maybe boolean comparisons aren't your cup of tea. Izzy has you covered my friend:
brandon.matches_all? name: /^br/, age: (20..30) # => true
brandon.matches_any? name: /br$/, age: (20..30) # => true
brandon.matches_none? name: /br&/, age: (30..40) # => true
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.
brandon.matches_all? name: String, age: Integer
Why stop there? Need a bit more power? We have Lambdas for that!
brandon.matches_all? name: -> n { n.length > 3 }, age: -> a { a.odd? } # => true
....or let's push it further for some interesting results:
brandon.matches_all?(
name: [
/br/, /an/, -> n { n.length > 5 }
],
age: [
(20..30), -> a { a > 20 }
]
)
# => true
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!
Installation
Add this line to your application's Gemfile:
gem 'izzy'
And then execute:
$ bundle
Or install it yourself as:
$ gem install izzy
Usage
It patches object, just use it with any method that matches the pattern is_foo? and you're good to go!
Contributing
- Fork it ( http://github.com/baweaver/izzy/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request


