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.

Examples:

Mixin the methods to the String class

String.include SimpleSymbolize::CoreExt::String

Defined Under Namespace

Modules: CoreExt Classes: Error, Translations

Constant Summary collapse

VERSION =
'4.0.0'

Class Method Summary collapse

Class Method Details

.camelize(obj) ⇒ Object

Turns a String object into a camelCase Symbol.

Examples:

Camelize a string using the camelize method

SimpleSymbolize.camelize("hello world!") #=> :helloWorld

Parameters:

  • obj (Object)

    the String object to be camelized.



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.

Examples:

Elementize a string using the elementize method

SimpleSymbolize.elementize("hello world!") #=> "helloWorld"

Parameters:

  • obj (Object)

    the object to be symbolized.



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

Examples:

Snakeize an object using the snakeize method

SimpleSymbolize.snakeize('Hello World!') #=> :hello_world

Parameters:

  • obj (Object)

    the object to be snakeize



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.

Examples:

Symbolize a string using the symbolize method

SimpleSymbolize.symbolize("hello world!") #=> :hello_world

Parameters:

  • obj (Object)

    the String object to be symbolized.



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.

Yield Parameters:

  • config (Translations)

    the translations object yielded to the block.



23
24
25
# File 'lib/simple_symbolize.rb', line 23

def self.translate
  yield(translations) if block_given?
end

.translationsTranslations

Returns the translations object, initializing it if necessary.

Returns:



16
17
18
# File 'lib/simple_symbolize.rb', line 16

def self.translations
  @translations ||= Translations.new
end