Skip to content

Instantly share code, notes, and snippets.

@johnkoht
Last active December 14, 2015 06:09
Show Gist options
  • Save johnkoht/5040651 to your computer and use it in GitHub Desktop.
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
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