Class: JsonInference::NodeValuesCollection::ValueCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/json-inference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reported_class) ⇒ ValueCounter

Returns a new instance of ValueCounter.



58
59
60
61
62
63
# File 'lib/json-inference.rb', line 58

def initialize(reported_class)
  @reported_class = reported_class
  @size = 0
  @empties = 0
  @values_by_count = Hash.new(0)
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



56
57
58
# File 'lib/json-inference.rb', line 56

def size
  @size
end

Instance Method Details

#<<(value) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/json-inference.rb', line 65

def <<(value)
  @values_by_count[value] += 1
  @size += 1
  if [Array, String].include?(@reported_class)
    @empties += 1 if value.empty?
  end
end

#to_s(all_values_count) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/json-inference.rb', line 73

def to_s(all_values_count)
  str = "#{@reported_class}: #{JsonInference.percent_string(size, all_values_count)}"
  primary_value_record = @values_by_count.detect { |value, count|
    count / size.to_f >= 0.9
  }
  if primary_value_record
    primary_value, count = primary_value_record
    str << ", #{JsonInference.percent_string(count, size)} #{primary_value.inspect}"
  end
  if [Array, String].include?(@reported_class)
    str << ", #{JsonInference.percent_string(@empties, size)} empty"
  end
  str
end