Class: String

Inherits:
Object show all
Defined in:
lib/gci-class-extensions.rb

Instance Method Summary collapse

Instance Method Details

#escape_sqlObject

replaces ‘ with ” and \ (two backslashes) with \\ (two escaped backslashes)



62
63
64
# File 'lib/gci-class-extensions.rb', line 62

def escape_sql
  self.gsub(/\\/, '\&\&').gsub(/'/, "''")
end

#is_valid_dollar_amount?Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/gci-class-extensions.rb', line 47

def is_valid_dollar_amount?
  # Optional parentheses or negative sign before or after the optional dollar sign, any number of digits and commas, followed by optional . and 1 or 2 digits, and optional close parentheses, with nothing on either side
  strip =~ /^[\(-]?\$?[\(-]?([\d,]+(\.\d{1,2})?|\.\d{1,2})\)?$/ ? true : false
end

#scrubbed_dollar_amountObject



52
53
54
55
# File 'lib/gci-class-extensions.rb', line 52

def scrubbed_dollar_amount
  # first strip out dollar signs and commas, then convert "(12.34)" to "-12.34"
  strip.gsub(/[\$,]/,"").gsub(/^\((.*)\)$/, '-\1')
end

#scrubbed_dollar_amount_if_validObject



57
58
59
# File 'lib/gci-class-extensions.rb', line 57

def scrubbed_dollar_amount_if_valid
  is_valid_dollar_amount? ? scrubbed_dollar_amount : self
end