• Stars
    star
    114
  • Rank 308,031 (Top 7 %)
  • Language
    Ruby
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Mitigate long conditionals - DEPRECATED - Use Qo

Izzy (DEPRECATED)

Izzy has been deprecated in favor of Qo: https://github.com/baweaver/qo

Qo achieves much of the same functionality and more without the need to monkeypatch, and is in many ways the spiritual successor to Izzy. Four years makes a lot of difference!


Build Status Code Climate Coverage Status

Izzy Module

To demo Izzy, let's make a Person:

brandon = Person.new('brandon', 23, 'm')

Matchers

Let's do some matching against it:

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

Type Checking

Perhaps you want to type check:

brandon.matches_all? name: String, age: Integer

Lambdas

Need a bit more power? We have Lambdas for that!

longer_than_3 = -> n { n.length > 3 }
is_odd = -> a { a.odd? }

brandon.matches_all? name: longer_than_3, age: is_odd # => true

Multi Matchers

....or let's push it further for some interesting results:

longer_than_5 = -> n { n.length > 5 }
greater_than_20 = -> a { a > 20 }

brandon.matches_all?(
  name: [/br/, /an/, longer_than_5],
  age: [(20..30), greater_than_20]
)

# => true

Boolean Matchers

...and do some comparisons on boolean methods!

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

Izzy Array Module

IzzyArray allows you a few more tricks on top of that!

class Array; include IzzyArray end

[0,0,0].all_are :zero?       # => true
[0,nil,'foo'].any_are :zero? # => true
[0,0,0].none_are :nil?       # => true

Combine with rails :present?, :empty?, and various other methods and you have some interesting results! This one will get some more power with experimentation.

Izzy Enumerable

Let's get ourselves a collection to use:

class Object; include Izzy end

brandon = Person.new('brandon', 23, 'm')
john    = Person.new('john',    42, 'm')
jill    = Person.new('jill',    31, 'f')
alice   = Person.new('alice',   50, 'f')
zeke    = Person.new('zeke',    18, 'm')

people = [brandon, john, jill, alice, zeke]

Select Where

Select all objects where params match:

people.select_where name: /^j/
# => [john, jill]

people.select_where(
  age: [
    (20..35),
    -> a { a.odd? }
  ]
)
# => [brandon]

Reject Where

Reject all objects where params match:

people.reject_where name: /^j/
# => [brandon, alice, zeke]

people.reject_where(
  age: [
    (20..35),
    -> a { a.odd? }
  ]
)
# => [john, jill, alice, zeke]

Find Where

Finds the first object where params match:

people.find_where name: /^j/, gender: 'm'
# => john

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

  1. Fork it ( http://github.com/baweaver/izzy/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

More Repositories

1

qo

Qo - Query Object - Pattern matching and fluent querying in Ruby
Ruby
358
star
2

xf

Xf - Transform Functions
Ruby
62
star
3

refactor

Utilities for refactoring and upgrading Ruby code based on ASTs.
Ruby
60
star
4

trace_spy

[ALPHA] TraceSpy is a wrapper around TracePoint with some extra pattern matching magic
Ruby
52
star
5

pry-macro

Macros for Pry
Ruby
49
star
6

mf

Mf - Modifier functions
Ruby
34
star
7

pokegem

Simple Ruby Wrapper for the PokeAPI
Ruby
24
star
8

dio

Dio - Dive Into Objects - Ruby Pattern Matching interface for Objects which have none (yet)
Ruby
22
star
9

ubiquiti

Tools and various creations for Ubiquiti Admins and WISPs
Ruby
17
star
10

clairvoyant

Divine what code you meant to write to make your tests pass
Ruby
16
star
11

Benchpress

Pit a few ruby methods against eachother, and chart it for great glory
Ruby
14
star
12

dependency_manager

Dependency Management prototype
Ruby
13
star
13

hesh

Hesh is fun with hashes! Eventually more, but this works for now
Ruby
11
star
14

any

An Any type, modeled after Scala's Any
Ruby
11
star
15

classtab

Classical Guitar Tabs from classtab.org
HTML
11
star
16

globs

Psuedo-glob text for set generation
Ruby
10
star
17

reducing_enumerable

Talk examples, articles, and other useful tidbits
Ruby
9
star
18

streamable

Stream data through functions in Ruby, like Clojure's ->
Ruby
9
star
19

you_type_too_much

Example code and other materials from my conference talk at HTML5DevConf 2015
TypeScript
8
star
20

advent_of_code_2020

Ruby
7
star
21

github_org_manager

Manages Github Organizations, currently downloading and keeping all org repos up to date
Ruby
6
star
22

qenumerable

Query against collections
Ruby
6
star
23

lexigrapher

Find the type of data inside a Ruby String
Ruby
6
star
24

taking

[Experimental] Argument forwarding for pattern matching
Ruby
5
star
25

matchable

Pattern Matching interfaces in Ruby made easy
Ruby
5
star
26

lazy_static

Lazy static type checking for ruby
Ruby
4
star
27

special-sauce

All of my aliases and other goodies I use to speed myself up.
Shell
4
star
28

actioncable_symphony

The ActionCable Symphony - A Symphony Orchestra played over Actioncable!
Ruby
3
star
29

hashiform

Transforms hashes
Ruby
2
star
30

tak

Ruby implementation of a Tak game
Ruby
2
star
31

valor

Automate creating Virtus Models from a Ruby Hash
Ruby
2
star
32

offense_to_corrector

The inverse of a RuboCop offense - Use the offense to generate a corrector (skeleton for now)
Ruby
1
star
33

ragnoster

Ruby based powerline mimicking the agnoster zsh theme
Ruby
1
star
34

ast_explorer

Ruby
1
star
35

Dantalion

Mass documentation, indexing, and categorization.
1
star
36

pipeable

A play at unix piping
Ruby
1
star
37

mockingjay

Dynamic Fixture Generator
Ruby
1
star