Class: Object

Inherits:
BasicObject
Defined in:
lib/mack-facets/extensions/object.rb

Class Method Summary collapse

Instance Method Summary collapse

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.



63
64
65
66
67
# File 'lib/mack-facets/extensions/object.rb', line 63

def self.needs_method(meth)
  define_method(meth) do
    raise NoMethodError.new("The interface you are using requires you define the following method '#{meth}'")
  end
end

Instance Method Details

#class_parentsObject

See Class parents for more information.



50
51
52
# File 'lib/mack-facets/extensions/object.rb', line 50

def class_parents
  self.class.parents
end

#define_instance_method(sym, &block) ⇒ Object



3
4
5
6
7
8
# File 'lib/mack-facets/extensions/object.rb', line 3

def define_instance_method(sym, &block)
  mod = Module.new do
    define_method(sym, &block)
  end
  self.extend mod
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 => 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



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mack-facets/extensions/object.rb', line 136

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



148
149
150
151
152
153
154
155
# File 'lib/mack-facets/extensions/object.rb', line 148

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

Returns the namespaces for a particular object.

Examples:

Animals::Dog::Poodle.new.namespaces # => ["Animals", "Dog"]


161
162
163
164
165
166
167
168
169
# File 'lib/mack-facets/extensions/object.rb', line 161

def namespaces
  ivar_cache("object_namespaces") do
    nss = []
    full_name = self.class.name.to_s
    nss = full_name.split("::")
    nss.pop
    nss
  end
end

Prints out the methods associated with this object in alphabetical order.



23
24
25
26
27
28
# File 'lib/mack-facets/extensions/object.rb', line 23

def print_methods
  m = "----- #{self} (methods) -----\n"
  m << methods.sort.join("\n")
  puts m
  m
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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mack-facets/extensions/object.rb', line 72

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

#safely_include_module(*modules) ⇒ Object

Includes a module into the current Class, and changes all the module’s public methods to protected.

Example:

class FooController
  safely_include_module(MyCoolUtils, MyOtherModule)
end


16
17
18
19
20
# File 'lib/mack-facets/extensions/object.rb', line 16

def safely_include_module(*modules)
  [modules].flatten.each do |mod|
    mod.include_safely_into(self)
  end
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


100
101
102
103
104
# File 'lib/mack-facets/extensions/object.rb', line 100

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

#to_paramObject

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.



56
57
58
# File 'lib/mack-facets/extensions/object.rb', line 56

def to_param
  self.to_s
end

#with_options(options) {|Mack::Utils::OptionMerger.new(self, options)| ... } ⇒ Object

An elegant way to refactor out common options

with_options :order => 'created_at', :class_name => 'Comment' do |post|
  post.has_many :comments, :conditions => ['approved = ?', true], :dependent => :delete_all
  post.has_many :unapproved_comments, :conditions => ['approved = ?', false]
  post.has_many :all_comments
end

Can also be used with an explicit receiver:

map.with_options :controller => "people" do |people|
  people.connect "/people",     :action => "index"
  people.connect "/people/:id", :action => "show"
end

Yields:



45
46
47
# File 'lib/mack-facets/extensions/object.rb', line 45

def with_options(options)
  yield Mack::Utils::OptionMerger.new(self, options)
end