Class: YAXML::Yaxml

Inherits:
REXML::Document
  • Object
show all
Defined in:
lib/yaxml.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, options = {}) ⇒ Yaxml

Constructor

source

It can be a String or a file name.

options

It is possible to pass the next options:

  • :root_name It is the name of the root tag in the YAXML document. Defaults to DEFAULT_ROOT_NAME

  • :from_yaxml If true, the source will be considered as YAXML input. If false, the source will be considered as YAML input. Defaults to :false

Example of use:

xml = Yaxml.new 'file.yml'
xml.write( $stdout, 2)
xml.to_json
xml.to_yaml


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/yaxml.rb', line 65

def initialize( source, options={} )
  super()
  
  if options[:from_yaxml]
    content = extract_content source
    doc = REXML::Document.new content
    self.add_element( doc.elements[1] ) if doc.elements[1]
    
  else #default is from_yaml
    options[:root_name] ||= DEFAULT_ROOT_NAME
    doc = YAXML::yaml2yaxml( source, options )
    
    self.add_element( doc.elements[1] ) if doc.elements[1]
  end

end

Instance Method Details

#to_jsonObject

The output is a JSON representation of YAXML object.



88
89
90
# File 'lib/yaxml.rb', line 88

def to_json
  YAXML::yaxml2json self
end

#to_yamlObject

The output is a YAML representation of YAXML object.



83
84
85
# File 'lib/yaxml.rb', line 83

def to_yaml
  YAXML::yaxml2yaml self
end

#to_yaxmlObject

The output is a YAXML representation of YAXML object.



93
94
95
# File 'lib/yaxml.rb', line 93

def to_yaxml
  self.to_s
end