Sha256: 40da6e7610610ac2ec1a77367ff078048a2479b5cc4ce0eb3b17f127cdc4fecd

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

module Blendris

  # This module provides a few utility methods that are used throughout Blendris.

  module Utils

    # Method lifted from Rails.
    def constantize(camel_cased_word)
      return if blank(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

    # Method lifted from Rails.
    def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
      if first_letter_in_uppercase
        lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
      else
        lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
      end
    end

    # Tests if the given object is blank.
    def blank(obj)
      return true if obj.nil?
      return obj.strip.empty? if obj.kind_of? String
      return obj.empty? if obj.respond_to? :empty?
      return false
    end

    def sanitize_key(key)
      key.to_s.gsub(/[\r\n\s]/, "_").gsub(/^:+|:+$/, "")
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blendris-0.0.4 lib/blendris/utils.rb
blendris-0.0.3 lib/blendris/utils.rb