Module: StateMachina::Util

Defined in:
lib/state_machina/util.rb

Class Method Summary collapse

Class Method Details

.demodulize(path) ⇒ Object

Based on the implementation of ActiveSupport::Inflector#demodulize



8
9
10
11
12
13
14
15
16
# File 'lib/state_machina/util.rb', line 8

def self.demodulize(path)
  path = path.to_s

  if i = path.rindex("::")
    path[(i + 2)..-1]
  else
    path
  end
end

.normalized_klass_to_name(klass) ⇒ Object



3
4
5
# File 'lib/state_machina/util.rb', line 3

def self.normalized_klass_to_name(klass)
  underscore(demodulize(klass.name)).to_s
end

.underscore(camel_cased_word) ⇒ Object

Based on the implementation of ActiveSupport::Inflector#underscore



19
20
21
22
23
24
25
26
27
28
# File 'lib/state_machina/util.rb', line 19

def self.underscore(camel_cased_word)
  return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)

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