tree_structure_digest 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 41d5a4f7e0d216beb3825460ec803daba17e58ce
4
+ data.tar.gz: 951b266f1b45e3205a8904b1f5485efc1e7ba559
5
+ SHA512:
6
+ metadata.gz: eaa75ee033f765b0826264b7d917de6b92e1c6eb6153f062e6155211368c86510c1c445e62c9e29db440400aa0b1fb94b58650a09040c857ce3aba0deaf551b6
7
+ data.tar.gz: be606e2835481fc87331df89b2b4b62a84f6e27dae7b57d6af4c76b4c4024b5d9bd8c319fed8dee96008bc623f1dd27395feb36958e55105d1d31165453234c5
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tree_structure_digest.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Grafton and Sara Tansey
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # TreeStructureDigest
2
+
3
+ Run the binary with one or more YAML files to find out what it does
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'tree_structure_digest'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install tree_structure_digest
19
+
20
+ ## Usage
21
+
22
+ `tree_structure_digest example.yml`
23
+
24
+ ## Contributing
25
+
26
+ Could use a shorter name maybe
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require 'tree_structure_digest'
4
+ rescue LoadError
5
+ require 'rubygems'
6
+ require 'tree_structure_digest'
7
+ end
8
+
9
+ $h = TreeStructureDigest::Digest.new.injest_yml_files(ARGV)
10
+ puts $h.shorthand
@@ -0,0 +1,16 @@
1
+ module TreeStructureDigest
2
+ module SchemaParts
3
+ class AbstractArrayDereference
4
+ def ==(other)
5
+ self.class == other.class
6
+ end
7
+
8
+ def serialize
9
+ "[]"
10
+ end
11
+
12
+ alias :eql? :==
13
+ def hash; [].hash; end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,45 @@
1
+ module TreeStructureDigest
2
+ module SchemaParts
3
+ class AbstractPath
4
+ def initialize(abstract_parts)
5
+ @parts = abstract_parts
6
+ end
7
+
8
+ def serialize
9
+ @parts.map(&:serialize).join
10
+ end
11
+
12
+ def accepts(path)
13
+ self == path.abstract
14
+ end
15
+
16
+ def add_part(part)
17
+ AbstractPath.new(@parts + [part])
18
+ end
19
+
20
+ def self.from_shorthand(shorthand)
21
+ shorthand = orig_shorthand.clone
22
+ abstract_path = AbstractPath.new
23
+ while !shorthand.empty?
24
+ if key = shorthand[/^\.(\w+)/, 1]
25
+ abstract_path = abstract_path.add_part(HashDereference.new(key))
26
+ shorthand.sub!(/^\.\w+/, '')
27
+ elsif shorthand[/^\[\]/]
28
+ abstract_path = abstract_path.add_part(AbstractArrayDereference.new)
29
+ shorthand.sub!("[]", '')
30
+ else
31
+ raise "shorthand #{shorthand} isn't a valid path shorthand"
32
+ end
33
+ end
34
+ abstract_path
35
+ end
36
+
37
+ attr_reader :parts
38
+ def ==(other)
39
+ self.class == other.class && @parts == other.parts
40
+ end
41
+ alias :eql? :==
42
+ def hash; [@parts].hash; end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ module TreeStructureDigest
2
+ module SchemaParts
3
+ class ArrayDereference
4
+ attr_reader :index
5
+ def initialize(index)
6
+ @index = index
7
+ end
8
+
9
+ def ==(other)
10
+ self.class == other.class && @index == other.index
11
+ end
12
+
13
+ alias :eql? :==
14
+ def hash; [@index].hash; end
15
+ def abstract
16
+ AbstractArrayDereference.new
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,69 @@
1
+ require 'yaml'
2
+
3
+ module TreeStructureDigest
4
+ class Digest
5
+ def injest_yml_files(file_paths)
6
+ file_paths.each do |p|
7
+ y = YAML.load_file(p)
8
+ gather_paths(y, paths)
9
+ end
10
+ @abstract_paths = paths.map(&:abstract).uniq
11
+ self
12
+ end
13
+
14
+ def add_validation(shorthand, &validateFn)
15
+ raise "isn't applicable for core paths of schema" unless @abstract_paths.include? SchemaParts::AbstractPath.from_shorthand(shorthand)
16
+ validators[shorthand] ||= []
17
+ validators[shorthand] << validateFn
18
+ end
19
+
20
+ def validate(hash)
21
+ paths = []
22
+ gather_paths(hash, paths)
23
+ paths.all? do |p|
24
+ print '.'
25
+ @abstract_paths.any?{|my_p| my_p.accepts(p) } && validators_for(p).all?{|v| v.call(p.last[:value]) }
26
+ end.tap do
27
+ puts
28
+ end
29
+ end
30
+
31
+ def validators_for(p)
32
+ validators[p.abstract.serialize] || []
33
+ end
34
+
35
+ def shorthand
36
+ @abstract_paths.map(&:serialize).uniq
37
+ end
38
+
39
+ private
40
+
41
+ def paths
42
+ @paths ||= []
43
+ end
44
+
45
+ def validators
46
+ @validators ||= {}
47
+ @validators
48
+ end
49
+
50
+ def gather_paths(node, solutions=[], partial_solution=SchemaParts::Path.new)
51
+ if Array === node
52
+ node.each.with_index do |e, i|
53
+ gather_paths(e, solutions, partial_solution.add_part(SchemaParts::ArrayDereference.new(i)))
54
+ end
55
+ elsif Hash === node
56
+ node.each do |k,v|
57
+ gather_paths(v, solutions, partial_solution.add_part(SchemaParts::HashDereference.new(k)))
58
+ end
59
+ else
60
+ solutions << (partial_solution.add_part(SchemaParts::Value.new(node)))
61
+ end
62
+ self
63
+ end
64
+
65
+ def deserialize(orig_shorthand)
66
+ SchemaParts::AbstractPath.from_shorthand(orig_shorthand)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,23 @@
1
+ module TreeStructureDigest
2
+ module SchemaParts
3
+ class HashDereference
4
+ def initialize(key)
5
+ @key = key
6
+ end
7
+
8
+ def serialize
9
+ ".#{key}"
10
+ end
11
+
12
+ def abstract
13
+ self
14
+ end
15
+ attr_reader :key
16
+ def ==(other)
17
+ self.class == other.class && @key == other.key
18
+ end
19
+ alias :eql? :==
20
+ def hash; [@key].hash; end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,24 @@
1
+ module TreeStructureDigest
2
+ module SchemaParts
3
+ class Path
4
+ def initialize(parts=[])
5
+ @parts = parts
6
+ end
7
+
8
+ def abstract
9
+ AbstractPath.new(@parts.map(&:abstract).compact)
10
+ end
11
+
12
+ def add_part(part)
13
+ Path.new(@parts + [part])
14
+ end
15
+
16
+ attr_reader :parts
17
+ def ==(other)
18
+ self.class == other.class && @parts == other.parts
19
+ end
20
+ alias :eql? :==
21
+ def hash; [@parts].hash; end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,20 @@
1
+ module TreeStructureDigest
2
+ module SchemaParts
3
+ class Value
4
+ def initialize(val)
5
+ @value = val
6
+ end
7
+
8
+ def abstract
9
+ nil
10
+ end
11
+
12
+ attr_reader :value
13
+ def ==(other)
14
+ self.class == other.class && @value == other.value
15
+ end
16
+ alias :eql? :==
17
+ def hash; [@value].hash; end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module TreeStructureDigest
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "tree_structure_digest/version"
2
+ Dir[File.dirname(__FILE__) + '/tree_structure_digest/*.rb'].each {|file| require file }
3
+
4
+ module TreeStructureDigest
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tree_structure_digest/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tree_structure_digest"
8
+ spec.version = TreeStructureDigest::VERSION
9
+ spec.authors = ["Serguei Filimonov"]
10
+ spec.email = ["serguei.filimonov@gmail.com"]
11
+ spec.description = %q{Digests and lists all the paths through a nested dictionary}
12
+ spec.summary = %q{run the binary on YAML files to check it out}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tree_structure_digest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Serguei Filimonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Digests and lists all the paths through a nested dictionary
42
+ email:
43
+ - serguei.filimonov@gmail.com
44
+ executables:
45
+ - tree_structure_digest
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - .gitignore
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/tree_structure_digest
55
+ - lib/tree_structure_digest.rb
56
+ - lib/tree_structure_digest/abstract_array_dereference.rb
57
+ - lib/tree_structure_digest/abstract_path.rb
58
+ - lib/tree_structure_digest/array_dereference.rb
59
+ - lib/tree_structure_digest/digest.rb
60
+ - lib/tree_structure_digest/hash_dereference.rb
61
+ - lib/tree_structure_digest/path.rb
62
+ - lib/tree_structure_digest/value.rb
63
+ - lib/tree_structure_digest/version.rb
64
+ - tree_structure_digest.gemspec
65
+ homepage: ''
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.1.5
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: run the binary on YAML files to check it out
89
+ test_files: []
90
+ has_rdoc: