Last active
December 14, 2015 06:09
-
-
Save johnkoht/5040651 to your computer and use it in GitHub Desktop.
Monkey patch Array so that we can find values. Used when caching some 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 | |
# Given an Array, find if a value exists for a given key. This is helpful in our settings | |
# since we cache the entire settings array, but occasionally we wan to find a specific | |
# setting, i.e. "Site Title", "Posts per page", etc.... | |
# USAGE::: array.find_value? "key", "Value is Case Sensitive" | |
def find_value key, value | |
if self.first.respond_to? key.parameterize.underscore.to_sym | |
self.select { |item| item.send(key.parameterize.underscore) == value }.first rescue nil | |
else | |
false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment