Last active
March 4, 2016 17:07
-
-
Save goncalvesjoao/e1111844fc8f0d4ccd07 to your computer and use it in GitHub Desktop.
Some helper methods to avoid simple ruby gems to include active_support
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
module Helpers | |
extend self | |
def super_method(object, method_name, *args) | |
return nil unless object.superclass.respond_to? method_name | |
object.superclass.send method_name, *args | |
end | |
def blank?(object) | |
if object.is_a?(String) | |
object !~ /[^[:space:]]/ | |
else | |
object.respond_to?(:empty?) ? object.empty? : !object | |
end | |
end | |
def extract_options!(array) | |
if array.last.is_a?(Hash) && array.last.instance_of?(Hash) | |
array.pop | |
else | |
{} | |
end | |
end | |
def symbolyze_keys(hash) | |
{}.tap do |symbolyzed_hash| | |
hash.each do |key, value| | |
symbolyzed_hash[key.to_sym] = value | |
end | |
end | |
end | |
def except(hash, *keys) | |
hash = hash.dup | |
keys.each { |key| hash.delete(key) } | |
hash | |
end | |
def slice(hash, *keys) | |
keys.each_with_object({}) do |key, sliced_hash| | |
sliced_hash[key] = hash[key] | |
end | |
end | |
def call_proc_or_method(base, proc_or_method, object = nil) | |
if proc_or_method.is_a?(Proc) | |
base.instance_exec(object, &proc_or_method) | |
else | |
base.send(proc_or_method, object) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment