leafcutter 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/leafcutter.rb +40 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8a92c7d2bf14a3da4e7fb5e81b88dfdcb01af76
|
4
|
+
data.tar.gz: 06d24317660b3245f04f08672672c9cdb171c519
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 511a4587b8f872efe0978e37c737c8796118f727b9431de1033eef48a4a40fad343cf414fa6f9e6519f263f8cf0dfab4c22b34fdc5babbc6f124f584ee54320c
|
7
|
+
data.tar.gz: 82a093973506f9ecded46a6acd2feddd90ab9b655c1536b07a121c380d85f21203c29d5eb94c74759d9b3ba66fca540e2d60dfdafd648f9fa8e77ec26c77860f
|
data/lib/leafcutter.rb
CHANGED
@@ -3,6 +3,15 @@ class Leafcutter
|
|
3
3
|
@data = json
|
4
4
|
end
|
5
5
|
|
6
|
+
def validate
|
7
|
+
status = { valid: true, errors: [] }
|
8
|
+
|
9
|
+
# This is like run except we don't know which routes we want
|
10
|
+
# Instead we want to explore all branches and report all the errors we find
|
11
|
+
|
12
|
+
return validate_branch @data, :condition, status
|
13
|
+
end
|
14
|
+
|
6
15
|
def run(params)
|
7
16
|
keys = @data.keys
|
8
17
|
|
@@ -16,6 +25,37 @@ class Leafcutter
|
|
16
25
|
|
17
26
|
private
|
18
27
|
|
28
|
+
def validate_branch data, node_type, status
|
29
|
+
keys = data.kind_of?(Hash) ? data.keys : []
|
30
|
+
|
31
|
+
unless keys.empty?
|
32
|
+
if node_type == :condition && keys.length > 1
|
33
|
+
status[:valid] = false
|
34
|
+
status[:errors].push('multiple_conditions')
|
35
|
+
elsif node_type == :option && keys.length < 2
|
36
|
+
status[:valid] = false
|
37
|
+
status[:errors].push('single_option')
|
38
|
+
end
|
39
|
+
|
40
|
+
keys.each do |key|
|
41
|
+
next_node_type = node_type == :condition ? :option : :condition
|
42
|
+
validate_branch data[key], next_node_type, status
|
43
|
+
end
|
44
|
+
else
|
45
|
+
if data.kind_of?(Array)
|
46
|
+
if data.empty?
|
47
|
+
status[:valid] = false
|
48
|
+
status[:errors].push('empty_leaf')
|
49
|
+
end
|
50
|
+
else
|
51
|
+
status[:valid] = false
|
52
|
+
status[:errors].push('non_array_leaf')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
return status
|
57
|
+
end
|
58
|
+
|
19
59
|
def explore_branch data, key, params
|
20
60
|
branch = params[key]
|
21
61
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: leafcutter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Hastie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|