lib/fat_table/column.rb in fat_table-0.2.6 vs lib/fat_table/column.rb in fat_table-0.2.7

- old
+ new

@@ -26,33 +26,32 @@ # Integer, String, etc. Thus, you can perform operations on the items, # perhaps after removing nils with +.items.compact+. attr_reader :items # Valid Column types as strings. - TYPES = %w(NilClass Boolean DateTime Numeric String).freeze + TYPES = %w[NilClass Boolean DateTime Numeric String].freeze # :category: Constructors # Create a new Column with the given +header+ and initialized with the given # +items+, as an array of either strings or ruby objects that are one of the # permissible types or strings parsable as one of the permissible types. If # no +items+ are passed, returns an empty Column to which items may be added - # with the Column#<< method. The item types must be one of the following types or - # strings parseable as one of them: + # with the Column#<< method. The item types must be one of the following + # types or strings parseable as one of them: # # Boolean:: # an object of type TrueClass or FalseClass or a string that is either # 't', 'true', 'y', 'yes', 'f', 'false', 'n', or 'no', in each case, # regardless of case. # # DateTime:: # an object of class Date, DateTime, or a string that matches # +/\d\d\d\d[-\/]\d\d?[-\/]\d\d?/+ and is parseable by DateTime.parse. # - # Numeric:: - # on object that is of class Numeric, or a string that looks - # like a number after removing '+$+', '+,+', and '+_+' as well as Rationals + # Numeric:: on object that is of class Numeric, or a string that looks like + # a number after removing '+$+', '+,+', and '+_+' as well as Rationals # in the form /<number>:<number>/ or <number>/<number>, where <number> # is an integer. # # String:: # if the object is a non-blank string that does not parse as any @@ -89,11 +88,12 @@ @raw_header else @raw_header.to_s.as_sym end @type = 'NilClass' - raise UserError, "Unknown column type '#{type}" unless TYPES.include?(@type.to_s) + msg = "unknown column type '#{type}" + raise UserError, msg unless TYPES.include?(@type.to_s) @items = [] items.each { |i| self << i } end ########################################################################## @@ -340,11 +340,12 @@ private def only_with(agg, *valid_types) return self if valid_types.include?(type) - raise UserError, "Aggregate '#{agg}' cannot be applied to a #{type} column" + msg = "aggregate '#{agg}' cannot be applied to a #{type} column" + raise UserError, msg end public ########################################################################## @@ -364,11 +365,12 @@ # Return a new Column appending the items of other to this Column's items, # checking for type compatibility. Use the header of this Column as the # header of the new Column. def +(other) - raise UserError, 'Cannot combine columns with different types' unless type == other.type + msg = 'cannot combine columns with different types' + raise UserError, msg unless type == other.type Column.new(header: header, items: items + other.items) end private @@ -399,60 +401,65 @@ convert_to_string(val) else bool_val end @type = - if new_val == true || new_val == false + if [true, false].include?(new_val) 'Boolean' elsif new_val.is_a?(Date) || new_val.is_a?(DateTime) 'DateTime' elsif new_val.is_a?(Numeric) 'Numeric' elsif new_val.is_a?(String) 'String' else - raise UserError, "Cannot add #{val} of type #{new_val.class.name} to a column" + msg = "can't add #{val} of type #{new_val.class.name} to a column" + raise UserError, msg end end new_val when 'Boolean' - if (val.is_a?(String) && val.blank? || val.nil?) + if val.is_a?(String) && val.blank? || val.nil? nil else new_val = convert_to_boolean(val) if new_val.nil? - raise UserError, "Attempt to add '#{val}' to a column already typed as #{type}" + msg = "attempt to add '#{val}' to a column already typed as #{type}" + raise UserError, msg end new_val end when 'DateTime' if val.blank? nil else new_val = convert_to_date_time(val) if new_val.nil? - raise UserError, "Attempt to add '#{val}' to a column already typed as #{type}" + msg = "attempt to add '#{val}' to a column already typed as #{type}" + raise UserError, msg end new_val end when 'Numeric' if val.blank? nil else new_val = convert_to_numeric(val) if new_val.nil? - raise UserError, "Attempt to add '#{val}' to a column already typed as #{type}" + msg = "attempt to add '#{val}' to a column already typed as #{type}" + raise UserError, msg end new_val end when 'String' if val.nil? nil else new_val = convert_to_string(val) if new_val.nil? - raise UserError, "Attempt to add '#{val}' to a column already typed as #{type}" + msg = "attempt to add '#{val}' to a column already typed as #{type}" + raise UserError, msg end new_val end else raise UserError, "Mysteriously, column has unknown type '#{type}'" @@ -464,19 +471,21 @@ # of case is assumed to be a boolean. def convert_to_boolean(val) return val if val.is_a?(TrueClass) || val.is_a?(FalseClass) val = val.to_s.clean return nil if val.blank? - if val =~ /\A(false|f|n|no)\z/i + if val.match?(/\A(false|f|n|no)\z/i) false - elsif val =~ /\A(true|t|y|yes)\z/i + elsif val.match?(/\A(true|t|y|yes)\z/i) true end end - IS0_DATE_RE = %r{\b(\d\d\d\d)[-/](\d\d?)[-/](\d\d?)\s*(T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)?\b} - AMR_DATE_RE = %r{\b(\d\d?)[-/](\d\d?)[-/](\d\d\d\d)\s*(T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)?\b} + IS0_DATE_RE = %r{\b(\d\d\d\d)[-/](\d\d?)[-/](\d\d?)\s* + (T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)?\b}x + AMR_DATE_RE = %r{\b(\d\d?)[-/](\d\d?)[-/](\d\d\d\d)\s* + (T\d\d:\d\d:\d\d(\+\d\d:\d\d)?)?\b}x # Convert the val to a DateTime if it is either a DateTime, a Date, 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 @@ -486,11 +495,11 @@ return val if val.is_a?(DateTime) return val if val.is_a?(Date) begin val = val.to_s.clean return nil if val.blank? - if val =~ IS0_DATE_RE + if val.match?(IS0_DATE_RE) val = DateTime.parse(val) elsif val =~ AMR_DATE_RE val = DateTime.new($3.to_i, $1.to_i, $2.to_i) else return nil @@ -500,10 +509,10 @@ rescue ArgumentError return nil end end - # Convert the val to a Numeric if is already a Numberic or is a String that + # Convert the val to a Numeric if is already a Numeric or is a String that # looks like one. Any Float is promoted to a BigDecimal. Otherwise return # nil. def convert_to_numeric(val) return BigDecimal.new(val, Float::DIG) if val.is_a?(Float) return val if val.is_a?(Numeric)