Module: Spinach::Support

Defined in:
lib/spinach/support.rb

Overview

A module to offer helpers for string mangling.

Class Method Summary collapse

Class Method Details

.camelize(name) ⇒ String

Returns The name in camel case.

Examples:

Spinach::Support.camelize('User authentication')
=> 'UserAuthentication'

Parameters:

  • name (String)

    The name to camelize.

Returns:

  • (String)

    The name in camel case.



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

def self.camelize(name)
  name.to_s.strip.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
end

.constantize(string) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/spinach/support.rb', line 75

def self.constantize(string)
  names = string.split('::')
  names.shift if names.empty? || names.first.empty?

  constant = Object
  names.each do |name|
    constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
  end
  constant
end

.escape_single_commas(text) ⇒ String

Escapes the single commas of a given text. Mostly used in the Generators classes

Examples:

Spinach::Support.escape_single_commas("I've been bad")
# => "I\'ve been bad"

Parameters:

  • text (String)

    The text to escape

Returns:

  • (String)

    The text with escaped commas



71
72
73
# File 'lib/spinach/support.rb', line 71

def self.escape_single_commas(text)
  text.gsub("'", "\\\\'")
end

.scoped_camelize(name) ⇒ String

Returns The name in camel case scoped to Spinach::Features.

Examples:

Spinach::Support.scoped_camelize('User authentication')
=> 'Spinach::Features::UserAuthentication'

Parameters:

  • name (String)

    The name to camelize.

Returns:

  • (String)

    The name in camel case scoped to Spinach::Features.



31
32
33
# File 'lib/spinach/support.rb', line 31

def self.scoped_camelize(name)
  "Spinach::Features::#{camelize(name)}"
end

.underscore(camel_cased_word) ⇒ Object

Makes an underscored, lowercase form from the expression in the string.

Changes ‘::’ to ‘/’ to convert namespaces to paths.

Examples:

"ActiveRecord".underscore         # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors

As a rule of thumb you can think of underscore as the inverse of camelize, though there are cases where that does not hold:

"SSLError".underscore.camelize # => "SslError"


47
48
49
50
51
52
53
54
55
56
# File 'lib/spinach/support.rb', line 47

def self.underscore(camel_cased_word)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.tr!(" ", "_")
  word.downcase!
  word
end