Class: Tzispa::Utils::String
- Inherits:
-
Object
- Object
- Tzispa::Utils::String
- Defined in:
- lib/tzispa/utils/string.rb
Constant Summary collapse
- NAMESPACE_SEPARATOR =
'::'- CLASSIFY_SEPARATOR =
'_'- UNDERSCORE_SEPARATOR =
'/'- UNDERSCORE_DIVISION_TARGET =
'\1_\2'
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #camelize ⇒ Object
- #camelize! ⇒ Object
- #constantize ⇒ Object
-
#initialize(str) ⇒ String
constructor
A new instance of String.
- #to_s ⇒ Object
- #underscore ⇒ Object
- #underscore! ⇒ Object
Constructor Details
#initialize(str) ⇒ String
Returns a new instance of String.
14 15 16 |
# File 'lib/tzispa/utils/string.rb', line 14 def initialize(str) @string = str.to_s end |
Class Method Details
.camelize(str) ⇒ Object
40 41 42 |
# File 'lib/tzispa/utils/string.rb', line 40 def self.camelize(str) self.new(str).camelize end |
.constantize(str) ⇒ Object
26 27 28 |
# File 'lib/tzispa/utils/string.rb', line 26 def self.constantize(str) self.new(str).constantize end |
.underscore(str) ⇒ Object
52 53 54 |
# File 'lib/tzispa/utils/string.rb', line 52 def self.underscore(str) self.new(str).underscore end |
Instance Method Details
#==(other) ⇒ Object
22 23 24 |
# File 'lib/tzispa/utils/string.rb', line 22 def ==(other) to_s == other end |
#camelize ⇒ Object
44 45 46 |
# File 'lib/tzispa/utils/string.rb', line 44 def camelize self.class.new( @string.split(CLASSIFY_SEPARATOR).collect{ |w| w.capitalize }.join) end |
#camelize! ⇒ Object
48 49 50 |
# File 'lib/tzispa/utils/string.rb', line 48 def camelize! @string = @string.split(CLASSIFY_SEPARATOR).collect!{ |w| w.capitalize }.join end |
#constantize ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/tzispa/utils/string.rb', line 30 def constantize names = @string.split(NAMESPACE_SEPARATOR) 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 |
#to_s ⇒ Object
18 19 20 |
# File 'lib/tzispa/utils/string.rb', line 18 def to_s @string end |
#underscore ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/tzispa/utils/string.rb', line 56 def underscore self.class.new(@string.dup.tap { |s| s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR) s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET) s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET) s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET) s.downcase! }) end |
#underscore! ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/tzispa/utils/string.rb', line 66 def underscore! @string = @string.tap { |s| s.gsub!(NAMESPACE_SEPARATOR, UNDERSCORE_SEPARATOR) s.gsub!(/([A-Z\d]+)([A-Z][a-z])/, UNDERSCORE_DIVISION_TARGET) s.gsub!(/([a-z\d])([A-Z])/, UNDERSCORE_DIVISION_TARGET) s.gsub!(/[[:space:]]|\-/, UNDERSCORE_DIVISION_TARGET) s.downcase! } end |