Sha256: 93178adfe9b52ee60985ff7c2f6756bad8b9ff6d083bede56452feb3d7eeaf5a

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 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(spaced = true)
        split($/).map { |line| line.strip }.join(spaced ? ' ' : '')
      end
      
      def margin(indicator = nil)
        target = dup
        lines = target.split($/)
        
        if indicator.nil?
          min_margin = nil
          lines.each do |line|
            if line =~ /(\s+)/ && (min_margin.nil? || $1.size < min_margin)
              min_margin = $1.size
            end
          end

          lines.map do |line|
            line.sub(/^\s{#{min_margin}}/, '')
          end.join($/)
        else
          lines.map do |line|
            line.sub(/^.*?#{"\\" + indicator}/, '')
          end.join($/)
        end
      end
      
    end # module String
  end # module Support
end # module DataMapper

class String #:nodoc:
  include DataMapper::Support::String  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
datamapper-0.2.1 lib/data_mapper/support/string.rb
datamapper-0.2.2 lib/data_mapper/support/string.rb