json-inference 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-inference (0.0.2)
4
+ json-inference (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -4,7 +4,7 @@ module JsonInference
4
4
  end
5
5
 
6
6
  def self.percent_string(numerator, denominator)
7
- "#{(numerator / denominator.to_f * 100).round}%"
7
+ sprintf("%.2f%", numerator / denominator.to_f * 100)
8
8
  end
9
9
 
10
10
  class Report
@@ -59,9 +59,11 @@ module JsonInference
59
59
  @reported_class = reported_class
60
60
  @size = 0
61
61
  @empties = 0
62
+ @values_by_count = Hash.new(0)
62
63
  end
63
64
 
64
65
  def <<(value)
66
+ @values_by_count[value] += 1
65
67
  @size += 1
66
68
  if [Array, String].include?(@reported_class)
67
69
  @empties += 1 if value.empty?
@@ -70,6 +72,13 @@ module JsonInference
70
72
 
71
73
  def to_s(all_values_count)
72
74
  str = "#{@reported_class}: #{JsonInference.percent_string(size, all_values_count)}"
75
+ primary_value_record = @values_by_count.detect { |value, count|
76
+ count / size.to_f >= 0.9
77
+ }
78
+ if primary_value_record
79
+ primary_value, count = primary_value_record
80
+ str << ", #{JsonInference.percent_string(count, size)} #{primary_value.inspect}"
81
+ end
73
82
  if [Array, String].include?(@reported_class)
74
83
  str << ", #{JsonInference.percent_string(@empties, size)} empty"
75
84
  end
@@ -1,5 +1,5 @@
1
1
  module Json
2
2
  module Inference
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -15,11 +15,11 @@ class JsonInferenceTestCase < Test::Unit::TestCase
15
15
 
16
16
  should "count selectors as part of the total" do
17
17
  assert_match(/:root > .foo/, @string)
18
- assert_match(/3\/3 \(100%\)/, @string)
18
+ assert_match(/3\/3 \(100.00%\)/, @string)
19
19
  end
20
20
 
21
21
  should "count classes per selector" do
22
- assert_match(/String: 100%/, @string)
22
+ assert_match(/String: 100.00%/, @string)
23
23
  end
24
24
 
25
25
  should "sort report by selector" do
@@ -36,7 +36,7 @@ class JsonInferenceTestCase < Test::Unit::TestCase
36
36
  end
37
37
 
38
38
  should "recognize date fields based on format" do
39
- assert_match(/Date: 100%/, @string)
39
+ assert_match(/Date: 100.00%/, @string)
40
40
  end
41
41
  end
42
42
 
@@ -49,7 +49,7 @@ class JsonInferenceTestCase < Test::Unit::TestCase
49
49
  end
50
50
 
51
51
  should "group boolean fields" do
52
- assert_match(/Boolean: 100%/, @string)
52
+ assert_match(/Boolean: 100.00%/, @string)
53
53
  end
54
54
  end
55
55
 
@@ -63,11 +63,11 @@ class JsonInferenceTestCase < Test::Unit::TestCase
63
63
 
64
64
  should "show full selectors" do
65
65
  assert_match(/:root > .embedded > .title/, @string)
66
- assert_match(/2\/2 \(100%\)/, @string)
66
+ assert_match(/2\/2 \(100.00%\)/, @string)
67
67
  end
68
68
 
69
69
  should "count classes per selector" do
70
- assert_match(/String: 100%/, @string)
70
+ assert_match(/String: 100.00%/, @string)
71
71
  end
72
72
 
73
73
  should "sort report by selector" do
@@ -75,7 +75,7 @@ class JsonInferenceTestCase < Test::Unit::TestCase
75
75
  end
76
76
 
77
77
  should "display count for the overall hash too" do
78
- assert_match(/:root > .embedded: 2\/2 \(100%\)/, @string)
78
+ assert_match(/:root > .embedded: 2\/2 \(100.00%\)/, @string)
79
79
  end
80
80
  end
81
81
 
@@ -88,7 +88,7 @@ class JsonInferenceTestCase < Test::Unit::TestCase
88
88
  end
89
89
 
90
90
  should "calculate percentages related to occurrences of the field" do
91
- assert_match(/String: 100%/, @string)
91
+ assert_match(/String: 100.00%/, @string)
92
92
  end
93
93
  end
94
94
 
@@ -101,8 +101,8 @@ class JsonInferenceTestCase < Test::Unit::TestCase
101
101
  end
102
102
 
103
103
  should "display all top-level classes" do
104
- assert_match(/Hash: 50%/, @string)
105
- assert_match(/String: 50%/, @string)
104
+ assert_match(/Hash: 50.00%/, @string)
105
+ assert_match(/String: 50.00%/, @string)
106
106
  end
107
107
 
108
108
  should "display sub nodes" do
@@ -123,7 +123,7 @@ class JsonInferenceTestCase < Test::Unit::TestCase
123
123
  end
124
124
 
125
125
  should "count types of children" do
126
- assert_match(/Fixnum: 100%/, @string)
126
+ assert_match(/Fixnum: 100.00%/, @string)
127
127
  end
128
128
  end
129
129
 
@@ -136,13 +136,13 @@ class JsonInferenceTestCase < Test::Unit::TestCase
136
136
  end
137
137
 
138
138
  should "count elements in each hash" do
139
- assert_match(/:root > .items:nth-child\(\) > .one: 4\/4 \(100%\)$/, @string)
140
- assert_match(/:root > .items:nth-child\(\) > .two: 4\/4 \(100%\)$/, @string)
139
+ assert_match(/:root > .items:nth-child\(\) > .one: 4\/4 \(100.00%\)$/, @string)
140
+ assert_match(/:root > .items:nth-child\(\) > .two: 4\/4 \(100.00%\)$/, @string)
141
141
  end
142
142
 
143
143
  should "count value classes in hashes too" do
144
- assert_match(/String: 75%/, @string)
145
- assert_match(/Fixnum: 25%/, @string)
144
+ assert_match(/String: 75.00%/, @string)
145
+ assert_match(/Fixnum: 25.00%/, @string)
146
146
  end
147
147
  end
148
148
 
@@ -169,7 +169,22 @@ class JsonInferenceTestCase < Test::Unit::TestCase
169
169
  end
170
170
 
171
171
  should "note how likely it is to be empty" do
172
- assert_match(/String: 100%, 67% empty/, @string)
172
+ assert_match(/String: 100.00%, 66.67% empty/, @string)
173
+ end
174
+ end
175
+
176
+ context "field with the same value most of the time" do
177
+ setup do
178
+ report = JsonInference.new_report
179
+ 19.times do
180
+ report << {something: true}
181
+ end
182
+ report << {something: false}
183
+ @string = report.to_s
184
+ end
185
+
186
+ should "note that the value is almost always the same" do
187
+ assert_match(/Boolean: 100.00%, 95.00% true/, @string)
173
188
  end
174
189
  end
175
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-inference
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-21 00:00:00.000000000 Z
12
+ date: 2014-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -80,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
80
80
  version: '0'
81
81
  segments:
82
82
  - 0
83
- hash: -3344776264180278235
83
+ hash: -1108998192670081338
84
84
  required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  none: false
86
86
  requirements:
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  segments:
91
91
  - 0
92
- hash: -3344776264180278235
92
+ hash: -1108998192670081338
93
93
  requirements: []
94
94
  rubyforge_project:
95
95
  rubygems_version: 1.8.23