lib/fat_table/column.rb in fat_table-0.3.1 vs lib/fat_table/column.rb in fat_table-0.3.3

- old
+ new

@@ -137,10 +137,23 @@ # Return the index of the last item in the Column. def last_i size - 1 end + # :category: Attributes + + # Force the column to have String type and then convert all items to + # strings. + def force_to_string_type + # msg = "Can only force an empty column to String type" + # raise UserError, msg unless empty? + @type = 'String' + unless empty? + @items = items.map(&:to_s) + end + end + ########################################################################## # Enumerable ########################################################################## include Enumerable @@ -492,28 +505,42 @@ ([-+](\d\d?)(:\d\d?))?)?}x AMR_DATE_RE = %r{(?<dy>\d\d?)[-/](?<mo>\d\d?)[-/](?<yr>\d\d\d\d)\s* (?<tm>T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)?}x + # A Date like 'Tue, 01 Nov 2016' or 'Tue 01 Nov 2016' or '01 Nov 2016'. + # These are emitted by Postgresql, so it makes from_sql constructor + # possible without special formatting of the dates. + INV_DATE_RE = %r{((mon|tue|wed|thu|fri|sat|sun)[a-zA-z]*,?)?\s+ # looks like dow + (?<dy>\d\d?)\s+ # one or two-digit day + (?<mo_name>[jfmasondJFMASOND][A-Za-z]{2,})\s+ # looks like a month name + (?<yr>\d\d\d\d) # and a 4-digit year + }xi + # Convert the val to a DateTime if it is either a DateTime, a Date, a Time, or a # String that can be parsed as a DateTime, otherwise return nil. It only # recognizes strings that contain a something like '2016-01-14' or '2/12/1985' # within them, otherwise DateTime.parse would treat many bare numbers as dates, # such as '2841381', which it would recognize as a valid date, but the user # probably does not intend it to be so treated. def convert_to_date_time(val) return val if val.is_a?(DateTime) return val if val.is_a?(Date) + return val.to_datetime if val.is_a?(Time) begin str = val.to_s.clean return nil if str.blank? if str.match(ISO_DATE_RE) date = DateTime.parse(val) elsif str =~ AMR_DATE_RE date = DateTime.new(Regexp.last_match[:yr].to_i, Regexp.last_match[:mo].to_i, Regexp.last_match[:dy].to_i) + elsif str =~ INV_DATE_RE + mo = Date.mo_name_to_num(last_match[:mo_name]) + date = DateTime.new(Regexp.last_match[:yr].to_i, mo, + Regexp.last_match[:dy].to_i) else return nil end # val = val.to_date if date.seconds_since_midnight.zero? ? date.to_date : date