¶ ↑
Ruby As Markup LanguageRequire the RAML library.
require 'raml'
Given a RAML document:
website "http://rubygems.org"
We can load the text via the #load method. (Note above document text has been placed in the @text variable.)
data = RAML.load(@text) data.assert == {:website=>"http://rubygems.org"}
One of the nicer features of RAML derives from Ruby’s block notation, allowing for nested entries.
Given a RAML document:
resources do home "http://rubyworks.github.com/raml" docs "http://rubyworks.github.com/raml/docs/api" wiki "http://wiki.rubyworks.github.com/raml" end
We get a two layer hash.
data = RAML.load(@text) data[:resources][:home].assert == "http://rubyworks.github.com/raml" data[:resources][:docs].assert == "http://rubyworks.github.com/raml/docs/api" data[:resources][:wiki].assert == "http://wiki.rubyworks.github.com/raml"
RAML is also considers the content of a block. If it is a scalar entry, such as a String, then that will be assigned to the key.
Given a RAML document:
description do "This is a description.\n" \ "It has multiple lines." end
Loading this document,
data = RAML.load(@text) data[:description].assert.start_with?("This is") data[:description].assert.end_with?("does too.")
RAML has some options that makes it more flexible than many other data lanaguages. For instance, it can allow for multi-key entries.
Given a RAML document:
source "http://rubygems.org" gem "facets", "~> 2.8" gem "ansi", "~> 1.1"
We simply need to inform the loader to allow identical keys.
data = RAML.load(@text, :multikey=>true) data.assert == { :source=>"http://rubygems.org", :gem=>[["facets", "~> 2.8"],["ansi", "~> 1.1"]] }
If we did not turn on the multi-key option, then the last ‘gem` entry would have simply overwritten the former.
data = RAML.load(@text) data.assert == { :source=>"http://rubygems.org", :gem=>["ansi", "~> 1.1"] }
Hi Tom
first whack at #initialize
Cheers Robert