Class: PopCap::ClassSupport

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

Overview

Public: This class adds methods to construct a class.

name - This is the name of the class.

Examples

ClassSupport.new('array')
ClassSupport.new('active_support')
ClassSupport.new('active_record/base')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ClassSupport

Returns a new instance of ClassSupport.



14
15
16
# File 'lib/pop_cap/class_support.rb', line 14

def initialize(name)
  @name = name.to_s
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/pop_cap/class_support.rb', line 12

def name
  @name
end

Instance Method Details

#camelizeObject

Public: This method camel cases a string or symbol.

Examples

support = ClassSupport.new('active_support')
support.camelize
# => 'ActiveSupport'


25
26
27
# File 'lib/pop_cap/class_support.rb', line 25

def camelize
  name.split('_').map { |word| word.capitalize }.join
end

#constantizeObject

Public: This converts a string into a constant.

Examples

support = ClassSupport.new('active_record/base')
support.constantize
# => ActiveSupport::Base


51
52
53
# File 'lib/pop_cap/class_support.rb', line 51

def constantize
  Object.module_eval(namespace)
end

#namespaceObject

Public: This namespaces a string by converting a filepath to a namespaced constant.

Examples

support = ClassSupport.new('active_record/base')
support.namespace
# => 'ActiveRecord::Base'


37
38
39
40
41
42
# File 'lib/pop_cap/class_support.rb', line 37

def namespace
  camelize.split('/').map do |word|
    _,head,tail = word.partition(%r(^[a-zA-Z]))
    head.upcase + tail
  end.join('::')
end

#symbolizeObject

Public: This converts a string into a symbol. It will handle CamelCased strings.

option - It takes an option to demodulize the string.

Examples

support = ClassSupport.new('ActiveSupport')
support.symbolize
# => :active_support

support = ClassSupport.new('ActiveSupport::CamelCase')
support.symbolize(:demodulize)
# => :camel_case


69
70
71
# File 'lib/pop_cap/class_support.rb', line 69

def symbolize
  name.split('::').last.split(%r((?=[A-Z]))).join('_').downcase.to_sym
end