-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
227 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'rexml/document' | ||
|
||
module Gyoku | ||
class Prettifier | ||
DEFAULT_INDENT = 2 | ||
DEFAULT_COMPACT = true | ||
|
||
attr_accessor :indent, :compact | ||
|
||
def self.prettify(xml, options = {}) | ||
new(options).prettify(xml) | ||
end | ||
|
||
def initialize(options = {}) | ||
@indent = options[:indent] || DEFAULT_INDENT | ||
@compact = options[:compact].nil? ? DEFAULT_COMPACT : options[:compact] | ||
end | ||
|
||
# Adds intendations and newlines to +xml+ to make it more readable | ||
def prettify(xml) | ||
result = '' | ||
formatter = REXML::Formatters::Pretty.new indent | ||
formatter.compact = compact | ||
doc = REXML::Document.new xml | ||
formatter.write doc, result | ||
result | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require "spec_helper" | ||
|
||
describe Gyoku::Prettifier do | ||
describe "#prettify" do | ||
context "when xml is valid" do | ||
let!(:xml) { Gyoku::Hash.build_xml(test: { pretty: "xml" }) } | ||
|
||
it "returns prettified xml" do | ||
expect(subject.prettify(xml)).to eql("<test>\n <pretty>xml</pretty>\n</test>") | ||
end | ||
|
||
context "when indent option is specified" do | ||
it "returns prettified xml with indent" do | ||
options = { indent: 3 } | ||
subject = Gyoku::Prettifier.new(options) | ||
expect(subject.prettify(xml)).to eql("<test>\n <pretty>xml</pretty>\n</test>") | ||
end | ||
end | ||
|
||
context "when compact option is specified" do | ||
it "returns prettified xml with indent" do | ||
options = { compact: false } | ||
subject = Gyoku::Prettifier.new(options) | ||
expect(subject.prettify(xml)).to eql("<test>\n <pretty>\n xml\n </pretty>\n</test>") | ||
end | ||
end | ||
end | ||
|
||
context "when xml is not valid" do | ||
let!(:xml) do | ||
Gyoku::Array.build_xml(["one", "two"], "test") | ||
end | ||
|
||
it "raises an error" do | ||
expect{ subject.prettify(xml) }.to raise_error REXML::ParseException | ||
end | ||
end | ||
end | ||
end |