Module: Workbook::Modules::TypeParser

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) clean!(options = {})



36
37
38
# File 'lib/workbook/modules/type_parser.rb', line 36

def clean! options={}
  parse! options
end

- (Object) parse(options = {})



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/workbook/modules/type_parser.rb', line 20

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

- (Object) parse!(options = {})



32
33
34
# File 'lib/workbook/modules/type_parser.rb', line 32

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

- (Object) string_boolean_converter



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/workbook/modules/type_parser.rb', line 84

def string_boolean_converter
  proc do |v|
    dv = v.downcase
    if dv == "true"
      return true
    elsif dv == "false"
      return false
    end
    v
  end
end

- (Object) string_cleaner



40
41
42
43
44
45
# File 'lib/workbook/modules/type_parser.rb', line 40

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

- (Object) string_integer_converter



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

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

- (Object) string_nil_converter



47
48
49
50
51
# File 'lib/workbook/modules/type_parser.rb', line 47

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

- (Object) string_optimistic_date_converter



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/workbook/modules/type_parser.rb', line 63

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
    normal_date_length = v.to_s.length <= 25
    if no_spaced_dash and starts_with_nr and normal_date_length
      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 is it is only based on the first number
      rescue ArgumentError
      end
    end          
    rv
  end
end

- (Object) string_parsers



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

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

- (Object) string_parsers=(arr)



12
13
14
# File 'lib/workbook/modules/type_parser.rb', line 12

def string_parsers= arr
  @string_parsers = arr
end

- (Object) string_parsers_as_procs



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

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

- (Object) strip_win_chars(csv_raw)



4
5
6
# File 'lib/workbook/modules/type_parser.rb', line 4

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