Class: MobyUtil::KernelHelper

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

Class Method Summary collapse

Class Method Details

.boolean?(value) ⇒ Boolean

Function to determine if given value is boolean

params

value

String containing boolean

returns

TrueClass
FalseClass

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/tdriver/util/common/kernel.rb', line 97

def self.boolean?( value )

  /^(true|false)$/i.match( value.to_s ).kind_of?( MatchData ) rescue false

end

.deprecated(deprecated_name, new_name = "") ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/tdriver/util/common/kernel.rb', line 148

def self.deprecated( deprecated_name, new_name = "" )

  output = "warning: #{ deprecated_name } is deprecated"

  output += "; use #{ new_name } instead" unless new_name.empty?

  $stderr.puts output

end

.find_source(backtrace) ⇒ Object

Searches for the given source file for a line

params

from_file

String defining the file to load. If at_line is nil, this argument can also contain a path and line number separated by : (eg. some_dir/some_file.rb:123).

at_line

(optional) Integer, number of the line (first line is 1).

returns

String

Contents of the line

throws

RuntimeError

from_file is not correctly formed, the file cannot be loaded or the line cannot be found.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/tdriver/util/common/kernel.rb', line 167

def self.find_source( backtrace )

  result = "\n"

  begin

    # split with colon 
    call_stack = backtrace.to_s.split(':')
      
    # TODO: document me      
    line_number = ( call_stack.size == 2 ? call_stack[ 1 ].to_i : call_stack[ call_stack.size - 2 ] ).to_i

    file_path = ""
    
    # TODO: document me      
    if ( call_stack.size == 2 )
    
      file_path = call_stack[ 0 ]
      
    else
    
      # TODO: document me      
      ( call_stack.size - 2 ).times do | index |
      
        file_path << "#{ call_stack[ index ].to_s }:"
        
      end
      
      # remove the trailing colon
      file_path.slice!(-1)
      
    end
    
    # TODO: document me      
    lines_to_read = line_number >= 2 ? 3 : line_number
    #puts "lines to read: " << lines_to_read.to_s

    # TODO: document me      
    start_line = line_number #- (lines_to_read <= 1 ? 0 : 1)
    #puts "start line:" << start_line.to_s

    # expand file path and name 
    filename = File.expand_path( file_path.to_s )

    # open source file
    File.open( filename, "r") { | source |

      # read lines
      lines = source.readlines
      
      # raise exception if line number is larger than total number of lines
      raise RuntimeError.new(
      
        "Unable to fetch line #{ start_line.to_s } from source file #{ filename } due to it is out of range (total lines: #{ lines.size })"
        
      ) if start_line > lines.size

      # TODO: document me
      lines_to_read = ( lines.size - start_line + 1 ) < 3 ? ( lines.size - start_line + 1 ) : lines_to_read
      
      # the array is zero based, first line is at position 0
      lines_to_read.times do | index |

        # add "=>" to line which failed                       
        result << ( ( line_number == ( start_line + index ) ) ? "=> " : "   " ) + lines[ start_line + index - 1 ]
        
      end

    }

  rescue Exception => e

    result << "Unable to load source lines.\n#{ e.inspect }"

  end

  # return result string
  result

end

.get_constant(constant_name) ⇒ Object

Function to return class constant from a string

params

constant_name

String containing path

returns

Class



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tdriver/util/common/kernel.rb', line 120

def self.get_constant( constant_name )

  begin

    constant_name.split("::").inject( Kernel ){ | scope, const_name | scope.const_get( const_name ) }

  rescue 

    raise NameError, "invalid constant #{ constant_name }"

  end

end

.parse_caller(at) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/tdriver/util/common/kernel.rb', line 134

def self.parse_caller( at )

  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at

    file = Regexp.last_match[ 1 ]
    line = Regexp.last_match[ 2 ].to_i
    method = Regexp.last_match[ 3 ]

    [ file, line, method ]

  end

end

.to_boolean(value, default = nil) ⇒ Object

Function to return boolean of given value

params

value

String containing boolean

returns

TrueClass
FalseClass


109
110
111
112
113
# File 'lib/tdriver/util/common/kernel.rb', line 109

def self.to_boolean( value, default = nil )

  /^(true|false)$/i.match( value.to_s ) ? $1.downcase == 'true' : default

end