module Workbook::Modules::TypeParser

Adds type parsing capabilities to e.g. a Cell.

Public Instance Methods

clean!(options={}) click to toggle source
# File lib/workbook/modules/type_parser.rb, line 54
def clean! options={}
  parse! options
end
parse(options={}) click to toggle source

Returns the parsed value (retrieved by calling value) @return [Object] The parsed object, ideally a date or integer when found to be a such…

# File lib/workbook/modules/type_parser.rb, line 37
def parse options={}
  options = {:detect_date=>false, :convert_empty_to_nil=>true}.merge(options)
  string_parsers.push :string_optimistic_date_converter if options[:detect_date]
  string_parsers.push :string_nil_converter if options[:convert_empty_to_nil]
  v = value
  string_parsers_as_procs.each do |p|
    if v.is_a? String
      v = p.call(v)
    end
  end
  v
end
parse!(options={}) click to toggle source
# File lib/workbook/modules/type_parser.rb, line 50
def parse! options={}
  self.value = parse(options)
end
string_american_date_converter() click to toggle source
# File lib/workbook/modules/type_parser.rb, line 107
def string_american_date_converter
  proc do |v|
    if v
      rv = v
      # try strptime with format 'mm/dd/yyyy'
      if rv.is_a?(String) && /^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}/ =~ v
        begin
          rv = Date.strptime(v, "%m/%d/%Y")
        rescue ArgumentError
        end
      end
      rv
    end
  end
end
string_boolean_converter() click to toggle source

converts 'true' or 'false' strings in `true` or `false` values return [Proc] that returns a boolean value if it is considered as such

# File lib/workbook/modules/type_parser.rb, line 139
def string_boolean_converter
  proc do |v|
    dv = v.downcase
    if dv == "true"
      v = true
    elsif dv == "false"
      v = false
    end
    v
  end
end
string_cleaner() click to toggle source
# File lib/workbook/modules/type_parser.rb, line 58
def string_cleaner
  proc do |v|
    v = v.strip
    v.gsub('mailto:','')
  end
end
string_integer_converter() click to toggle source
# File lib/workbook/modules/type_parser.rb, line 71
def string_integer_converter
  proc do |v|
    if v.to_i.to_s == v
      v.to_i
    else
      v
    end
  end
end
string_nil_converter() click to toggle source
# File lib/workbook/modules/type_parser.rb, line 65
def string_nil_converter
  proc do |v|
    (v == "" ? nil : v)
  end
end
string_non_american_date_converter() click to toggle source
# File lib/workbook/modules/type_parser.rb, line 123
def string_non_american_date_converter
  proc do |v|
    rv = v
    # try strptime with format 'mm/dd/yyyy'
    if rv.is_a?(String) && /^\d{1,2}[\/\-\.]\d{1,2}[\/\-\.]\d{4}/ =~ v
      begin
        rv = Date.strptime(v, "%m/%d/%Y")
      rescue ArgumentError
      end
    end
    rv
  end
end
string_optimistic_date_converter() click to toggle source
# File lib/workbook/modules/type_parser.rb, line 81
def string_optimistic_date_converter
  proc do |v|
    if v
      rv = v
      starts_with_nr = v.chars.first.to_i.to_s == v.chars.first #it should at least start with a number...
      no_spaced_dash = v.to_s.match(" - ") ? false : true
      min_two_dashes = v.to_s.scan("-").count > 1 ? true : false
      min_two_dashes = v.to_s.scan("/").count > 1 ? true : false if min_two_dashes == false

      normal_date_length = v.to_s.length <= 25
      if no_spaced_dash and starts_with_nr and normal_date_length and min_two_dashes
        begin
          rv = (v.length > 10) ? DateTime.parse(v) : Date.parse(v)
        rescue ArgumentError
          rv = v
        end
        begin
          rv = Date.parse(v.to_i.to_s) == rv ? v : rv # disqualify if it is only based on the first number
        rescue ArgumentError
        end
      end
      rv
    end
  end
end
string_parsers() click to toggle source

Return the different active string parsers @return [Array<Symbol>] A list of parsers

# File lib/workbook/modules/type_parser.rb, line 18
def string_parsers
  @string_parsers ||= [:string_cleaner,:string_integer_converter,:string_boolean_converter]
end
string_parsers=(parsers) click to toggle source

Set the list of string parsers @param [Array<Symbol>] parsers A list of parsers @return [Array<Symbol>] A list of parsers

# File lib/workbook/modules/type_parser.rb, line 25
def string_parsers= parsers
  @string_parsers = parsers
end
string_parsers_as_procs() click to toggle source

Return the different active string parsers @return [Array<Proc>] A list of parsers as Procs

# File lib/workbook/modules/type_parser.rb, line 31
def string_parsers_as_procs
  string_parsers.collect{|c| c.is_a?(Proc) ? c : self.send(c)}
end
strip_win_chars(csv_raw) click to toggle source

Cleans a text file from all kinds of different ways of representing new lines @param [String] csv_raw a raw csv string

# File lib/workbook/modules/type_parser.rb, line 12
def strip_win_chars csv_raw
  csv_raw.gsub(/(\n\r|\r\n|\r)/,"\n")
end