Module: Rseed::AttributeConverters
- Included in:
- Attribute
- Defined in:
- lib/rseed/attribute_converters.rb
Instance Method Summary collapse
- #deserialize_boolean(value) ⇒ Object
- #deserialize_clean_string(value) ⇒ Object
- #deserialize_date(s) ⇒ Object
- #deserialize_datetime(s) ⇒ Object
- #deserialize_decimal(value) ⇒ Object
- #deserialize_string(value) ⇒ Object
Instance Method Details
#deserialize_boolean(value) ⇒ Object
18 19 20 |
# File 'lib/rseed/attribute_converters.rb', line 18 def deserialize_boolean value /^y|t/.match(value.strip.downcase) ? true : false end |
#deserialize_clean_string(value) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rseed/attribute_converters.rb', line 11 def deserialize_clean_string(value) value = value.to_i if (value.to_i == value.to_f) if /^\s*[\d]+(\.0+){0,1}\s*$/.match(value.to_s) value = value.gsub(/[^A-Za-z0-9 \.,\?'""!@#\$%\^&\*\(\)-_=\+;:<>\/\\\|\}\{\[\]`~]/, '').strip if value.is_a?(String) return nil if value.to_s.blank? || value.to_s.nil? value.to_s end |
#deserialize_date(s) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rseed/attribute_converters.rb', line 26 def deserialize_date s return nil if (s.nil? || s.blank?) return Date.strptime(s, "%d/%m/%y") if /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2}$/.match(s) return DateTime.new(1899, 12, 30) + s.to_f if s.to_f unless s !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/ begin result = Date.parse(s) rescue Rseed.logger.error "Could not parse date ".red + "'#{s}'" end return result end |
#deserialize_datetime(s) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/rseed/attribute_converters.rb', line 39 def deserialize_datetime s begin result = Time.zone.parse(s) rescue Rseed.logger.error "Could not parse datetime ".red + "'#{s}'" end return result end |
#deserialize_decimal(value) ⇒ Object
22 23 24 |
# File 'lib/rseed/attribute_converters.rb', line 22 def deserialize_decimal value BigDecimal(value) end |
#deserialize_string(value) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/rseed/attribute_converters.rb', line 3 def deserialize_string(value) # If the string represents a number with .0 on the end (something that comes from Roo excel, then remove it) # this is a big problem with phone numbers. value.gsub!(/\.0+$/, '') if (value.to_i == value.to_f) if /^\s*[\d]+(\.0+){0,1}\s*$/.match(value.to_s) return nil if value.to_s.blank? || value.to_s.nil? value.to_s end |