Sha256: 3022034974475f1815ca1ee9473fbb6e6b6021d7c430e2af8e84aad6ac8df5f2

Contents?: true

Size: 1.45 KB

Versions: 4

Compression:

Stored size: 1.45 KB

Contents

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.parse(object), :short => 'to_d'} : {}
		end

		def is_text?
			{:type => "String", :value => object, :short => 'to_s'}
		end

	end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
identify_datatype-0.0.6 lib/datatypes/classify.rb
identify_datatype-0.0.4 lib/datatypes/classify.rb
identify_datatype-0.0.3 lib/datatypes/classify.rb
identify_datatype-0.0.2 lib/datatypes/classify.rb