Class: Object

Inherits:
BasicObject
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


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

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



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

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



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/m_object.rb', line 84

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



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/m_object.rb', line 3

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

#send_with_chain(methods, *args) ⇒ Object



24
25
26
27
28
29
# File 'lib/m_object.rb', line 24

def send_with_chain(methods, *args)
  obj = self
  methods = [methods] unless methods.is_a?(Array)
  methods.each {|m| obj = obj.send(m, *args)}
  obj
end