Add the has_many_polymorphs plugin
[cs356-p2-videostore.git] / vendor / plugins / has_many_polymorphs / lib / has_many_polymorphs / support_methods.rb
1
2 class String
3
4   # Changes an underscored string into a class reference.
5   def _as_class
6     # classify expects self to be plural
7     self.classify.constantize
8   end
9
10   # For compatibility with the Symbol extensions.
11   alias :_singularize :singularize
12   alias :_pluralize :pluralize
13   alias :_classify :classify
14 end
15
16 class Symbol
17   
18   # Changes an underscored symbol into a class reference.
19   def _as_class; self.to_s._as_class; end
20   
21   # Changes a plural symbol into a singular symbol.
22   def _singularize; self.to_s.singularize.to_sym; end
23
24   # Changes a singular symbol into a plural symbol.
25   def _pluralize; self.to_s.pluralize.to_sym; end
26
27   # Changes a symbol into a class name string.
28   def _classify; self.to_s.classify; end
29 end
30
31 class Array
32
33   # Flattens the first level of self.
34   def _flatten_once
35     self.inject([]){|r, el| r + Array(el)}
36   end
37
38   # Rails 1.2.3 compatibility method. Copied from http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/core_ext/array/extract_options.rb?rev=7217 
39   def _extract_options!
40     last.is_a?(::Hash) ? pop : {}
41   end
42 end
43
44 class Hash
45
46   # An implementation of select that returns a Hash.
47   def _select
48     Hash[*self.select do |key, value|
49       yield key, value
50     end._flatten_once]
51   end
52 end
53
54 class Object
55
56   # Returns the metaclass of self.
57   def _metaclass; (class << self; self; end); end
58
59   # Logger shortcut.
60   def _logger_debug s
61     s = "** has_many_polymorphs: #{s}"
62     RAILS_DEFAULT_LOGGER.debug(s) if RAILS_DEFAULT_LOGGER
63   end  
64
65   # Logger shortcut.  
66   def _logger_warn s
67     s = "** has_many_polymorphs: #{s}"
68     if RAILS_DEFAULT_LOGGER
69       RAILS_DEFAULT_LOGGER.warn(s) 
70     else
71       $stderr.puts(s)
72     end    
73   end
74   
75 end
76
77 class ActiveRecord::Base
78
79   # Return the base class name as a string.
80   def _base_class_name
81     self.class.base_class.name.to_s
82   end
83   
84 end