Class: PopCap::ClassSupport
- Inherits:
-
Object
- Object
- PopCap::ClassSupport
- Defined in:
- lib/pop_cap/class_support.rb
Overview
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#camelize ⇒ Object
Public: This method camel cases a string or symbol.
-
#constantize ⇒ Object
Public: This converts a string into a constant.
-
#initialize(name) ⇒ ClassSupport
constructor
A new instance of ClassSupport.
-
#namespace ⇒ Object
Public: This namespaces a string by converting a filepath to a namespaced constant.
-
#symbolize ⇒ Object
Public: This converts a string into a symbol.
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
#name ⇒ Object (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
#camelize ⇒ Object
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 |
#constantize ⇒ Object
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 |
#namespace ⇒ Object
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 |
#symbolize ⇒ Object
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 |