Class: Object
- Inherits:
- BasicObject
- Includes:
- ActionView::Helpers::DateHelper, ActionView::Helpers::TextHelper
- Defined in:
- lib/m_object.rb
Instance Method Summary collapse
-
#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
- #running_time(message = "", logger = nil) ⇒ Object
- #send_with_chain(methods, *args) ⇒ Object
Instance Method Details
#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 => bar, 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 => bar, 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 => bar, etc... => etc))
end
end
now it will cache it to @foo_var
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/m_object.rb', line 60 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
72 73 74 75 76 77 78 79 |
# File 'lib/m_object.rb', line 72 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
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/m_object.rb', line 81 def namespaces ivar_cache("object_namespaces") do nss = [] full_name = self.class.name.to_s ns = full_name.split("::") ns.each do |n| nss << n end nss.pop nss end end |
#running_time(message = "", logger = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/m_object.rb', line 5 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? x = "Running time #{distance_of_time_in_words(s_time, e_time, true)} (#{e_time - s_time} seconds)." x += " [MESSAGE]: #{}" unless .blank? puts x logger.info x unless logger.nil? end |