Sha256: 10417591120d0395d41e432fe21522bb37a209f381912f33139fd8fa6589cff6

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

module Diesel
  module Utils
    module Inflections

      def self.acronyms
        @@acronyms ||= {}
      end

      def self.acronym_regex
        @@acronym_regex ||= /(?=a)b/
      end

      def camelize(term, uppercase_first_letter = true)
        string = term.to_s
        if uppercase_first_letter
          string = string.sub(/^[a-z\d]*/) { ::Diesel::Utils::Inflections.acronyms[$&] || $&.capitalize }
        else
          string = string.sub(/^(?:#{::Diesel::Utils::Inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { $&.downcase }
        end
        string.gsub!(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{::Diesel::Utils::Inflections.acronyms[$2] || $2.capitalize}" }
        string.gsub!('/', '::')
        string
      end

      def underscore(camel_cased_word)
        word = camel_cased_word.to_s.gsub('::', '/')
        word.gsub!(/(?:([A-Za-z\d])|^)(#{::Diesel::Utils::Inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
        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

      def constantize(camel_cased_word)
        unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
          raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
        end

        Object.module_eval("::#{$1}", __FILE__, __LINE__)
      end
    end
  end
end

Diesel::Utils::Inflections.extend(Diesel::Utils::Inflections)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
diesel-api-dsl-0.1.5 lib/diesel/utils/inflections.rb
diesel-api-dsl-0.1.3 lib/diesel/utils/inflections.rb
diesel-api-dsl-0.1.2 lib/diesel/utils/inflections.rb
diesel-api-dsl-0.1.1 lib/diesel/utils/inflections.rb
diesel-api-dsl-0.1.0 lib/diesel/utils/inflections.rb