| Module | MUES::UtilityFunctions |
| In: |
lib/mues/mixins.rb
(CVS)
|
Mixin module that adds various miscellaneous utility functions to the current scope.
Given a muesId, return the specified MUES::Object instance, or nil if no such object exists in the objectspace. Can only be used in $SAFE <= 2 and an untainted object.
# File lib/mues/mixins.rb, line 456 def getObjectByMuesId( objectId ) MUES::SafeCheckFunctions::checkTaintAndSafe() targetObject = nil ObjectSpace.each_object( MUES::Object ) {|obj| next unless obj.muesid == objectId targetObject = obj break } return targetObject end
Given a Ruby objectId, return the specified MUES::Object instance, or nil if no such object exists in the objectspace. Can only be used in $SAFE <= 2 and an untainted object.
# File lib/mues/mixins.rb, line 441 def getObjectByRubyId( objectId ) MUES::SafeCheckFunctions::checkTaintAndSafe() targetObject = nil ObjectSpace.each_object( MUES::Object ) {|obj| next unless obj.id == objectId targetObject = obj break } return targetObject end
Return a String containing a description of the specified number of seconds in the form: "/y/ years /d/ days /h/ hours /m/ minutes /s/ seconds". If includeZero is true, units that are zero are included; if it‘s false, they are omitted. If joinWithComma, the units will be separated by a comma in addition to the space.
# File lib/mues/mixins.rb, line 398 def timeDelta( seconds, includeZero=false, joinWithComma=true ) minuteSeconds = 60 hourSeconds = ( minuteSeconds * 60 ) daySeconds = ( hourSeconds * 24 ) yearSeconds = ( daySeconds * 365 ) part = Struct::new( :unit, :count ) parts = [] parts << part.new('year', (seconds/yearSeconds).to_i) seconds %= yearSeconds parts << part.new('day', (seconds/daySeconds).to_i) seconds %= daySeconds parts << part.new('hour', (seconds/hourSeconds).to_i) seconds %= hourSeconds parts << part.new('minute', (seconds/minuteSeconds).to_i) seconds %= minuteSeconds parts << part.new('second', seconds.to_i) joinStr = joinWithComma ? ', ' : ' ' return parts.find_all {|p| includeZero || p.count.nonzero?}. collect {|p| "%d %s%s" % [p.count, p.unit, p.count == 1 ? "" : "s"]}. join( joinStr ) end
Trim a string to the given maxLength (at maximum), appending an ellipsis if it was truncated.
# File lib/mues/mixins.rb, line 430 def trimString( string, maxLength=20 ) if string.length > maxLength return string[ 0, maxLength - 3 ] + "..." end return string end
Return an Array of untainted parts of the specified string after having been untainted with the given pattern (a Regexp object). The pattern should contain paren-groups for all the parts it wishes returned.
# File lib/mues/mixins.rb, line 387 def untaintString( string, pattern ) match = pattern.match( string ) or return nil parts = match.to_a[ 1 .. -1 ].collect{|part| part.untaint} end