From 363823822c73decbfc00251a7cc8fa748069b83c Mon Sep 17 00:00:00 2001 From: Pol Miro Date: Mon, 30 Mar 2015 10:59:16 -0700 Subject: [PATCH] Add singleton file_path method to contents resolved by include --- lib/raml/node/template.rb | 2 +- lib/raml/parser.rb | 2 ++ test/raml/parser_spec.rb | 8 +++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/raml/node/template.rb b/lib/raml/node/template.rb index 909d033..fa0f563 100644 --- a/lib/raml/node/template.rb +++ b/lib/raml/node/template.rb @@ -16,7 +16,7 @@ def interpolate(params) def clone_data # ugly but effective - Marshal.load Marshal.dump @value + Marshal.load Marshal.dump @value.dup end def interpolate_params(value, params) diff --git a/lib/raml/parser.rb b/lib/raml/parser.rb index f816138..5d307b0 100644 --- a/lib/raml/parser.rb +++ b/lib/raml/parser.rb @@ -35,7 +35,9 @@ def expand_includes_transform(cwd) if val.is_a? Raml::Parser::Include child_wd = expand_includes_working_dir cwd, val.path + path = val.path val = val.content cwd + val.define_singleton_method(:file_path) { child_wd + "/" + path.split("/").last } end expand_includes val, child_wd diff --git a/test/raml/parser_spec.rb b/test/raml/parser_spec.rb index 9d978af..6fc2615 100644 --- a/test/raml/parser_spec.rb +++ b/test/raml/parser_spec.rb @@ -36,7 +36,13 @@ root.schemas['Test' ].value.should eq 'test_schema' root.schemas['File' ].value.should eq 'file_schema' end - + + it "inserts singleton method to read the file_path" do + file = File.new 'fixtures/include_1.raml' + root = Raml::Parser.parse file.read, 'fixtures' + root.schemas['FileUpdate'].value.file_path.should eq 'fixtures/schemas/filesystem/fileupdate.json' + end + context 'when the included file is not redable' do it do expect { Raml::Parser.parse('- !include does_not_exit') }.to raise_error Raml::CantIncludeFile