Class: String

Inherits:
Object show all
Defined in:
lib/tdriver/util/common/string.rb

Overview

extend Ruby String class functionality

Instance Method Summary collapse

Instance Method Details

#boolean?Boolean

Function determines if string is “true” or “false”

params

string

String

returns

TrueClass/FalseClass

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/tdriver/util/common/string.rb', line 62

def boolean?

  /^(true|false)$/i.match( self ).kind_of?( MatchData )

end

#encode_to_xmlObject

TODO: document me



51
52
53
54
55
# File 'lib/tdriver/util/common/string.rb', line 51

def encode_to_xml

  gsub('&', '&amp;').gsub('<', '&lt;').gsub('>', '&gt;').gsub( '"', '&quot;' ).gsub( '\'', '&apos;' )

end

#false?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/tdriver/util/common/string.rb', line 29

def false?

  /^false$/i.match( to_s ) != nil

end

#not_empty(message = "String must not be empty", exception = ArgumentError) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tdriver/util/common/string.rb', line 35

def not_empty( message = "String must not be empty", exception = ArgumentError )

  if empty?

    # replace macros
    #message.gsub!( '$1', inspect )

    raise exception, message, caller 

  end

  self

end

#numeric?Boolean

Function determines if string is numeric

params

string

Numeric string

returns

TrueClass/FalseClass

Returns:

  • (Boolean)


73
74
75
76
77
# File 'lib/tdriver/util/common/string.rb', line 73

def numeric?

  /^[0-9]+$/.match( self ).kind_of?( MatchData )

end

#to_boolean(*default) ⇒ Object

Function converts “true” or “false” to boolean

params

string

String

returns

TrueClass/FalseClass



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tdriver/util/common/string.rb', line 84

def to_boolean( *default )

  if /^(true|false)$/i.match( to_s )
  
    $1.downcase == 'true'
    
  else
      
    # pass default value if string didn't contain boolean
    if default.count > 0
    
      # retrieve first value from array
      default = default.first
      
      # check that argument type is correct
      default.check_type( [ TrueClass, FalseClass ], 'wrong argument type $1 for to_boolean default value argument (expecting $2)') 
      
      # return default value as result
      default
      
    else
    
      # raise exception if no default given
      raise TypeError, "Unable to convert string \"#{ self }\" to boolean (Expected \"true\" or \"false\")", caller

    end

  end

end

#true?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/tdriver/util/common/string.rb', line 23

def true?

  /^true$/i.match( to_s ) != nil

end