Class: Object

Inherits:
BasicObject
Includes:
ActionView::Helpers::DateHelper, ActionView::Helpers::TextHelper
Defined in:
lib/m_object.rb

Instance Method Summary collapse

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


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/m_object.rb', line 53

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



65
66
67
68
69
70
71
72
# File 'lib/m_object.rb', line 65

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

#namespacesObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/m_object.rb', line 74

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(message = "", 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]: #{message}" unless message.blank?
  puts x
  logger.info x unless logger.nil?
end