Class: String

Inherits:
Object show all
Defined in:
lib/quickbooks/ruby_ext.rb,
lib/quickbooks/extlib/string.rb

Instance Method Summary collapse

Instance Method Details

#camel_caseString

Convert to camel case.

"foo_bar".camel_case          #=> "FooBar"

Returns:

  • (String)

    Receiver converted to camel case.



28
29
30
31
# File 'lib/quickbooks/extlib/string.rb', line 28

def camel_case
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  split('_').map{|e| e.capitalize}.join
end

#countr(regexp) ⇒ Object



51
52
53
54
55
# File 'lib/quickbooks/ruby_ext.rb', line 51

def countr(regexp)
  i = 0
  gsub(regexp) {|m| i += 1}
  i
end

#snake_caseString

Convert to snake case.

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

Returns:

  • (String)

    Receiver converted to snake case.



14
15
16
17
18
# File 'lib/quickbooks/extlib/string.rb', line 14

def snake_case
  return self.downcase if self =~ /^[A-Z]+$/
  self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
    return $+.downcase
end

#unescape_xmlObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/quickbooks/ruby_ext.rb', line 69

def unescape_xml
  gsub(/&(amp|quot|gt|lt|\#[0-9]+|\#x[0-9A-Fa-f]+);/n) do
    match = $1.dup
    case match
    when 'amp'                 then '&'
    when 'quot'                then '"'
    when 'gt'                  then '>'
    when 'lt'                  then '<'
    when /\A#0*(\d+)\z/n       then
      if Integer($1) < 256
        Integer($1).chr
      else
        if Integer($1) < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
          [Integer($1)].pack("U")
        else
          "&##{$1};"
        end
      end
    when /\A#x([0-9a-f]+)\z/ni then
      if $1.hex < 256
        $1.hex.chr
      else
        if $1.hex < 65536 and ($KCODE[0] == ?u or $KCODE[0] == ?U)
          [$1.hex].pack("U")
        else
          "&#x#{$1};"
        end
      end
    else
      "&#{match};"
    end
  end
end

#xml_escapedObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/quickbooks/ruby_ext.rb', line 57

def xml_escaped
  gsub(/[&<>'"]/) {|match|
    case match
    when '&' then return '&amp;'
    when '<' then return '&lt;'
    when '>' then return '&gt;'
    when "'" then return '&apos;'
    when '"' then return '&quote;'
    end
  }
end