Skip to content

Instantly share code, notes, and snippets.

@michaeldv
Created March 11, 2011 05:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeldv/865473 to your computer and use it in GitHub Desktop.
Save michaeldv/865473 to your computer and use it in GitHub Desktop.
# 0.4.0 TODO
#
# - merge Windows fix
# - merge Stephan's ap/object
# - merge Mongo(?)
# - specs for arbitrary Obj
# - rescue LoadError if in specs if Rails is not installed
# - require unless defined?(::AwesomePrint) (when required from .irbrc)
# - Obj overrides :send
# - Obj overrides :method
# - use anonymous instead of "original"
# - need separate ActiveRecord mixin?
# - get rid of Jeweler dependency(?)
# - obj.ai(:html => true) to force html output (Sinatra w/o ActionView)
#
class Object #:nodoc:
# Remaining instance '_methods' are handled in core_ext/class.rb.
%w(methods private_methods protected_methods public_methods singleton_methods).each do |name|
original_method = instance_method(name)
# alias :"original_#{name}" :"#{name}"
define_method name do |*args|
# methods = self.__send__(:"original_#{name}", *args)
methods = original_method.bind(self).call(*args)
methods.instance_variable_set('@__awesome_methods__', self) # Evil?!
methods.sort!
end
end
end
it "methods/to_sym should be awesome" do
class Hello
def test
his = methods.grep(/h/) { |n| n.to_sym }
her = methods.grep(/e/) { |n| n.to_sym }
puts (his - her) # <= Here ;-)
end
end
h = Hello.new
h.test
end
# Add ap method to logger
#------------------------------------------------------------------------------
def ap(object, level = nil)
level ||= AwesomePrint.defaults[:log_level] if AwesomePrint.defaults
level ||= :debug
send level, object.ai
end
# Ruby 1.8.6 only: define missing String methods that are needed for the specs to pass.
if RUBY_VERSION < '1.8.7'
class String
def shellescape # Taken from Ruby 1.9.2, see lib/shellwords.rb.
return "''" if self.empty?
str = self.dup
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
str.gsub!(/\n/, "'\n'")
str
end
def start_with?(*prefixes)
prefixes.each do |prefix|
prefix = prefix.to_s
return true if prefix == self[0, prefix.size]
end
false
end
def end_with?(*suffixes)
suffixes.each do |suffix|
suffix = suffix.to_s
return true if suffix == self[-suffix.size, suffix.size]
end
false
end
end
end
def methods_array(a)
object = a.instance_variable_get('@__awesome_methods__')
tuples = a.map do |name|
tuple = if object.respond_to?(name, true) # Is this a regular method?
the_method = object.method(name) rescue nil # Avoid potential ArgumentError if object#method is overritten.
if the_method && the_method.respond_to?(:arity) # Is object#method overritten?
method_tuple(the_method) # No, we are good.
end
elsif object.respond_to?(:instance_method) # Is this an unbound method?
method_tuple(object.instance_method(name))
end
tuple || [ name.to_s, '(?)', '' ] # Return WTF if all the above fails.
end
it "should handle a class defines its own #method method (ex. request.method)" do
class My
def method
'POST'
end
end
my = My.new
my.methods.ai(:plain => true).should_not raise_error(ArgumentError)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment