structure_digest 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8184011e1799c2b33e1341e3dad068f0aabd094c
4
- data.tar.gz: ba65e85584a22cbc2efac85705e0de0b6eb1b262
3
+ metadata.gz: 1979b197924b8145bd076f3c63d3b82c9c3d8ef5
4
+ data.tar.gz: 540556788eac03fc0fe2e1deed1b148af770d9de
5
5
  SHA512:
6
- metadata.gz: 0360f22b4bf9d8febe85219c0a0650c61538a20c8785e3746b63f2136a9cc278589c7729c65c291305f9b4330eea62058dda7d5e2e6f6fafe9384161e51d0852
7
- data.tar.gz: b4ad1f41ef59909ece4e1de10af1e951f16337270d5be9818509fcf0e12e2051a449b90cd115266c46d45127e79beeffed115270d02fbe5ba2b21f6188334629
6
+ metadata.gz: cc2c74da006206bcadab880e06711d015ae43f87a2347ae28d52bfdd8d2934082ace24762ba29973239fe6fc4eaf5090ea7f8ec6a30e2b5801db48d3eed71c1e
7
+ data.tar.gz: 0860b75d8c9583909ab14b7e60b7f30bf80f8d1e6e1297345d194d568d9bc3e7cfc56c2e46d703aee0edb8ec86d3d40835fe2b819ff68397ef8e90f9db547f12
@@ -1 +1 @@
1
- 2.0.0
1
+ 2.2.2
@@ -10,6 +10,10 @@ module StructureDigest
10
10
  self.class == other.class && @index == other.index
11
11
  end
12
12
 
13
+ def serialize
14
+ "[#{@index}]"
15
+ end
16
+
13
17
  alias :eql? :==
14
18
  def hash; [@index].hash; end
15
19
  def abstract
@@ -6,10 +6,29 @@ module StructureDigest
6
6
  @tree = opts[:tree] || false
7
7
  end
8
8
 
9
+ def self.diff(h1,h2)
10
+ paths1 = []
11
+ paths2 = []
12
+ gather_paths(h1, paths1)
13
+ paths1 = paths1.map(&:serialize)
14
+ gather_paths(h2, paths2)
15
+ paths2 = paths2.map(&:serialize)
16
+
17
+ added = (paths2 - paths1)
18
+ removed = (paths1 - paths2)
19
+ (added + removed).sort.map do |p|
20
+ if removed.include?(p)
21
+ "- #{p}"
22
+ elsif added.include?(p)
23
+ "+ #{p}"
24
+ end
25
+ end
26
+ end
27
+
9
28
  def injest_yml_files(file_paths)
10
29
  file_paths.each do |p|
11
30
  y = YAML.load_file(p)
12
- gather_paths(y, paths)
31
+ Digest.gather_paths(y, paths)
13
32
  end
14
33
  @abstract_paths = paths.map(&:abstract).uniq
15
34
  self
@@ -23,7 +42,7 @@ module StructureDigest
23
42
 
24
43
  def validate(hash)
25
44
  paths = []
26
- gather_paths(hash, paths)
45
+ Digest.gather_paths(hash, paths)
27
46
  paths.all? do |p|
28
47
  print '.'
29
48
  @abstract_paths.any?{|my_p| my_p.accepts(p) } && validators_for(p).all?{|v| v.call(p.last[:value]) }
@@ -82,19 +101,27 @@ module StructureDigest
82
101
  @validators
83
102
  end
84
103
 
85
- def gather_paths(node, solutions=[], partial_solution=SchemaParts::Path.new)
104
+ def self.gather_paths(node, solutions=[], partial_solution=SchemaParts::Path.new)
86
105
  if Array === node
87
- node.each.with_index do |e, i|
88
- gather_paths(e, solutions, partial_solution.add_part(SchemaParts::ArrayDereference.new(i)))
106
+ if node.empty?
107
+ solutions << (partial_solution.add_part(SchemaParts::Value.new(node)))
108
+ else
109
+ node.each.with_index do |e, i|
110
+ self.gather_paths(e, solutions, partial_solution.add_part(SchemaParts::ArrayDereference.new(i)))
111
+ end
89
112
  end
90
113
  elsif Hash === node
91
- node.each do |k,v|
92
- gather_paths(v, solutions, partial_solution.add_part(SchemaParts::HashDereference.new(k)))
114
+ if node.empty?
115
+ solutions << (partial_solution.add_part(SchemaParts::Value.new(node)))
116
+ else
117
+ node.each do |k,v|
118
+ self.gather_paths(v, solutions, partial_solution.add_part(SchemaParts::HashDereference.new(k)))
119
+ end
93
120
  end
94
121
  else
95
122
  solutions << (partial_solution.add_part(SchemaParts::Value.new(node)))
96
123
  end
97
- self
124
+ nil
98
125
  end
99
126
 
100
127
  def deserialize(orig_shorthand)
@@ -9,6 +9,10 @@ module StructureDigest
9
9
  AbstractPath.new(@parts.map(&:abstract).compact)
10
10
  end
11
11
 
12
+ def serialize
13
+ @parts.map(&:serialize).join
14
+ end
15
+
12
16
  def add_part(part)
13
17
  Path.new(@parts + [part])
14
18
  end
@@ -5,6 +5,10 @@ module StructureDigest
5
5
  @value = val
6
6
  end
7
7
 
8
+ def serialize
9
+ "=#{@value}"
10
+ end
11
+
8
12
  def abstract
9
13
  nil
10
14
  end
@@ -1,3 +1,3 @@
1
1
  module StructureDigest
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -2,11 +2,13 @@
2
2
  "firstName": "John",
3
3
  "lastName": "Smith",
4
4
  "age": 25,
5
+ "stuff": [],
5
6
  "address": {
6
7
  "streetAddress": "21 2nd Street",
7
8
  "city": "New York",
8
9
  "state": "NY",
9
- "postalCode": 10021
10
+ "postalCode": 10021,
11
+ "box": {}
10
12
  },
11
13
  "phoneNumbers": [
12
14
  {
@@ -38,6 +38,7 @@ HEREDOC
38
38
 
39
39
  it "works with JSON" do
40
40
  digest_fixture("sample.json").should == <<HEREDOC.chomp
41
+ .address.box
41
42
  .address.city
42
43
  .address.postalCode
43
44
  .address.state
@@ -47,12 +48,14 @@ HEREDOC
47
48
  .lastName
48
49
  .phoneNumbers[].number
49
50
  .phoneNumbers[].type
51
+ .stuff
50
52
  HEREDOC
51
53
  end
52
54
 
53
55
  it "works with JSON in tree format" do
54
56
  digest_fixture("sample.json", tree: true).should == <<HEREDOC.chomp
55
57
  .address
58
+ .box
56
59
  .city
57
60
  .postalCode
58
61
  .state
@@ -63,6 +66,41 @@ HEREDOC
63
66
  .phoneNumbers[]
64
67
  .number
65
68
  .type
69
+ .stuff
66
70
  HEREDOC
67
71
  end
72
+
73
+ it "works as a diff library" do
74
+ h1 = {
75
+ a: 1,
76
+ b: [{
77
+ c: 4,
78
+ d: 5
79
+ },
80
+ {
81
+ e: 6,
82
+ f: []
83
+ },
84
+ 3
85
+ ]
86
+ }
87
+ h2 = {
88
+ b: [{
89
+ c: 4,
90
+ d: 8,
91
+ f: 6
92
+ },
93
+ {
94
+ e: 9
95
+ },
96
+ 3,
97
+ {}
98
+ ]
99
+ }
100
+
101
+ diff = StructureDigest::Digest.diff(h1, h2)
102
+ expect(diff).to eq([
103
+ "- .a=1", "- .b[0].d=5", "+ .b[0].d=8", "+ .b[0].f=6", "- .b[1].e=6", "+ .b[1].e=9", "- .b[1].f=[]", "+ .b[3]={}"
104
+ ])
105
+ end
68
106
  end
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: structure_digest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serguei Filimonov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-12 00:00:00.000000000 Z
11
+ date: 2016-04-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Digests and lists all the paths through a nested dictionary
@@ -46,9 +46,9 @@ executables:
46
46
  extensions: []
47
47
  extra_rdoc_files: []
48
48
  files:
49
- - .gitignore
50
- - .ruby-gemset
51
- - .ruby-version
49
+ - ".gitignore"
50
+ - ".ruby-gemset"
51
+ - ".ruby-version"
52
52
  - Gemfile
53
53
  - LICENSE.txt
54
54
  - README.md
@@ -78,17 +78,17 @@ require_paths:
78
78
  - lib
79
79
  required_ruby_version: !ruby/object:Gem::Requirement
80
80
  requirements:
81
- - - '>='
81
+ - - ">="
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  required_rubygems_version: !ruby/object:Gem::Requirement
85
85
  requirements:
86
- - - '>='
86
+ - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
90
  rubyforge_project:
91
- rubygems_version: 2.2.1
91
+ rubygems_version: 2.4.5
92
92
  signing_key:
93
93
  specification_version: 4
94
94
  summary: run the binary on YAML files to check it out