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



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

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…



37
38
39
40
41
42
43
44
45
46
47
48
# 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 = {}) ⇒ Object



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

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

#string_american_date_converterObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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_converterObject

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



139
140
141
142
143
144
145
146
147
148
149
# 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_cleanerObject



58
59
60
61
62
63
# 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_converterObject



71
72
73
74
75
76
77
78
79
# 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_converterObject



65
66
67
68
69
# 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_converterObject



123
124
125
126
127
128
129
130
131
132
133
134
135
# 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_converterObject



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
# 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_parsersArray<Symbol>

Return the different active string parsers

Returns:

  • (Array<Symbol>)

    A list of parsers



18
19
20
# 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) ⇒ Array<Symbol>

Set the list of string parsers

Parameters:

  • parsers (Array<Symbol>)

    A list of parsers

Returns:

  • (Array<Symbol>)

    A list of parsers



25
26
27
# File 'lib/workbook/modules/type_parser.rb', line 25

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



31
32
33
# 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) ⇒ Object

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

Parameters:

  • csv_raw (String)

    a raw csv string



12
13
14
# 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