yaml_ref_resolver 0.4.2 → 0.4.3

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: b15959cdb30b6bd1175233f3c88618cd02654ace
4
- data.tar.gz: 11dab92dca480e42528fdcaa75018099dd70fa32
3
+ metadata.gz: 11b5ee504e3aa03e93bc04e602b2ef9f8dfafb25
4
+ data.tar.gz: 504177fda0c4e4b5d0c224780112bf5720a85c22
5
5
  SHA512:
6
- metadata.gz: 7dbe62b28a440a9be12fe06546a4f34f7d7df1b091d22297ea4c9427febbba7dbc7d1b4e721bb5d4ef330e1cfba430a9b5638198cd948bcf36fa62081b3f4ae5
7
- data.tar.gz: 4e196c70f1a6711faad5bcc72b4539fb18c3c67797a8084a8ad1307e51c15b89adbf65a0eb6766fc0443a2387f5d2f5be7c248ee2d958884bfa3ebd687ebbdd3
6
+ metadata.gz: 78c18f5058db6dfdd47ff70ed19c994d9a280188cb012f9a26bf18b15faeb00c000419d2ce5f57486e07db5939653cc39f6259abf5b7d9493de4ef711c8b7d18
7
+ data.tar.gz: ec9c2cb5b4c1a1d560ddaf6464168928fa5524095bb6f53582cb0e331e3b9456c3280bf402296750b975e6fe8bc9a5124b51546db53b871fb5e412c2e1a094c0
data/README.md CHANGED
@@ -35,7 +35,7 @@ This gem resolves `$ref` references.
35
35
  ```ruby
36
36
  resolver = YamlRefResolver.new
37
37
 
38
- hash = resolver.resolve('index.yaml')
38
+ hash = resolver.resolve!('index.yaml')
39
39
  #=> {'author' => {'name' => "john", 'age' => 28}}
40
40
  ```
41
41
 
@@ -23,30 +23,33 @@ class YamlRefResolver
23
23
 
24
24
  if @watch
25
25
  FileWatcher.new(resolver.files).watch do |filename|
26
- resolver.reload(File.expand_path(filename))
27
- resolve_and_dump
26
+ resolve_and_dump(File.expand_path filename)
28
27
  end
29
28
  end
30
29
  end
31
30
 
32
31
  private
33
32
 
34
- def resolve_and_dump
33
+ def resolve_and_dump(changed_file = nil)
34
+ resolver.reload!(changed_file)
35
35
 
36
36
  if @output
37
37
  File.open(@output, "w") do |f|
38
- f.puts yaml_or_json
38
+ f.puts resolved_yaml_or_json!
39
39
  end
40
+ log "bundled successfully."
40
41
  else
41
- $stdout.write yaml_or_json
42
+ $stdout.write resolved_yaml_or_json!
42
43
  end
44
+ rescue => e
45
+ log e.message
43
46
  end
44
47
 
45
- def yaml_or_json
48
+ def resolved_yaml_or_json!
46
49
  if @json
47
- JSON.pretty_generate resolver.resolve(@input)
50
+ JSON.pretty_generate resolver.resolve!(@input)
48
51
  else
49
- resolver.resolve(@input).to_yaml
52
+ resolver.resolve!(@input).to_yaml
50
53
  end
51
54
  end
52
55
 
@@ -92,5 +95,9 @@ class YamlRefResolver
92
95
  exit 1
93
96
  end
94
97
  end
98
+
99
+ def log(message)
100
+ puts "[#{Time.now}] #{message}"
101
+ end
95
102
  end
96
103
  end
@@ -0,0 +1,21 @@
1
+ class YamlRefResolver
2
+ class YamlNotFoundException < StandardError
3
+ attr_reader :path
4
+
5
+ def initialize(path:)
6
+ @path = path
7
+
8
+ super("yaml file not found: #{path}")
9
+ end
10
+ end
11
+
12
+ class YamlSyntaxErrorException < StandardError
13
+ attr_reader :original_exception
14
+
15
+ def initialize(exception:)
16
+ @original_exception = exception
17
+
18
+ super("syntax error #{@original_exception.message}")
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  class YamlRefResolver
2
- VERSION = "0.4.2"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -1,13 +1,24 @@
1
- require "yaml_ref_resolver/ref"
2
1
  require "yaml"
3
2
 
4
3
  class YamlRefResolver::Yaml
5
4
  attr_reader :content
6
5
 
7
- def initialize(path:, key: '$ref')
6
+ def self.load!(path:, key: '$ref')
7
+ if File.exists?(path)
8
+ begin
9
+ self.new(path, YAML.load_file(path), key)
10
+ rescue Psych::SyntaxError => e
11
+ raise YamlRefResolver::YamlSyntaxErrorException.new(exception: e)
12
+ end
13
+ else
14
+ raise YamlRefResolver::YamlNotFoundException.new(path: path)
15
+ end
16
+ end
17
+
18
+ def initialize(path, content, key)
8
19
  @filepath = path
20
+ @content = content
9
21
  @key = key
10
- @content = YAML.load_file(path)
11
22
  end
12
23
 
13
24
  def refs
@@ -1,5 +1,8 @@
1
1
  require "yaml_ref_resolver/version"
2
+ require "yaml_ref_resolver/exceptions"
3
+ require "yaml_ref_resolver/ref"
2
4
  require "yaml_ref_resolver/yaml"
5
+ require "yaml"
3
6
 
4
7
  class YamlRefResolver
5
8
  def initialize(opts = {})
@@ -7,16 +10,24 @@ class YamlRefResolver
7
10
  @map = {}
8
11
  end
9
12
 
10
- def resolve(path)
13
+ def resolve!(path)
11
14
  entry_point = File.expand_path(path)
12
- preload(entry_point)
15
+ preload!(entry_point)
13
16
 
14
17
  resolve_refs(@map[entry_point].content, entry_point)
15
18
  end
16
19
 
17
- def reload(path)
18
- @map.delete(path)
19
- preload(path)
20
+ def reload!(path)
21
+ return unless path
22
+
23
+ backup = @map.delete(path)
24
+
25
+ begin
26
+ preload!(path)
27
+ rescue => e
28
+ @map[path] = backup
29
+ raise e
30
+ end
20
31
  end
21
32
 
22
33
  def files
@@ -25,12 +36,12 @@ class YamlRefResolver
25
36
 
26
37
  private
27
38
 
28
- def preload(abs_path)
39
+ def preload!(abs_path)
29
40
  return if @map.has_key?(abs_path)
30
41
 
31
- @map[abs_path] = Yaml.new(path: abs_path, key: @key)
42
+ @map[abs_path] = Yaml.load!(path: abs_path, key: @key)
32
43
  @map[abs_path].refs.each do |ref|
33
- preload(ref.abs_path)
44
+ preload!(ref.abs_path)
34
45
  end
35
46
  end
36
47
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_ref_resolver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe-noh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-03-22 00:00:00.000000000 Z
11
+ date: 2017-04-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: filewatcher
@@ -99,6 +99,7 @@ files:
99
99
  - exe/yaml_ref_resolver
100
100
  - lib/yaml_ref_resolver.rb
101
101
  - lib/yaml_ref_resolver/cli.rb
102
+ - lib/yaml_ref_resolver/exceptions.rb
102
103
  - lib/yaml_ref_resolver/ref.rb
103
104
  - lib/yaml_ref_resolver/version.rb
104
105
  - lib/yaml_ref_resolver/yaml.rb
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  version: '0'
124
125
  requirements: []
125
126
  rubyforge_project:
126
- rubygems_version: 2.5.1
127
+ rubygems_version: 2.5.2
127
128
  signing_key:
128
129
  specification_version: 4
129
130
  summary: Resolves referencing to other YAML files.