babel_diff 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/babel_diff/version.rb +1 -1
- data/lib/babel_diff/yaml_differ.rb +44 -47
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 636cfb635813720ebceed48ded1eea465486bcde
|
4
|
+
data.tar.gz: a645857700b111c53dd4f467e87e9cd66a247e97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db1ff545db9294c18c2261482b90922cc71f800806a8fef30463a53af59718c54d3d71087cc78d0db887614e1ab4f874a88fec221922b6f9e706194dc70c0f0c
|
7
|
+
data.tar.gz: ee0c4a2297c52df41a54e851afeb89bfa42b9cce3c6d6e4086d1e4abab70b130be44c1f4a1beedcecaa83f1dbfe02c7efbc3b3ef72bed39b06e3a2897ee6d3e3
|
data/lib/babel_diff/version.rb
CHANGED
@@ -4,71 +4,48 @@ require 'pry'
|
|
4
4
|
module BabelDiff
|
5
5
|
class YamlDiffer < Struct.new(:current_version, :previous_version)
|
6
6
|
def updates
|
7
|
-
process_difference
|
7
|
+
process_difference unless @processed
|
8
8
|
@processed = true
|
9
|
-
|
9
|
+
|
10
|
+
unflatten(updates_hash).to_yaml
|
10
11
|
end
|
11
12
|
|
12
13
|
def additions
|
13
|
-
process_difference
|
14
|
+
process_difference unless @processed
|
14
15
|
@processed = true
|
15
|
-
|
16
|
+
|
17
|
+
unflatten(additions_hash).to_yaml
|
16
18
|
end
|
17
19
|
|
18
|
-
def process_difference
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
add_key_to_updates(keys, value)
|
28
|
-
end
|
20
|
+
def process_difference
|
21
|
+
current = HashFlattener.new(current_version_hash).flatten
|
22
|
+
previous = HashFlattener.new(previous_version_hash).flatten
|
23
|
+
|
24
|
+
current.each do |k,v|
|
25
|
+
if ! previous.has_key?(k)
|
26
|
+
additions_hash[k] = v
|
27
|
+
elsif previous[k] != v
|
28
|
+
updates_hash[k] = v
|
29
29
|
end
|
30
|
-
keys.pop
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
|
-
def previous_version_is_missing_key?(keys)
|
35
|
-
current_value = previous_version_hash
|
36
|
-
keys.each do |key|
|
37
|
-
current_value = current_value[key]
|
38
|
-
return true if current_value.nil?
|
39
|
-
end
|
40
33
|
|
41
|
-
|
42
|
-
end
|
34
|
+
private
|
43
35
|
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
36
|
+
def unflatten(hash)
|
37
|
+
{}.tap do |unflattened_hash|
|
38
|
+
hash.each do |k,v|
|
39
|
+
keys = k.split(".")
|
40
|
+
current_hash = unflattened_hash
|
49
41
|
|
50
|
-
|
51
|
-
end
|
42
|
+
keys[0...-1].each { |key| current_hash = current_hash[key] ||= {} }
|
52
43
|
|
53
|
-
|
54
|
-
|
55
|
-
keys[0...-1].each do |key|
|
56
|
-
current_value = current_value[key] ||= {}
|
57
|
-
end
|
58
|
-
current_value[keys.last] = value
|
59
|
-
end
|
60
|
-
|
61
|
-
def add_key_to_additions(keys, value)
|
62
|
-
current_value = additions_hash
|
63
|
-
keys[0...-1].each do |key|
|
64
|
-
current_value = current_value[key] ||= {}
|
44
|
+
current_hash[keys.last] = v
|
45
|
+
end
|
65
46
|
end
|
66
|
-
current_value[keys.last] = value
|
67
47
|
end
|
68
48
|
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
49
|
def updates_hash
|
73
50
|
@updated_hash ||= {}
|
74
51
|
end
|
@@ -85,4 +62,24 @@ module BabelDiff
|
|
85
62
|
YAML.load(current_version) || {}
|
86
63
|
end
|
87
64
|
end
|
65
|
+
|
66
|
+
class HashFlattener < Struct.new(:hash)
|
67
|
+
|
68
|
+
def flatten(current_hash = hash, keys = [])
|
69
|
+
current_hash.each do |key, value|
|
70
|
+
new_keys = keys.dup << key
|
71
|
+
if value.is_a? Hash
|
72
|
+
flatten(value, new_keys)
|
73
|
+
else
|
74
|
+
flat_hash[new_keys.join(".")] = value
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
flat_hash
|
79
|
+
end
|
80
|
+
|
81
|
+
def flat_hash
|
82
|
+
@_flat_hash ||= {}
|
83
|
+
end
|
84
|
+
end
|
88
85
|
end
|