Module: SimpleSymbolize
- Defined in:
- lib/simple_symbolize.rb,
lib/simple_symbolize/version.rb,
lib/simple_symbolize/translations.rb,
lib/simple_symbolize/core_ext/string/symbolize.rb
Overview
Extends the String class by mixing in the symbolize module.
Defined Under Namespace
Modules: CoreExt Classes: Error, Translations
Constant Summary collapse
- VERSION =
'4.0.0'
Class Method Summary collapse
-
.camelize(obj) ⇒ Object
Turns a String object into a camelCase Symbol.
-
.elementize(obj) ⇒ Object
Symbolizes a String object and returns it as a String object.
-
.snakeize(obj) ⇒ Object
Turns a String || Symbol into a snake_case Symbol.
-
.symbolize(obj) ⇒ Object
Symbolizes a String object.
-
.translate {|config| ... } ⇒ Object
Configures the Symbolize environment.
-
.translations ⇒ Translations
Returns the translations object, initializing it if necessary.
Class Method Details
.camelize(obj) ⇒ Object
Turns a String object into a camelCase Symbol.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/simple_symbolize.rb', line 69 def self.camelize(obj) return obj unless obj.respond_to?(:to_s) return obj if [Hash, Array, NilClass].include?(obj.class) return obj if obj.respond_to?(:empty?) && obj.empty? first, *rest = elementize(obj).split('_') return obj if first.nil? rest.size.positive? ? (first << rest.map(&:capitalize).join).to_sym : symbolize(first) end |
.elementize(obj) ⇒ Object
Symbolizes a String object and returns it as a String object.
55 56 57 58 59 60 61 |
# File 'lib/simple_symbolize.rb', line 55 def self.elementize(obj) return obj unless obj.respond_to?(:to_s) return obj if [Hash, Array, NilClass].include?(obj.class) return obj if obj.respond_to?(:empty?) && obj.empty? symbolize(obj).to_s end |
.snakeize(obj) ⇒ Object
Turns a String || Symbol into a snake_case Symbol
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/simple_symbolize.rb', line 86 def self.snakeize(obj) return obj unless obj.respond_to?(:to_s) return obj if [Hash, Array, NilClass].include?(obj.class) return obj if obj.respond_to?(:empty?) && obj.empty? obj.to_s .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_') .gsub(Regexp.union(SimpleSymbolize.translations.remove), '') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .downcase .to_sym end |
.symbolize(obj) ⇒ Object
Symbolizes a String object.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/simple_symbolize.rb', line 33 def self.symbolize(obj) return obj unless obj.respond_to?(:to_s) return obj if [Hash, Array, NilClass].include?(obj.class) return obj if obj.respond_to?(:empty?) && obj.empty? obj = if SimpleSymbolize.translations.handle_camel_case snakeize(obj) else obj.to_s .downcase .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_') .gsub(Regexp.union(SimpleSymbolize.translations.remove), '') end obj.to_sym end |
.translate {|config| ... } ⇒ Object
Configures the Symbolize environment.
23 24 25 |
# File 'lib/simple_symbolize.rb', line 23 def self.translate yield(translations) if block_given? end |
.translations ⇒ Translations
Returns the translations object, initializing it if necessary.
16 17 18 |
# File 'lib/simple_symbolize.rb', line 16 def self.translations @translations ||= Translations.new end |