Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/extensions/object.rb
Class Method Summary collapse
-
.needs_method(meth) ⇒ Object
Uses
define_method
to create an empty for the method parameter defined.
Instance Method Summary collapse
-
#class_parents ⇒ Object
See Class parents for more information.
-
#ivar_cache(var_name = nil, &block) ⇒ Object
ivar_cache allows you to cache the results of the block into an instance variable in a class, and then will serve up that instance variable the next time you call that method again.
- #ivar_cache_clear(var_name = nil, &block) ⇒ Object
-
#namespaces ⇒ Object
Returns the namespaces for a particular object.
-
#running_time(message = "", logger = nil) ⇒ Object
This prints out running time for the block provided.
-
#send_with_chain(methods, *args) ⇒ Object
This method will call send to all the methods defined on the previous method.
-
#to_param ⇒ Object
This method gets called when a parameter is passed into a named route.
- #with_options(options) {|Mack::Utils::OptionMerger.new(self, options)| ... } ⇒ Object
Class Method Details
.needs_method(meth) ⇒ Object
Uses define_method
to create an empty for the method parameter defined. That method will then raise MethodNotImplemented. This is useful for creating interfaces and you want to stub out methods that others need to implement.
21 22 23 24 25 |
# File 'lib/extensions/object.rb', line 21 def self.needs_method(meth) define_method(meth) do raise MethodNotImplemented.new(meth) end end |
Instance Method Details
#class_parents ⇒ Object
See Class parents for more information.
8 9 10 |
# File 'lib/extensions/object.rb', line 8 def class_parents self.class.parents end |
#ivar_cache(var_name = nil, &block) ⇒ Object
ivar_cache allows you to cache the results of the block into an instance variable in a class, and then will serve up that instance variable the next time you call that method again.
old way:
def show_page_link
unless @show_page_link # check if instance variable exists
# if the instance variable doesn't exist let's do some work and assign it to the instance variable.
@show_page_link = link_to("show", some_url(:id => self.id, :foo => , etc... => etc))
end
@show_page_link # now return the instance variable
end
new way:
def show_page_link
ivar_cache do
link_to("show", some_url(:id => self.id, :foo => , etc... => etc))
end
end
this does everything the old way did, but it is much cleaner, and a lot less code! in case you’re wondering it caches the result, by default, to an instance variable named after the method, so in our above example the instance variable would be name, @show_page_link
. this can be overridden like such:
def show_page_link
ivar_cache("foo_var") do
link_to("show", some_url(:id => self.id, :foo => , etc... => etc))
end
end
now it will cache it to @foo_var
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/extensions/object.rb', line 94 def ivar_cache(var_name = nil, &block) if var_name.nil? call = caller[0] var_name = call[(call.index('`')+1)...call.index("'")] end var = instance_variable_get("@#{var_name}") unless var return instance_variable_set("@#{var_name}", yield) if block_given? end instance_variable_get("@#{var_name}") end |
#ivar_cache_clear(var_name = nil, &block) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/extensions/object.rb', line 106 def ivar_cache_clear(var_name = nil, &block) if var_name.nil? call = caller[0] var_name = call[(call.index('`')+1)...call.index("'")] end remove_instance_variable("@#{var_name}") #rescue yield if block_given? end |
#namespaces ⇒ Object
Returns the namespaces for a particular object.
Examples:
Animals::Dog::Poodle.new.namespaces # => ["Animals", "Dog"]
119 120 121 122 123 124 125 126 127 |
# File 'lib/extensions/object.rb', line 119 def namespaces ivar_cache("object_namespaces") do nss = [] full_name = self.class.name.to_s nss = full_name.split("::") nss.pop nss end end |
#running_time(message = "", logger = nil) ⇒ Object
This prints out running time for the block provided. This is great for things like Rake tasks, etc… where you would like to know how long it, or a section of it took to run.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/extensions/object.rb', line 30 def running_time( = "", logger = nil) s_time = Time.now s = "---Starting at #{s_time}---" puts s logger.info s unless logger.nil? yield if block_given? e_time = Time.now e = "---Ending at #{e_time}---" puts e logger.info e unless logger.nil? secs = e_time - s_time if secs < 60 x = "Running time #{secs} seconds." else x = "Running time roughly #{secs/60} minutes [#{secs} seconds]" end x += " [MESSAGE]: #{}" unless .blank? puts x logger.info x unless logger.nil? end |
#send_with_chain(methods, *args) ⇒ Object
This method will call send to all the methods defined on the previous method.
Example:
Fruit.send_with_chain([:new, :get_citrus, :get_orange, :class]) # => Orange
This would be the equivalent:
Fruit.new.get_citrus.get_orange.class
58 59 60 61 62 |
# File 'lib/extensions/object.rb', line 58 def send_with_chain(methods, *args) obj = self [methods].flatten.each {|m| obj = obj.send(m, *args)} obj end |
#to_param ⇒ Object
This method gets called when a parameter is passed into a named route. This can be overridden in an Object to provlde custom handling of parameters.
14 15 16 |
# File 'lib/extensions/object.rb', line 14 def to_param self.to_s end |
#with_options(options) {|Mack::Utils::OptionMerger.new(self, options)| ... } ⇒ Object
3 4 5 |
# File 'lib/extensions/object.rb', line 3 def () yield Mack::Utils::OptionMerger.new(self, ) end |