Class: DataKit::CSV::FieldAnalysis

Inherits:
Object
  • Object
show all
Defined in:
lib/data_kit/csv/field_analysis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field_name, options = {}) ⇒ FieldAnalysis

Returns a new instance of FieldAnalysis.



12
13
14
15
16
17
18
19
20
21
22
23
# 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

Instance Attribute Details

#field_nameObject (readonly)

Returns the value of attribute field_name.



4
5
6
# File 'lib/data_kit/csv/field_analysis.rb', line 4

def field_name
  @field_name
end

#match_typeObject (readonly)

Returns the value of attribute match_type.



5
6
7
# File 'lib/data_kit/csv/field_analysis.rb', line 5

def match_type
  @match_type
end

#row_countObject (readonly)

Returns the value of attribute row_count.



9
10
11
# File 'lib/data_kit/csv/field_analysis.rb', line 9

def row_count
  @row_count
end

#sample_countObject (readonly)

Returns the value of attribute sample_count.



10
11
12
# File 'lib/data_kit/csv/field_analysis.rb', line 10

def sample_count
  @sample_count
end

#typesObject (readonly)

=> :string, …



7
8
9
# File 'lib/data_kit/csv/field_analysis.rb', line 7

def types
  @types
end

#valuesObject (readonly)

=> “2010-13-01”



8
9
10
# File 'lib/data_kit/csv/field_analysis.rb', line 8

def values
  @values
end

Instance Method Details

#has_only_numeric_types?Boolean

Returns:

  • (Boolean)


69
70
71
# 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?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/data_kit/csv/field_analysis.rb', line 65

def has_single_type?
  (type_list - [:null]).length == 1
end

#increment_sampleObject



29
30
31
# File 'lib/data_kit/csv/field_analysis.rb', line 29

def increment_sample
  @sample_count += 1
end

#increment_totalObject



25
26
27
# File 'lib/data_kit/csv/field_analysis.rb', line 25

def increment_total
  @row_count += 1
end

#insert(value) ⇒ Object



33
34
35
36
37
38
39
40
41
# 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?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



57
58
59
# File 'lib/data_kit/csv/field_analysis.rb', line 57

def type_count(type)
  types[type].length
end

#type_listObject



61
62
63
# 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) ⇒ Object



53
54
55
# File 'lib/data_kit/csv/field_analysis.rb', line 53

def value_at(row_num)
  @values[row_num]
end