tree_structure_digest 0.0.1 → 0.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 41d5a4f7e0d216beb3825460ec803daba17e58ce
4
- data.tar.gz: 951b266f1b45e3205a8904b1f5485efc1e7ba559
3
+ metadata.gz: 6feccfd4e179cbabb4602bbc2717af3af5845353
4
+ data.tar.gz: 8e4f1ae6ee53ad6c4a4f4548436e34a18dce7bb8
5
5
  SHA512:
6
- metadata.gz: eaa75ee033f765b0826264b7d917de6b92e1c6eb6153f062e6155211368c86510c1c445e62c9e29db440400aa0b1fb94b58650a09040c857ce3aba0deaf551b6
7
- data.tar.gz: be606e2835481fc87331df89b2b4b62a84f6e27dae7b57d6af4c76b4c4024b5d9bd8c319fed8dee96008bc623f1dd27395feb36958e55105d1d31165453234c5
6
+ metadata.gz: bfe33d146ecbc7062c6b75125e60e122d6bf4d718a253a4bc801b761be8c87e2909923df9da1e3278be7e1ea396575a980bba5f741b6304218bcacab809b0197
7
+ data.tar.gz: 77533393a26a524be382a3aa619026e1c6bd2c968ed75a98d8fc5792600675d857bd83759c934b6c6bd8fdb6c0b83a9b144a1fdd0decd43bf309a4645276579b
data/README.md CHANGED
@@ -19,7 +19,60 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- `tree_structure_digest example.yml`
22
+ Given a sample YAML document from Wikipedia:
23
+
24
+ ---
25
+ receipt: Oz-Ware Purchase Invoice
26
+ date: 2012-08-06
27
+ customer:
28
+ given: Dorothy
29
+ family: Gale
30
+
31
+ items:
32
+ - part_no: A4786
33
+ descrip: Water Bucket (Filled)
34
+ price: 1.47
35
+ quantity: 4
36
+
37
+ - part_no: E1628
38
+ descrip: High Heeled "Ruby" Slippers
39
+ size: 8
40
+ price: 100.27
41
+ quantity: 1
42
+
43
+ bill-to: &id001
44
+ street: |
45
+ 123 Tornado Alley
46
+ Suite 16
47
+ city: East Centerville
48
+ state: KS
49
+
50
+ ship-to: *id001
51
+
52
+ specialDelivery: >
53
+ Follow the Yellow Brick
54
+ Road to the Emerald City.
55
+ Pay no attention to the
56
+ man behind the curtain.
57
+
58
+ `tree_structure_digest example.yml` will produce:
59
+
60
+ .receipt
61
+ .date
62
+ .customer.given
63
+ .customer.family
64
+ .items[].part_no
65
+ .items[].descrip
66
+ .items[].price
67
+ .items[].quantity
68
+ .items[].size
69
+ .bill-to.street
70
+ .bill-to.city
71
+ .bill-to.state
72
+ .ship-to.street
73
+ .ship-to.city
74
+ .ship-to.state
75
+ .specialDelivery
23
76
 
24
77
  ## Contributing
25
78
 
@@ -1,10 +1,29 @@
1
1
  #!/usr/bin/env ruby
2
- begin
3
- require 'tree_structure_digest'
4
- rescue LoadError
5
- require 'rubygems'
6
- require 'tree_structure_digest'
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '../lib/')
3
+ require 'tree_structure_digest'
4
+ require 'tempfile'
5
+
6
+ require 'optparse'
7
+ ARGV << "--help" if ARGV.empty? && STDIN.tty?
8
+ tree = false
9
+
10
+ opt = OptionParser.new do |opt|
11
+ opt.banner = "Usage: tree_structure_digest [options] File1[, File2, ...]"
12
+ opt.on('-t', '--tree', 'replace repeated suffixes with indents') { tree = true }
7
13
  end
8
14
 
9
- $h = TreeStructureDigest::Digest.new.injest_yml_files(ARGV)
10
- puts $h.shorthand
15
+ opt.parse!
16
+
17
+ files = ARGV
18
+ file = Tempfile.new('tree_structure_digest_in')
19
+ if !STDIN.tty?
20
+ file.write STDIN.read
21
+ files += [file.path]
22
+ file.rewind
23
+ end
24
+
25
+ $h = TreeStructureDigest::Digest.new(tree: tree).injest_yml_files(files)
26
+ print $h.shorthand
27
+
28
+ file.close!
29
+
@@ -1,5 +1 @@
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
1
+ Dir[File.dirname(__FILE__) + "/**/*.rb"].each(&method(:require))
@@ -2,6 +2,10 @@ require 'yaml'
2
2
 
3
3
  module TreeStructureDigest
4
4
  class Digest
5
+ def initialize(opts={})
6
+ @tree = opts[:tree] || false
7
+ end
8
+
5
9
  def injest_yml_files(file_paths)
6
10
  file_paths.each do |p|
7
11
  y = YAML.load_file(p)
@@ -33,7 +37,38 @@ module TreeStructureDigest
33
37
  end
34
38
 
35
39
  def shorthand
36
- @abstract_paths.map(&:serialize).uniq
40
+ if @tree
41
+ root = {}
42
+ @abstract_paths.each do |apath|
43
+ append_to_tree(root, apath.parts)
44
+ end
45
+ sio = StringIO.new
46
+ pretty_print(sio, root)
47
+ sio.rewind
48
+ sio.read.chomp
49
+ else
50
+ @abstract_paths.map(&:serialize).uniq.sort.join("\n")
51
+ end
52
+ end
53
+
54
+ def pretty_print(io, tree, level=0)
55
+ tree.keys.sort.each do |k|
56
+ v = tree[k]
57
+ io << ' '*level + k
58
+ if v.keys.empty?
59
+ io << "\n"
60
+ elsif v.keys.size == 1
61
+ pretty_print(io, v, level)
62
+ else
63
+ io << "\n"
64
+ pretty_print(io, v, level+1)
65
+ end
66
+ end
67
+ end
68
+
69
+ def append_to_tree(tree, parts)
70
+ return if parts.empty?
71
+ append_to_tree(tree[parts.first.serialize] || (tree[parts.first.serialize] = {}), parts.drop(1))
37
72
  end
38
73
 
39
74
  private
@@ -1,3 +1,3 @@
1
1
  module TreeStructureDigest
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/sandbox/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ gem 'pry'
2
+ gem 'tree_structure_digest', path: '../'
@@ -0,0 +1,21 @@
1
+ {
2
+ "firstName": "John",
3
+ "lastName": "Smith",
4
+ "age": 25,
5
+ "address": {
6
+ "streetAddress": "21 2nd Street",
7
+ "city": "New York",
8
+ "state": "NY",
9
+ "postalCode": 10021
10
+ },
11
+ "phoneNumbers": [
12
+ {
13
+ "type": "home",
14
+ "number": "212 555-1234"
15
+ },
16
+ {
17
+ "type": "fax",
18
+ "number": "646 555-4567"
19
+ }
20
+ ]
21
+ }
@@ -0,0 +1,44 @@
1
+ ---
2
+ receipt: Oz-Ware Purchase Invoice
3
+ date: 2012-08-06
4
+ customer:
5
+ given: Dorothy
6
+ family: Gale
7
+
8
+ problem:
9
+ return: true
10
+
11
+ recursive:
12
+ problem:
13
+ que: 'what'
14
+ ans: 'who'
15
+ problem2:
16
+ que: 'a'
17
+ ans: 'b'
18
+
19
+ items:
20
+ - part_no: A4786
21
+ descrip: Water Bucket (Filled)
22
+ price: 1.47
23
+ quantity: 4
24
+
25
+ - part_no: E1628
26
+ descrip: High Heeled "Ruby" Slippers
27
+ size: 8
28
+ price: 100.27
29
+ quantity: 1
30
+
31
+ bill-to: &id001
32
+ street: |
33
+ 123 Tornado Alley
34
+ Suite 16
35
+ city: East Centerville
36
+ state: KS
37
+
38
+ ship-to: *id001
39
+
40
+ specialDelivery: >
41
+ Follow the Yellow Brick
42
+ Road to the Emerald City.
43
+ Pay no attention to the
44
+ man behind the curtain.
@@ -0,0 +1,68 @@
1
+ lib_path = File.join(File.dirname(__FILE__), '../lib/')
2
+ $LOAD_PATH << lib_path
3
+ require 'tree_structure_digest'
4
+
5
+ describe "tree_structure_digest" do
6
+
7
+ def digest_fixture(file, opts={})
8
+ TreeStructureDigest::Digest.new(opts).injest_yml_files(
9
+ [File.expand_path("./fixtures/#{file}", File.dirname(__FILE__))]
10
+ ).shorthand
11
+ end
12
+
13
+ it "works with yml" do
14
+ digest_fixture("sample.yml").should == <<HEREDOC.chomp
15
+ .bill-to.city
16
+ .bill-to.state
17
+ .bill-to.street
18
+ .customer.family
19
+ .customer.given
20
+ .date
21
+ .items[].descrip
22
+ .items[].part_no
23
+ .items[].price
24
+ .items[].quantity
25
+ .items[].size
26
+ .problem.return
27
+ .receipt
28
+ .recursive.problem.ans
29
+ .recursive.problem.que
30
+ .recursive.problem2.ans
31
+ .recursive.problem2.que
32
+ .ship-to.city
33
+ .ship-to.state
34
+ .ship-to.street
35
+ .specialDelivery
36
+ HEREDOC
37
+ end
38
+
39
+ it "works with JSON" do
40
+ digest_fixture("sample.json").should == <<HEREDOC.chomp
41
+ .address.city
42
+ .address.postalCode
43
+ .address.state
44
+ .address.streetAddress
45
+ .age
46
+ .firstName
47
+ .lastName
48
+ .phoneNumbers[].number
49
+ .phoneNumbers[].type
50
+ HEREDOC
51
+ end
52
+
53
+ it "works with JSON in tree format" do
54
+ digest_fixture("sample.json", tree: true).should == <<HEREDOC.chomp
55
+ .address
56
+ .city
57
+ .postalCode
58
+ .state
59
+ .streetAddress
60
+ .age
61
+ .firstName
62
+ .lastName
63
+ .phoneNumbers[]
64
+ .number
65
+ .type
66
+ HEREDOC
67
+ end
68
+ end
data/specs.sh ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+
3
+ diff spec/output_fixtures/sample_output.yml <(bin/tree_structure_digest spec/fixtures/sample.yml)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tree_structure_digest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.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-01-08 00:00:00.000000000 Z
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -61,6 +61,11 @@ files:
61
61
  - lib/tree_structure_digest/path.rb
62
62
  - lib/tree_structure_digest/value.rb
63
63
  - lib/tree_structure_digest/version.rb
64
+ - sandbox/Gemfile
65
+ - spec/fixtures/sample.json
66
+ - spec/fixtures/sample.yml
67
+ - spec/integration_spec.rb
68
+ - specs.sh
64
69
  - tree_structure_digest.gemspec
65
70
  homepage: ''
66
71
  licenses:
@@ -82,9 +87,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
87
  version: '0'
83
88
  requirements: []
84
89
  rubyforge_project:
85
- rubygems_version: 2.1.5
90
+ rubygems_version: 2.2.1
86
91
  signing_key:
87
92
  specification_version: 4
88
93
  summary: run the binary on YAML files to check it out
89
- test_files: []
94
+ test_files:
95
+ - spec/fixtures/sample.json
96
+ - spec/fixtures/sample.yml
97
+ - spec/integration_spec.rb
90
98
  has_rdoc: