Sha256: 811a2626009aa3f8bfaa514f90270c762d75bb9141931bd82eda06925ad82ebb

Contents?: true

Size: 1.42 KB

Versions: 15

Compression:

Stored size: 1.42 KB

Contents

module Eddy
  module Util
    # @!group Normalize

    # Given an Element Id (positive number under 1688, or I01-I64), returns a string sutable for use as a Ruby class name.
    #
    # @param id [String]
    # @return [String]
    def self.normalize_id(id)
      name_regex = /\A(?<prefix>[iI]{1})?(?<numbers>\d+)\Z/
      res = ""
      if matches = id.match(name_regex)
        if matches[:prefix]
          res << "I"
        else
          res << "E"
        end
        res << matches[:numbers]
      else
        raise Eddy::Errors::BuildError, "invalid element id"
      end
    end

    # Convert a string to [*PascalCase*](http://wiki.c2.com/?PascalCase), or *UpperCamelCase*
    # Remove dashes, slashes, underscores, spaces, periods, and parens from a string then titleize it.
    #
    # @param name [String]
    # @return [String]
    def self.normalize_name(name)
      return name.gsub(/\s*\(.*\)|[']/, "")
                 .gsub(%r{[.,_\-/]}, " ")
                 .split(" ")
                 .map(&:capitalize)
                 .join("")
    end

    # Convert a string to [*snake_case*](https://en.wikipedia.org/wiki/Snake_case)
    #
    # @param name [String]
    # @return [String]
    def self.snake_case(name)
      return name.gsub(/\s*\(.*\)|['.]/, "")
                 .gsub(%r{[,_\-/]}, " ")
                 .split(" ")
                 .map(&:downcase)
                 .join("_")
    end

    # @!endgroup Normalize
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
eddy-0.10.0 lib/eddy/util/normalize.rb
eddy-0.9.2 lib/eddy/util/normalize.rb
eddy-0.9.1 lib/eddy/util/normalize.rb
eddy-0.9.0 lib/eddy/util/normalize.rb
eddy-0.8.4 lib/eddy/util/normalize.rb
eddy-0.8.3 lib/eddy/util/normalize.rb
eddy-0.8.2 lib/eddy/util/normalize.rb
eddy-0.8.1 lib/eddy/util/normalize.rb
eddy-0.8.0 lib/eddy/util/normalize.rb
eddy-0.7.0 lib/eddy/util/normalize.rb
eddy-0.6.0 lib/eddy/util/normalize.rb
eddy-0.5.1 lib/eddy/util/normalize.rb
eddy-0.5.0 lib/eddy/util/normalize.rb
eddy-0.4.0 lib/eddy/util/normalize.rb
eddy-0.3.0 lib/eddy/util/normalize.rb