coub / raml_ruby

Raml Ruby
MIT License
93 stars 27 forks source link

Support !include #5

Open choffmeister opened 9 years ago

choffmeister commented 9 years ago

Seems like this is not active anymore, but in case, you start again anytime and want to support imports: The following does the trick:

Psych.add_domain_type 'include', 'include' do |_, value|
  case value
    when /^https?:\/\//
      # TODO implement remote loading of included files
      ''
    else
      case value
        when /\.raml$/
          read_yaml(value)
        when /\.ya?ml$/
          read_yaml(value)
        else
          File.read(value)
      end
  end
end

def read_yaml(path)
  # change working directory so that !includes work properly
  pwd_old = Dir.pwd
  Dir.chdir(File.dirname(path))
  raw = File.read(File.basename(path))
  node = YAML.load(raw)
  Dir.chdir(pwd_old)
  node
end
zlx commented 8 years ago

:+1: