Module: Periodic::Duration
- Included in:
- Periodic
- Defined in:
- lib/periodic/duration.rb
Defined Under Namespace
Modules: Units Classes: Duration
Class Method Summary collapse
Class Method Details
.sanitize_formatted_string(string) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/periodic/duration.rb', line 18 def self.sanitize_formatted_string(string) if string.match(/:/) && !string.match(/[a-zA-Z ]/) # add leading zeros where missing... string.gsub!(/!(\d):/, '!0\1:') string.gsub!(/^(\d):/, '0\1:') string.gsub!(/:(\d):/, ':0\1:') string.gsub!(/:(\d):/, ':0\1:') # needs to happen twice?? string.gsub!(/:(\d(.\d)*)$/, ':0\1') # remove leading zero-value digitals string.sub!(/[0:]*/, '') else # if the string starts with a number we can assume the value-label pairs are like '10 minutes' if string[0,1].match(/\d/) || string[0,1] == "!" string = string.split(/(!?\d[.\d]*[-_:, a-zA-Z]+)/).delete_if{|x| x == ""}.inject(String.new) { |memo, s| memo << ((s.match(/!/) || s.match(/[1-9]/)) ? s : "") } # if starts with a letter we can assume the value-label pairs are like 'minutes: 10' else string = string.split(/([-A-Za-z: ,]+\d[.\d]*)/).delete_if{|x| x == ""}.inject(String.new) { |memo, s| memo << ((s.match(/!/) || s.match(/[1-9]/)) ? s : "") } string.sub!(/([ ,])*([a-zA-Z]+)/, '\2') end # remove leading zero-value digitals string.sub!(/[0:]*/, '') end string.strip.gsub(/!/, '') end |