tree_delta 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/lib/tree_delta/intermediate.rb +17 -17
- data/lib/tree_delta/operation.rb +7 -7
- data/lib/tree_delta/sorter.rb +1 -1
- 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: 132981c95ed21fd232a44c1f93aca5477da0afa6
|
4
|
+
data.tar.gz: 591ba9d6faf95b30b565a4622e5224f6a6c6a384
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d43ea4013511758125d18b1ff31c38610d9c50f3142a58e248817fc012df0843f22d210be2b191e67ee70d4d55c726f3f3d6db28c756513cb108770c15bcd448
|
7
|
+
data.tar.gz: db22eb1b9992dd0c7da0ed491734f81905e7be29b67a67c999fa0adf0ff1d175704d913dfccd3d76d4f564e3362db31c82c0b23a2345f9464c8e7a347bc915e1
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
## Tree Delta
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/whichdigital/tree_delta.svg?branch=master)](https://travis-ci.org/whichdigital/tree_delta)
|
4
|
+
|
3
5
|
Calculates the minimum set of operations that transform one tree into another.
|
4
6
|
|
5
7
|
## Overview
|
@@ -49,7 +51,7 @@ value object for a node. This is where you'd store the node's attributes.
|
|
49
51
|
You must first define a node class with the following methods:
|
50
52
|
|
51
53
|
```ruby
|
52
|
-
#
|
54
|
+
#identity
|
53
55
|
Returns an identifier that uniquely identifies the node across trees.
|
54
56
|
|
55
57
|
#parent
|
@@ -100,7 +102,7 @@ An operation is a simple object that describes a transformation.
|
|
100
102
|
|
101
103
|
It can contain up to five pieces of information, as shown here:
|
102
104
|
|
103
|
-
| | type |
|
105
|
+
| | type | identity | value | parent | position |
|
104
106
|
| --------:|:--------:|:--------:|:--------:|:--------:|:--------:|
|
105
107
|
| create | ✓ | ✓ | ✓ | ✓ | ✓ |
|
106
108
|
| update | ✓ | ✓ | ✓ | | |
|
@@ -114,7 +116,7 @@ Here is an example:
|
|
114
116
|
operation.type
|
115
117
|
#=> :create
|
116
118
|
|
117
|
-
operation.
|
119
|
+
operation.identity
|
118
120
|
#=> "foo"
|
119
121
|
|
120
122
|
operation.value
|
@@ -11,9 +11,9 @@ class TreeDelta::Intermediate
|
|
11
11
|
additions.each do |node|
|
12
12
|
y.yield TreeDelta::Operation.new(
|
13
13
|
type: :create,
|
14
|
-
|
14
|
+
identity: node.identity,
|
15
15
|
value: node.value,
|
16
|
-
parent:
|
16
|
+
parent: parent_identity(node),
|
17
17
|
position: position(node)
|
18
18
|
)
|
19
19
|
end
|
@@ -25,7 +25,7 @@ class TreeDelta::Intermediate
|
|
25
25
|
updated_nodes.each do |node|
|
26
26
|
y.yield TreeDelta::Operation.new(
|
27
27
|
type: :update,
|
28
|
-
|
28
|
+
identity: node.identity,
|
29
29
|
value: node.value,
|
30
30
|
)
|
31
31
|
end
|
@@ -36,8 +36,8 @@ class TreeDelta::Intermediate
|
|
36
36
|
Enumerator.new do |y|
|
37
37
|
normalised_deletions.each do |node|
|
38
38
|
y.yield TreeDelta::Operation.new(
|
39
|
-
type:
|
40
|
-
|
39
|
+
type: :delete,
|
40
|
+
identity: node.identity
|
41
41
|
)
|
42
42
|
end
|
43
43
|
end
|
@@ -48,8 +48,8 @@ class TreeDelta::Intermediate
|
|
48
48
|
moves.each do |node|
|
49
49
|
unless previous_root?(node)
|
50
50
|
y.yield TreeDelta::Operation.new(
|
51
|
-
type:
|
52
|
-
|
51
|
+
type: :detach,
|
52
|
+
identity: node.identity
|
53
53
|
)
|
54
54
|
end
|
55
55
|
end
|
@@ -62,8 +62,8 @@ class TreeDelta::Intermediate
|
|
62
62
|
unless root?(node)
|
63
63
|
y.yield TreeDelta::Operation.new(
|
64
64
|
type: :attach,
|
65
|
-
|
66
|
-
parent:
|
65
|
+
identity: node.identity,
|
66
|
+
parent: parent_identity(node),
|
67
67
|
position: position(node)
|
68
68
|
)
|
69
69
|
end
|
@@ -106,11 +106,11 @@ class TreeDelta::Intermediate
|
|
106
106
|
end
|
107
107
|
|
108
108
|
def subtract(a, b)
|
109
|
-
a.reject { |e| b.any? { |f| e.
|
109
|
+
a.reject { |e| b.any? { |f| e.identity == f.identity } }
|
110
110
|
end
|
111
111
|
|
112
|
-
def
|
113
|
-
node && node.parent ? node.parent.
|
112
|
+
def parent_identity(node)
|
113
|
+
node && node.parent ? node.parent.identity : nil
|
114
114
|
end
|
115
115
|
|
116
116
|
def position(node)
|
@@ -128,11 +128,11 @@ class TreeDelta::Intermediate
|
|
128
128
|
|
129
129
|
def changed_parent?(from_node)
|
130
130
|
to_node = to_node_for(from_node)
|
131
|
-
to_node &&
|
131
|
+
to_node && parent_identity(to_node) != parent_identity(from_node)
|
132
132
|
end
|
133
133
|
|
134
134
|
def normalised_position_changes
|
135
|
-
groups = position_changes.group_by { |n|
|
135
|
+
groups = position_changes.group_by { |n| parent_identity(n) }
|
136
136
|
|
137
137
|
groups.map do |_, nodes|
|
138
138
|
TreeDelta::Normaliser.normalise_position_changes(nodes)
|
@@ -148,16 +148,16 @@ class TreeDelta::Intermediate
|
|
148
148
|
to_node = to_node_for(from_node)
|
149
149
|
|
150
150
|
to_node &&
|
151
|
-
|
151
|
+
parent_identity(from_node) == parent_identity(to_node) &&
|
152
152
|
position(from_node) != position(to_node)
|
153
153
|
end
|
154
154
|
|
155
155
|
def from_node_for(to_node)
|
156
|
-
from_nodes.detect { |n| n.
|
156
|
+
from_nodes.detect { |n| n.identity == to_node.identity }
|
157
157
|
end
|
158
158
|
|
159
159
|
def to_node_for(from_node)
|
160
|
-
to_nodes.detect { |n| n.
|
160
|
+
to_nodes.detect { |n| n.identity == from_node.identity }
|
161
161
|
end
|
162
162
|
|
163
163
|
def root?(to_node)
|
data/lib/tree_delta/operation.rb
CHANGED
@@ -1,20 +1,20 @@
|
|
1
1
|
class TreeDelta::Operation
|
2
2
|
|
3
|
-
attr_reader :type, :
|
3
|
+
attr_reader :type, :identity, :value, :parent, :position
|
4
4
|
|
5
|
-
def initialize(type:,
|
5
|
+
def initialize(type:, identity:, value: nil, parent: nil, position: nil)
|
6
6
|
@type = type
|
7
|
-
@
|
7
|
+
@identity = identity
|
8
8
|
@value = value if value
|
9
9
|
@parent = parent if parent
|
10
10
|
@position = position if position
|
11
11
|
end
|
12
12
|
|
13
13
|
def ==(other)
|
14
|
-
@type == other.type
|
15
|
-
@
|
16
|
-
@value == other.value
|
17
|
-
@parent == other.parent
|
14
|
+
@type == other.type &&
|
15
|
+
@identity == other.identity &&
|
16
|
+
@value == other.value &&
|
17
|
+
@parent == other.parent &&
|
18
18
|
@position == other.position
|
19
19
|
end
|
20
20
|
|
data/lib/tree_delta/sorter.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tree_delta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Patuzzo
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2015-
|
13
|
+
date: 2015-07-20 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|