Class: Classify

Inherits:
Object
  • Object
show all
Defined in:
lib/classify.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.it!(string) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
# File 'lib/classify.rb', line 2

def self.it!(string)
  raise ArgumentError, "Wrong argument #{string.inspect}" if not string.class == String or string.match(/(^\_)|(\_$)|([^a-z0-9\_])/i)
  Classify.new.camelize(string)
end

Instance Method Details

#camelize(string) ⇒ Object



15
16
17
# File 'lib/classify.rb', line 15

def camelize(string)
  self.try_converting(string.gsub(/^(.{1})|\_.{1}/) { |s| s.gsub(/[^a-z0-9]+/i, '').capitalize })
end

#try_converting(string) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/classify.rb', line 7

def try_converting(string)
  unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ string
    raise ArgumentError, "#{string} is not a valid constant name!"
  end

  Object.module_eval("::#{$1}", __FILE__, __LINE__)
end