Module: Workbook::Modules::TypeParser
- Included in:
- Cell
- Defined in:
- lib/workbook/modules/type_parser.rb
Instance Method Summary (collapse)
- - (Object) clean!(options = {})
- - (Object) parse(options = {})
- - (Object) parse!(options = {})
- - (Object) string_boolean_converter
- - (Object) string_cleaner
- - (Object) string_integer_converter
- - (Object) string_nil_converter
- - (Object) string_optimistic_date_converter
- - (Object) string_parsers
- - (Object) string_parsers=(arr)
- - (Object) string_parsers_as_procs
- - (Object) strip_win_chars(csv_raw)
Instance Method Details
- (Object) clean!(options = {})
36 37 38 |
# File 'lib/workbook/modules/type_parser.rb', line 36 def clean! ={} parse! 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 ={} = {:detect_date=>false}.merge() string_parsers.push :string_optimistic_date_converter if [: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! ={} self.value = parse() 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 |