Sha256: a842df74b7e2c5201950777c350a6be1b31cfd5850c82413a4663007f931c908

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module DataMapper
  module Support
    module String
      
      # I set the constant on the String itself to avoid inheritance chain lookups.
      def self.included(base)
        base.const_set('EMPTY', ''.freeze)
      end
      
      def ensure_starts_with(part)
        [0,1] == part ? self : (part + self)
      end
  
      def ensure_ends_with(part)
        [-1,1] == part ? self : (self + part)
      end
  
      def ensure_wrapped_with(a, b = nil)
        ensure_starts_with(a).ensure_ends_with(b || a)
      end
      
      # Matches any whitespace (including newline) and replaces with a single space
      # EXAMPLE:
      #   <<QUERY.compress_lines
      #     SELECT name
      #     FROM users
      #   QUERY
      #   => "SELECT name FROM users"
      def compress_lines
        gsub(/\s+/, ' ').strip
      end
      
    end # module String
  end # module Support
end # module DataMapper

class String #:nodoc:
  include DataMapper::Support::String
  
  def self.fragile_underscore(camel_cased_word)
    camel_cased_word.gsub(/([a-z\d])([A-Z])/,'\1_\2').downcase
  end
  
  @underscore_cache = Hash.new { |h,k| h[k.freeze] = fragile_underscore(k) }
  
  def self.memoized_underscore(camel_cased_word)
    @underscore_cache[camel_cased_word]
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.1.1 lib/data_mapper/support/string.rb