Module: Jack::String

Defined in:
lib/jack/string.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jack/string.rb', line 3

def self.included(klass)
  klass.class_eval do
    def camelize
      self.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
    end
    
    
    # Ruby 1.9 introduces an inherit argument for Module#const_get and
    # #const_defined? and changes their default behavior.
    # Taken from rails' inflectors. http://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
    if Module.method(:const_get).arity == 1
      def constantize
        names = self.split('::')
        names.shift if names.empty? || names.first.empty?

        constant = Object
        names.each do |name|
          constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
        end
        constant
      end
    else
      def constantize
        names = self.split('::')
        names.shift if names.empty? || names.first.empty?

        constant = Object
        names.each do |name|
          constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
        end
        constant
      end
    end
  end
end