class DataKit::CSV::FieldAnalysis

Attributes

field_name[R]
match_type[R]
row_count[R]
sample_count[R]
types[R]
values[R]

Public Class Methods

new(field_name, options = {}) click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 12
def initialize(field_name, options = {})
  @field_name = field_name

  @types, @values = {}, {}
  @row_count, @sample_count = 0, 0

  @match_type = options[:match_type] || :any

  Dataset::Field::Types.each do |type|
    @types[type] = []
  end
end

Public Instance Methods

has_only_numeric_types?() click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 69
def has_only_numeric_types?
  (type_list - [:integer, :number, :null]).length == 0
end
has_single_type?() click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 65
def has_single_type?
  (type_list - [:null]).length == 1
end
increment_sample() click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 29
def increment_sample
  @sample_count += 1
end
increment_total() click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 25
def increment_total
  @row_count += 1
end
insert(value) click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 33
def insert(value)
  value_type = Dataset::Field.type?(value)

  if match_type.nil? || match_type == :any
    insert_value_with_type(value, value_type)
  elsif value_type == match_type
    insert_value_with_type(value, value_type)
  end
end
type?() click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 43
def type?
  if has_single_type?
    type_list.first
  elsif has_only_numeric_types?
    :number
  else
    :string
  end
end
type_count(type) click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 57
def type_count(type)
  types[type].length
end
type_list() click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 61
def type_list
  types.keys.select{ |type| @types[type].length > 0 }
end
value_at(row_num) click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 53
def value_at(row_num)
  @values[row_num]
end

Private Instance Methods

insert_value_with_type(value, type) click to toggle source
# File lib/data_kit/csv/field_analysis.rb, line 74
def insert_value_with_type(value, type)
  @values[row_count] = value
  @types[type] << row_count
end