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,
lib/simple_symbolize/core_ext/symbol/symbolize.rb

Overview

Extends the Symbol class by mixing in the symbolize module.

Examples:

Mixin the methods to the Symbol class

Symbol.include SimpleSymbolize::CoreExt::Symbol

Defined Under Namespace

Modules: CoreExt Classes: Error, Translations

Constant Summary collapse

VERSION =
'6.0.0'

Class Method Summary collapse

Class Method Details

.camelize(obj, strip_chars: true) ⇒ 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
# File 'lib/simple_symbolize.rb', line 69

def self.camelize(obj, strip_chars: true)
  return obj unless valid_input?(obj)

  first, *rest = elementize(obj, strip_chars:).split('_')
  return obj if first.nil?

  assemble_came_case_string(first, rest)
end

.elementize(obj, strip_chars: true) ⇒ 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.



57
58
59
60
61
# File 'lib/simple_symbolize.rb', line 57

def self.elementize(obj, strip_chars: true)
  return obj unless valid_input?(obj)

  symbolize(obj, strip_chars:).to_s
end

.snakeize(obj, strip_chars: true) ⇒ 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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/simple_symbolize.rb', line 84

def self.snakeize(obj, strip_chars: true)
  return obj unless valid_input?(obj)

  obj.to_s
     .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
     .then { |str| strip_chars?(strip_chars) ? str.gsub(Regexp.union(SimpleSymbolize.translations.remove), '') : str }
     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
     .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
     .downcase
     .to_sym
end

.symbolize(obj, strip_chars: true) ⇒ 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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/simple_symbolize.rb', line 34

def self.symbolize(obj, strip_chars: true)
  return obj unless valid_input?(obj)

  obj = if SimpleSymbolize.translations.handle_camel_case
          snakeize(obj, strip_chars:)
        else
          obj.to_s
             .downcase
             .gsub(Regexp.union(SimpleSymbolize.translations.underscore), '_')
             .then do |str|
               strip_chars?(strip_chars) ? str.gsub(Regexp.union(SimpleSymbolize.translations.remove), '') : str
             end
        end

  obj.to_sym
end

.translate {|config| ... } ⇒ Object

Configures the Symbolize environment.

Yield Parameters:

  • config (Translations)

    the translations object yielded to the block.



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

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

.translationsTranslations

Returns the translations object, initializing it if necessary.

Returns:



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

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