Sha256: 7db01c5d0b8cf9d6dd709aea53f007cfb22a90fa54c13856d6f3e5f4563197ea

Contents?: true

Size: 775 Bytes

Versions: 7

Compression:

Stored size: 775 Bytes

Contents

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

    # Based on the implementation of ActiveSupport::Inflector#demodulize
    def self.demodulize(path)
      path = path.to_s

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

    # Based on the implementation of ActiveSupport::Inflector#underscore
    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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
state_machina-0.1.6 lib/state_machina/util.rb
state_machina-0.1.5 lib/state_machina/util.rb
state_machina-0.1.4 lib/state_machina/util.rb
state_machina-0.1.3 lib/state_machina/util.rb
state_machina-0.1.2 lib/state_machina/util.rb
state_machina-0.1.1 lib/state_machina/util.rb
state_machina-0.1.0 lib/state_machina/util.rb