Class: Gyoku::Prettifier

Inherits:
Object
  • Object
show all
Defined in:
lib/gyoku/prettifier.rb

Constant Summary collapse

DEFAULT_INDENT =
2
DEFAULT_COMPACT =
true

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Prettifier

Returns a new instance of Prettifier.



14
15
16
17
# File 'lib/gyoku/prettifier.rb', line 14

def initialize(options = {})
  @indent = options[:indent] || DEFAULT_INDENT
  @compact = options[:compact].nil? ? DEFAULT_COMPACT : options[:compact]
end

Instance Attribute Details

#compactObject

Returns the value of attribute compact.



8
9
10
# File 'lib/gyoku/prettifier.rb', line 8

def compact
  @compact
end

#indentObject

Returns the value of attribute indent.



8
9
10
# File 'lib/gyoku/prettifier.rb', line 8

def indent
  @indent
end

Class Method Details

.prettify(xml, options = {}) ⇒ Object



10
11
12
# File 'lib/gyoku/prettifier.rb', line 10

def self.prettify(xml, options = {})
  new(options).prettify(xml)
end

Instance Method Details

#prettify(xml) ⇒ Object

Adds intendations and newlines to xml to make it more readable



20
21
22
23
24
25
26
27
# File 'lib/gyoku/prettifier.rb', line 20

def prettify(xml)
  result = ''
  formatter = REXML::Formatters::Pretty.new indent
  formatter.compact = compact
  doc = REXML::Document.new xml
  formatter.write doc, result
  result
end