Created
March 3, 2015 20:57
-
-
Save tlux/24fcbf71dc84cbb14a2b to your computer and use it in GitHub Desktop.
ActiveRecord like finders for Arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Array | |
| def find_by!(attributes) | |
| find_by(attributes) || fail('No element found') | |
| end | |
| def find_by(attributes) | |
| detect do |item| | |
| attributes.collect do |key, value| | |
| item.public_send(key) == value | |
| end.reduce(:&) | |
| end | |
| end | |
| def where(attributes) | |
| select do |item| | |
| attributes.collect do |key, value| | |
| item.public_send(key) == value | |
| end.reduce(:&) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment