Class: Parse::Algorithm::Ver0_1_0
- Inherits:
-
Object
- Object
- Parse::Algorithm::Ver0_1_0
- Defined in:
- lib/parse/algorithm/ver0_1_0.rb
Constant Summary collapse
- NULL =
from bigml’s list
[ '', '-', '?', 'N/A', 'n/a', 'NULL', 'null', '#REF!', '#NAME?', 'NIL', 'nil', 'NA', 'na', '#VALUE!', '#NULL!', '00/00/00', '0000-00-00']
- REGION_DATE_FORMAT =
{ euro: ['%d-%m-%Y', '%d-%m-%y'], us: ['%m-%d-%Y', '%m-%d-%y'], iso: ['%Y-%m-%d', '%y-%m-%d'], # second one is silly }
- DATE_DETECT =
{ %r{\A0*[12]\d\d\d[\-/](?:(?:0[1-9])|(?:1[0-2]))[\-/][1-9]\d\z} => :iso, # $1 will be delimiter }
- EMPTY_OPTIONS =
{}
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
Instance Method Summary collapse
-
#initialize(raw, options = nil) ⇒ Ver0_1_0
constructor
A new instance of Ver0_1_0.
- #result ⇒ Object
Constructor Details
#initialize(raw, options = nil) ⇒ Ver0_1_0
Returns a new instance of Ver0_1_0.
17 18 19 20 |
# File 'lib/parse/algorithm/ver0_1_0.rb', line 17 def initialize(raw, = nil) @raw = raw @options = || EMPTY_OPTIONS end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/parse/algorithm/ver0_1_0.rb', line 16 def @options end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
15 16 17 |
# File 'lib/parse/algorithm/ver0_1_0.rb', line 15 def raw @raw end |
Instance Method Details
#result ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/parse/algorithm/ver0_1_0.rb', line 22 def result return raw unless raw.is_a? String memo = raw.strip return nil if NULL.include? memo return 1.0/0 if INFINITY.include? memo return -1.0/0 if NEG_INFINITY.include? memo return 0.0/0 if NAN.include? memo date_region = if [:date] [:date] else catch :hit do DATE_DETECT.each do |pattern, date_region| # binding.pry if memo.include?('2011-') if memo =~ pattern throw :hit, date_region end end nil end end if date_region.nil? and [:type] == Date date_region = :iso end if date_region yyyy, yy = REGION_DATE_FORMAT.fetch date_region is_yyyy = memo =~ /[1-9]\d\d\d/ memo.sub! /\A0+/, '' memo.gsub! '/', '-' if is_yyyy if memo.length < 10 and date_region == :iso return Date.parse(memo) else return Date.strptime(memo, yyyy) end else return Date.strptime(memo, yy) end end possible_numeric = nil not_numeric = nil certain_numeric = nil if [Numeric, Integer, Float].include?([:type]) certain_numeric = true possible_numeric = true not_numeric = false else # not_numeric ||= memo =~ /[1-9][^)\d_,%.eE]/ # has a dash in the middle not_numeric ||= memo.include?('_') not_numeric ||= memo =~ %r{[1-9][/-]\d} not_numeric ||= memo =~ /,\d{1,2}(?:[.\D]|\z)/ not_numeric ||= memo.scan(/[^\d_,%.eE]/).length > memo.scan(/[\d_,%.eE]/).length not_numeric ||= memo =~ /\A[^(+\-\$0-9%]/ # starts with letter or smth possible_numeric = !not_numeric end accounting_negative = nil percentage = nil if possible_numeric accounting_negative = memo =~ /\A[0$]*\([0$]*/ percentage = memo.end_with?('%') memo.sub! /%\z/, '' if percentage memo.delete!('()') if accounting_negative # accounting negative # in yaml 1.1, anything starting with zero is treated as octal... in 1.2, it's 0o memo.sub!(/0+/, '') if memo =~ /\A[+\-]?0+[+\-\$]?[1-9]+/ # leading zeros memo.delete!('$') if memo =~ /\A[+\-]?0*\$/ memo.sub!('D', 'e') if memo =~ /\A[+\-]?[\d.]+D[+\-]?[\d.]+\z/ # fortran double precision if memo.include?(',') a, b = memo.split('.', 2) a.delete! ',' memo = b ? [a, b].join('.') : a end end if certain_numeric memo.gsub! /[a-z]/i, '' end not_safe_for_yaml = nil not_safe_for_yaml ||= memo =~ /\A(on|off)\z/i not_safe_for_yaml ||= memo.include?('#') not_safe_for_yaml ||= memo =~ /\A[@&,]/ not_safe_for_yaml ||= not_numeric && memo.start_with?('0') not_safe_for_yaml ||= not_numeric && memo =~ /\A[^{\[]*\d[,_]/ #1,2,3, maybe a csv safe_for_yaml = !not_safe_for_yaml if safe_for_yaml begin memo = SafeYAML.load memo rescue Exception # Psych::SyntaxError will blow up plain rescue in 1.9.3 $stderr.puts "#{memo.inspect} => #{$!}" end end if possible_numeric case memo when /\A[+\-]?[\d._]+[eE][+\-]?[\d._]+\z/ # scientific notation memo = memo.to_f when /\A[+\-]?0o/ # octal per yaml 1.2 memo = memo.to_i 8 end end if memo.is_a?(String) # compress whitespace memo.gsub! /\s+/, ' ' end memo = memo / 100.0 if percentage memo = -memo if accounting_negative memo rescue if and [:ignore_error] # nothing to see here else raise "#{memo.inspect} => #{$!}" end end |