module Datatypes class Classify require 'date' attr_accessor :object DATA_CHECKS = ["integer", "float", "date", "text"] def initialize(param) @object = param[:object] if param.has_key? :object @enumerable = param[:enumerable] if param.has_key? :enumerable end def checks DATA_CHECKS end def identify checks.each do |check| result = self.send(:"is_#{check}?") return result if result.has_key? :type end end def identify_distributed return is_text? if @enumerable.empty? return is_text? unless @enumerable.is_a?(Array) result = {} @enumerable.each do |variable| @object = variable if identify.has_key? :type result[identify[:type].to_sym] ||= 0 result[identify[:type].to_sym] += 1 end end {:type => result.max_by{ |key, value| value }[0].to_s} end def is_float? result = !(object !~ /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/) result ? {:type => "Float", :value => object.to_f, :short => 'to_f'} : {} end def is_integer? result = !(object !~ /^[-+]?[0-9]+$/) result ? {:type => "Integer", :value => object.to_i, :short => 'to_i'} : {} end def is_date? result = !(object !~ /(\d{1,2}[-\/]\d{1,2}[-\/]\d{4})|(\d{4}[-\/]\d{1,2}[-\/]\d{1,2})/) result ? {:type => "Date", :value => Date.strptime(object, "%m/%d/%Y"), :short => 'to_d'} : {} end def is_text? {:type => "String", :value => object, :short => 'to_s'} end end end