psych-inherit-file 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25d5eb1755ecc9f5d1b41cc5ff86586ad0817bd5
4
+ data.tar.gz: 4d35bf4df0a7e02f512d9720dc74ef6069ff6667
5
+ SHA512:
6
+ metadata.gz: b06a918686d7c2985448d34fdc7b6e38cf7cd8f176dee755f8c9fb2b5956690e8f37da3dfc7359247596d3b59e44eceac8bf316929bdd5e2e1916b34acb87f19
7
+ data.tar.gz: eec6250d475d7aab374dce572c41cdd39afe7e78b0bb462834303041269ae428784146958f63a0febaf9359861e4ea759d797e2ad33e137af59aaf21a87d46ef
@@ -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 psych-require-file.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Florian Gilcher
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.
@@ -0,0 +1,59 @@
1
+ # Psych::Inherit::File
2
+
3
+ A psych handler and parser that allows inherit other files within YAML files.
4
+
5
+ It is an extraction from [elasticsearch-rake-tasks](http://github.com/asquera/elasticsearch-rake-tasks).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'psych-require-file'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install psych-require-file
20
+
21
+ ## Usage
22
+
23
+ Just use the `Parser` class instead of your YAML parser:
24
+
25
+ ```ruby
26
+ parser = Psych::Inherit::File::Parser.new
27
+ parser.parse_file('myfile.yaml')
28
+ ```
29
+
30
+ This parser allows you to include the following tag in your YAML:
31
+
32
+ ```yaml
33
+ # bar.yml
34
+ ---
35
+ name: &string_analyzed
36
+ type: string
37
+ ```
38
+
39
+ ```
40
+ # foo.yml
41
+ properties:
42
+ inherit: !file "bar.yml"
43
+ surname: *string_analyzed
44
+ ```
45
+
46
+ This allows you to share definitions between files. The "inherit" node will be replaced by inherited file.
47
+
48
+ ## Contributors
49
+
50
+ * [Sebastian Ziebell](http://github.com/zebel) (original implementation)
51
+ * [Florian Gilcher](http://github.com/skade) (extraction)
52
+
53
+ ## Contributing
54
+
55
+ 1. Fork it
56
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
57
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
58
+ 4. Push to the branch (`git push origin my-new-feature`)
59
+ 5. Create new Pull Request
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
@@ -0,0 +1,11 @@
1
+ require "psych/inherit/file/version"
2
+ require "psych/inherit/file/handler"
3
+ require "psych/inherit/file/parser"
4
+
5
+ module Psych
6
+ module Require
7
+ module File
8
+ # Your code goes here...
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,51 @@
1
+ require "psych"
2
+
3
+ module Psych
4
+ module Inherit
5
+ module File
6
+ class Handler < Psych::TreeBuilder
7
+ TAG_NAME = '!file'
8
+ NODE_NAME = 'inherit'
9
+
10
+ attr_reader :reader
11
+
12
+ def initialize(reader)
13
+ @reader = reader
14
+ super()
15
+ end
16
+
17
+ def document
18
+ root.children.first
19
+ end
20
+
21
+ def last_node
22
+ @last.children.last
23
+ end
24
+
25
+ def include_node?(node)
26
+ node.respond_to?(:value) && node.value == NODE_NAME
27
+ end
28
+
29
+ def scalar(value, anchor, tag, plain, quoted, style)
30
+ if tag == TAG_NAME && include_node?(last_node)
31
+ pop_previous_node
32
+
33
+ nodes = @stack.last.children
34
+ content = reader.parse_file(value).children.first
35
+ nodes << content
36
+ push content
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def pop_previous_node
45
+ @stack.pop
46
+ @stack.last.children.pop
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ require 'ostruct'
2
+
3
+ module Psych
4
+ module Inherit
5
+ module File
6
+ class Parser
7
+ def load_file(file)
8
+ parse_file(file).to_ruby
9
+ end
10
+
11
+ def parse_file(file)
12
+ parse(::File.read(file))
13
+ rescue StandardError => e
14
+ # STDOUT.puts "Error while reading file #{file}: #{e}"
15
+ raise e
16
+ end
17
+
18
+ def parse(content)
19
+ parser = Psych::Parser.new(File::Handler.new(self))
20
+ parser.parse(content).handler.document
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Psych
2
+ module Inherit
3
+ module File
4
+ VERSION = "1.0.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Psych
2
+ module Require
3
+ module File
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'psych/inherit/file/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "psych-inherit-file"
8
+ spec.version = Psych::Inherit::File::VERSION
9
+ spec.authors = ["Sebastian Ziebell", "Florian Gilcher"]
10
+ spec.email = ["sebastian.ziebell@asquera.de", "florian.gilcher@asquera.de"]
11
+ spec.description = %q{A handler for Psych that allows you to inhert other YAML files,
12
+ for example to share certain repetitive definitions.}
13
+ spec.summary = %q{A handler for Psych allowing you to inherit other files.}
14
+ spec.homepage = "http://github.com/asquera/psych-inherit-file"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ properties:
3
+ inherit: !file "bar.yml"
4
+ surname: *string_analyzed
@@ -0,0 +1,3 @@
1
+ ---
2
+ name: &string_analyzed
3
+ type: string
File without changes
@@ -0,0 +1,6 @@
1
+ ---
2
+ properties:
3
+ inherit: !file "foo.yml"
4
+ name:
5
+ type: string
6
+ analyzer: foo_analyzer
@@ -0,0 +1,4 @@
1
+ ---
2
+ title:
3
+ type: string
4
+ analyzer: foo_analyzer
@@ -0,0 +1,8 @@
1
+ ---
2
+ index:
3
+ analysis:
4
+ analyzer:
5
+ foo_analyzer:
6
+ filter:
7
+ - standard
8
+ - lowercase
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'psych/inherit/file'
5
+
6
+ RSpec.configure do |config|
7
+ config.color_enabled = true
8
+ config.formatter = 'documentation'
9
+ end
10
+
11
+ def examples_root
12
+ templates = "examples"
13
+ @root ||= File.join(File.dirname(__FILE__), templates)
14
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ describe Psych::Inherit::File::Parser do
4
+ let(:parser){ described_class.new }
5
+
6
+ def mapping_folder(template)
7
+ "#{examples_root}/#{template}/mappings"
8
+ end
9
+
10
+ describe "#parse_yaml_content" do
11
+ subject{ parser.parse(yaml) }
12
+ context "without an inherit value" do
13
+ let(:yaml){ "---\nfoo:\n bar: !file \"test.yml\"" }
14
+
15
+ it "does not load external file" do
16
+ described_class.should_not_receive(:parse_file)
17
+ subject.to_ruby
18
+ end
19
+
20
+ it "parses correctly" do
21
+ subject.to_ruby.should == {
22
+ 'foo' => { 'bar' => 'test.yml' }
23
+ }
24
+ end
25
+ end
26
+
27
+ context "without a !file tag" do
28
+ let(:yaml){ "---\nfoo:\n inherit: \"bar.yml\"" }
29
+
30
+ it "does not load external file" do
31
+ described_class.should_not_receive(:parse_file)
32
+ subject.to_ruby
33
+ end
34
+
35
+ it "parses correctly" do
36
+ subject.to_ruby.should == {
37
+ 'foo' => { 'inherit' => 'bar.yml' }
38
+ }
39
+ end
40
+ end
41
+
42
+ context "with distant properties" do
43
+ let(:yaml){ "---\nfoo:\n inherit:\n foo: !file \"bar.yml\"" }
44
+
45
+ it "does not load external file" do
46
+ described_class.should_not_receive(:parse_file)
47
+ subject.to_ruby
48
+ end
49
+
50
+ it "parses correctly" do
51
+ subject.to_ruby.should == {
52
+ 'foo' => { 'inherit' => { 'foo' => 'bar.yml' } }
53
+ }
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "#load_file" do
59
+ subject do
60
+ Dir.chdir(dir) do
61
+ parser.load_file(yaml)
62
+ end
63
+ end
64
+
65
+ context "missing file" do
66
+ let(:dir){ mapping_folder("simple") }
67
+ let(:yaml){ "#{dir}/unknow.yml" }
68
+
69
+ it "raises an error" do
70
+ expect{ subject }.to raise_error
71
+ end
72
+ end
73
+
74
+ context "simple template" do
75
+ let(:dir){ mapping_folder("simple") }
76
+ let(:yaml){ "#{dir}/foo.yml" }
77
+
78
+ it "does not raise error" do
79
+ expect{ subject }.not_to raise_error
80
+ end
81
+
82
+ it "matches hash" do
83
+ subject.should == {
84
+ 'title' => {
85
+ 'type' => 'string',
86
+ 'analyzer' => 'foo_analyzer'
87
+ }
88
+ }
89
+ end
90
+ end
91
+
92
+ context "template with file include" do
93
+ let(:dir){ mapping_folder("simple") }
94
+ let(:yaml){ "#{dir}/bar.yml" }
95
+
96
+ it "does not raise error" do
97
+ expect{ subject }.not_to raise_error
98
+ end
99
+
100
+ it "matches hash" do
101
+ subject.should == {
102
+ 'properties' => {
103
+ 'title' => {
104
+ 'type' => 'string',
105
+ 'analyzer' => 'foo_analyzer'
106
+ },
107
+ 'name' => {
108
+ 'type' => 'string',
109
+ 'analyzer' => 'foo_analyzer'
110
+ }
111
+ }
112
+ }
113
+ end
114
+ end
115
+
116
+ context "template with external alias" do
117
+ let(:dir){ mapping_folder("include") }
118
+ let(:yaml){ "#{dir}/alias.yml" }
119
+
120
+ it "does not raise error" do
121
+ expect{ subject }.not_to raise_error
122
+ end
123
+
124
+ it "matches hash" do
125
+ subject.should == {
126
+ 'properties' => {
127
+ 'name' => { 'type' => 'string' },
128
+ 'surname' => { 'type' => 'string' }
129
+ }
130
+ }
131
+ end
132
+ end
133
+ end
134
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: psych-inherit-file
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Ziebell
8
+ - Florian Gilcher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: |-
57
+ A handler for Psych that allows you to inhert other YAML files,
58
+ for example to share certain repetitive definitions.
59
+ email:
60
+ - sebastian.ziebell@asquera.de
61
+ - florian.gilcher@asquera.de
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - lib/psych/inherit/file.rb
72
+ - lib/psych/inherit/file/handler.rb
73
+ - lib/psych/inherit/file/parser.rb
74
+ - lib/psych/inherit/file/version.rb
75
+ - lib/psych/require/file/version.rb
76
+ - psych-require-file.gemspec
77
+ - spec/examples/include/mappings/alias.yml
78
+ - spec/examples/include/mappings/bar.yml
79
+ - spec/examples/include/settings.yaml
80
+ - spec/examples/simple/mappings/bar.yml
81
+ - spec/examples/simple/mappings/foo.yml
82
+ - spec/examples/simple/settings.yaml
83
+ - spec/examples/simple/template_pattern
84
+ - spec/spec_helper.rb
85
+ - spec/yaml_parser_spec.rb
86
+ homepage: http://github.com/asquera/psych-inherit-file
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.0.14
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: A handler for Psych allowing you to inherit other files.
110
+ test_files:
111
+ - spec/examples/include/mappings/alias.yml
112
+ - spec/examples/include/mappings/bar.yml
113
+ - spec/examples/include/settings.yaml
114
+ - spec/examples/simple/mappings/bar.yml
115
+ - spec/examples/simple/mappings/foo.yml
116
+ - spec/examples/simple/settings.yaml
117
+ - spec/examples/simple/template_pattern
118
+ - spec/spec_helper.rb
119
+ - spec/yaml_parser_spec.rb
120
+ has_rdoc: