Class: DataKit::CSV::FieldAnalysis
- Inherits:
-
Object
- Object
- DataKit::CSV::FieldAnalysis
- Defined in:
- lib/data_kit/csv/field_analysis.rb
Instance Attribute Summary collapse
-
#field_name ⇒ Object
readonly
Returns the value of attribute field_name.
-
#match_type ⇒ Object
readonly
Returns the value of attribute match_type.
-
#row_count ⇒ Object
readonly
Returns the value of attribute row_count.
-
#sample_count ⇒ Object
readonly
Returns the value of attribute sample_count.
-
#types ⇒ Object
readonly
=> :string, ….
-
#values ⇒ Object
readonly
=> “2010-13-01”.
Instance Method Summary collapse
- #has_only_numeric_types? ⇒ Boolean
- #has_single_type? ⇒ Boolean
- #increment_sample ⇒ Object
- #increment_total ⇒ Object
-
#initialize(field_name, options = {}) ⇒ FieldAnalysis
constructor
A new instance of FieldAnalysis.
- #insert(value) ⇒ Object
- #type? ⇒ Boolean
- #type_count(type) ⇒ Object
- #type_list ⇒ Object
- #value_at(row_num) ⇒ Object
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, = {}) @field_name = field_name @types, @values = {}, {} @row_count, @sample_count = 0, 0 @match_type = [:match_type] || :any Dataset::Field::Types.each do |type| @types[type] = [] end end |
Instance Attribute Details
#field_name ⇒ Object (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_type ⇒ Object (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_count ⇒ Object (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_count ⇒ Object (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 |
#types ⇒ Object (readonly)
=> :string, …
7 8 9 |
# File 'lib/data_kit/csv/field_analysis.rb', line 7 def types @types end |
#values ⇒ Object (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
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
65 66 67 |
# File 'lib/data_kit/csv/field_analysis.rb', line 65 def has_single_type? (type_list - [:null]).length == 1 end |
#increment_sample ⇒ Object
29 30 31 |
# File 'lib/data_kit/csv/field_analysis.rb', line 29 def increment_sample @sample_count += 1 end |
#increment_total ⇒ Object
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
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_list ⇒ Object
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 |