Module: Workbook::Modules::TypeParser

Included in:
Cell
Defined in:
lib/workbook/modules/type_parser.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#clean!(options = {}) ⇒ Object



50
51
52
# File 'lib/workbook/modules/type_parser.rb', line 50

def clean! options={}
  parse! options
end

#parse(options = {}) ⇒ Object

Returns the parsed value (retrieved by calling #value)

Returns:

  • (Object)

    The parsed object, ideally a date or integer when found to be a such…



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/workbook/modules/type_parser.rb', line 34

def parse options={}
  options = {:detect_date=>false}.merge(options)
  string_parsers.push :string_optimistic_date_converter if options[:detect_date]
  v = value
  string_parsers_as_procs.each do |p|
    if v.is_a? String
      v = p.call(v)
    end
  end
  v
end

#parse!(options = {}) ⇒ Object



46
47
48
# File 'lib/workbook/modules/type_parser.rb', line 46

def parse! options={}
  self.value = parse(options)
end

#string_boolean_converterObject

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



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/workbook/modules/type_parser.rb', line 110

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_cleanerObject



54
55
56
57
58
59
# File 'lib/workbook/modules/type_parser.rb', line 54

def string_cleaner
  proc do |v|
    v = v.strip
    v.gsub('mailto:','')
  end
end

#string_integer_converterObject



67
68
69
70
71
72
73
74
75
# File 'lib/workbook/modules/type_parser.rb', line 67

def string_integer_converter
  proc do |v|
    if v.to_i.to_s == v
      v.to_i
    else
      v
    end
  end
end

#string_nil_converterObject



61
62
63
64
65
# File 'lib/workbook/modules/type_parser.rb', line 61

def string_nil_converter
  proc do |v|
    (v == "" ? nil : v)
  end
end

#string_optimistic_date_converterObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/workbook/modules/type_parser.rb', line 77

def string_optimistic_date_converter
  proc do |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
      # try strptime with format 'mm/dd/yyyy'
      if rv == v && /^\d{1,2}[\/-]\d{1,2}[\/-]\d{4}/ =~ v
        begin
          rv = Date.strptime(v, "%m/%d/%Y")
        rescue ArgumentError
        end
      end
    end
    rv
  end
end

#string_parsersArray<Symbol>

Return the different active string parsers

Returns:

  • (Array<Symbol>)

    A list of parsers



15
16
17
# File 'lib/workbook/modules/type_parser.rb', line 15

def string_parsers
  @string_parsers ||= [:string_cleaner,:string_nil_converter,:string_integer_converter,:string_boolean_converter]
end

#string_parsers=(parsers) ⇒ Array<Symbol>

Set the list of string parsers

Parameters:

  • parsers (Array<Symbol>)

    A list of parsers

Returns:

  • (Array<Symbol>)

    A list of parsers



22
23
24
# File 'lib/workbook/modules/type_parser.rb', line 22

def string_parsers= parsers
  @string_parsers = parsers
end

#string_parsers_as_procsArray<Proc>

Return the different active string parsers

Returns:

  • (Array<Proc>)

    A list of parsers as Procs



28
29
30
# File 'lib/workbook/modules/type_parser.rb', line 28

def string_parsers_as_procs
  string_parsers.collect{|c| c.is_a?(Proc) ? c : self.send(c)}
end

#strip_win_chars(csv_raw) ⇒ Object

Cleans a text file from all kinds of different ways of representing new lines

Parameters:

  • csv_raw (String)

    a raw csv string



9
10
11
# File 'lib/workbook/modules/type_parser.rb', line 9

def strip_win_chars csv_raw
  csv_raw.gsub(/(\n\r|\r\n|\r)/,"\n")
end