Class: SiteMaps::Primitive::String
- Inherits:
-
String
- Object
- String
- SiteMaps::Primitive::String
- Defined in:
- lib/site_maps/primitive/string.rb
Class Method Summary collapse
Instance Method Summary collapse
- #camelize(uppercase_first_letter = true) ⇒ Object
- #classify ⇒ Object
- #constantize ⇒ Object
- #pluralize ⇒ Object
- #singularize ⇒ Object
- #underscore ⇒ Object
Class Method Details
.inflector ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/site_maps/primitive/string.rb', line 17 def self.inflector return @inflector if defined?(@inflector) @inflector = if defined?(::ActiveSupport::Inflector) ::ActiveSupport::Inflector elsif defined?(::Dry::Inflector) ::Dry::Inflector.new end end |
Instance Method Details
#camelize(uppercase_first_letter = true) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/site_maps/primitive/string.rb', line 83 def camelize(uppercase_first_letter = true) new_str = inflector&.camelize(self, uppercase_first_letter) || begin # dummy camelize str = to_s str = str.sub(/^[a-z\d]*/) { $&.capitalize } str = str.tr("-", "_") str = str.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" } str = str.gsub("/", "::") unless uppercase_first_letter str = str.sub(/^[A-Z]*/) { $&.downcase } end str end self.class.new(new_str) end |
#classify ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/site_maps/primitive/string.rb', line 27 def classify new_str = inflector&.classify(self) || split("/").collect do |c| c.split("_").collect(&:capitalize).join end.join("::") self.class.new(new_str) end |
#constantize ⇒ Object
35 36 37 |
# File 'lib/site_maps/primitive/string.rb', line 35 def constantize inflector&.constantize(self) || Object.const_get(self) end |
#pluralize ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/site_maps/primitive/string.rb', line 53 def pluralize new_str = inflector&.pluralize(self) || begin # dummy pluralize if /y$/.match?(self) sub(/y$/, "ies") elsif /s$/.match?(self) self else self + "s" end end new_str.is_a?(self.class) ? new_str : self.class.new(new_str) end |
#singularize ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/site_maps/primitive/string.rb', line 68 def singularize new_str = inflector&.singularize(self) || begin # dummy singularize if /ies$/.match?(self) sub(/ies$/, "y") elsif /s$/.match?(self) sub(/s$/, "") else self end end new_str.is_a?(self.class) ? new_str : self.class.new(new_str) end |
#underscore ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/site_maps/primitive/string.rb', line 39 def underscore new_str = sub(/^::/, "") .gsub("::", "/") .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr("-", "_") .tr(".", "_") .gsub(/\s/, "_") .gsub(/__+/, "_") .downcase self.class.new(new_str) end |