Module: CommandKit::Inflector
- Defined in:
- lib/command_kit/inflector.rb
Overview
Note:
If you need something more powerful, checkout dry-inflector
A very simple inflector.
Class Method Summary collapse
-
.camelize(name) ⇒ String
Converts an under_scored name to a CamelCased name.
-
.dasherize(name) ⇒ String
Replaces all underscores with dashes.
-
.demodularize(name) ⇒ String
Removes the namespace from a constant name.
-
.underscore(name) ⇒ String
Converts a CamelCased name to an under_scored name.
Class Method Details
.camelize(name) ⇒ String
Converts an under_scored name to a CamelCased name.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/command_kit/inflector.rb', line 70 def self.camelize(name) name = name.to_s.dup # sourced from: https://github.com/dry-rb/dry-inflector/blob/c918f967ff82611da374eb0847a77b7e012d3fa8/lib/dry/inflector.rb#L329-L334 name.sub!(/^[a-z\d]*/,&:capitalize) name.gsub!(%r{(?:[_-]|(/))([a-z\d]*)}i) do |match| slash = Regexp.last_match(1) word = Regexp.last_match(2) "#{slash}#{word.capitalize}" end name.gsub!('/','::') name end |
.dasherize(name) ⇒ String
Replaces all underscores with dashes.
57 58 59 |
# File 'lib/command_kit/inflector.rb', line 57 def self.dasherize(name) name.to_s.tr('_','-') end |
.demodularize(name) ⇒ String
Removes the namespace from a constant name.
23 24 25 |
# File 'lib/command_kit/inflector.rb', line 23 def self.demodularize(name) name.to_s.split('::').last end |
.underscore(name) ⇒ String
Converts a CamelCased name to an under_scored name.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/command_kit/inflector.rb', line 36 def self.underscore(name) # sourced from: https://github.com/dry-rb/dry-inflector/blob/c918f967ff82611da374eb0847a77b7e012d3fa8/lib/dry/inflector.rb#L286-L287 name = name.to_s.dup name.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') name.gsub!(/([a-z\d])([A-Z])/, '\1_\2') name.tr!('-','_') name.downcase! name end |