Module: Workbench::StringHelpers

Defined in:
lib/workbench/string_helpers.rb

Overview

A collection of needed String methods. They are here to remove a dependency on ActiveSupport. You can use them if you want… but you probably should use ActiveSupport or copy them into your project.

Class Method Summary collapse

Class Method Details

.classify(string) ⇒ Object

“transforms ‘namespace/model_name’ to ‘Namespace::ModelName’”



14
15
16
# File 'lib/workbench/string_helpers.rb', line 14

def self.classify(string)
  string.to_s.gsub("/", "::").gsub(/(_|\b)(.)/) { |r| r[-1..-1].upcase }
end

.constantize(value) ⇒ Object

takes :User, “user”, or :user and returns User



5
6
7
8
9
10
11
# File 'lib/workbench/string_helpers.rb', line 5

def self.constantize(value)
  return value if value.is_a?(Module)
  value = value.to_s
  value = classify(value) unless value =~ /^[A-Z]/
  value = "::#{value}" unless value =~ /^:/
  eval(value.to_s)
end