bakkdoor-srxml 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,52 @@
1
+ =========================
2
+ SRXML - Simple Ruby XML
3
+ =========================
4
+
5
+ SRXML is a very (super, if you will!) lightweight xml generator for Ruby. No big magic here, it simply uses method_missing to create the tags. There is some plan to make it more useful. For Example being able to parse existing files etc. but there are probably better libraries to do that.
6
+ If you simply want to create an xml file without the need for extra fancy formatting, SRXML could just be the deal for you!
7
+
8
+
9
+ =========================
10
+ LICENSE:
11
+
12
+ SRXML is licensed under the terms of the GNU GPL v2.
13
+ Play with it as much as you like, fork it and put the changes here back on github or so :)
14
+
15
+ =========================
16
+
17
+
18
+ ==================================
19
+ Short Example (from examble.rb):
20
+ ==================================
21
+
22
+ require "sr_xml"
23
+
24
+ xml = SRXML::XML.new
25
+
26
+ xml.projects{
27
+ xml.project(:name => "project #1", :description => "some text", :id => 1){
28
+ xml.something_else "yo!"
29
+ }
30
+ }
31
+
32
+ puts xml.to_s
33
+
34
+
35
+ Will output the following:
36
+
37
+ <?xml version="1.0" encoding="UTF-8"?><projects><project name="project #1" description="some text" id="1"><something_else>yo!</something_else></project></projects>
38
+
39
+ You can also use some simple formatting:
40
+
41
+ puts xml.to_s(:formatted)
42
+
43
+ Which will then give you something like this:
44
+
45
+ <?xml version="1.0" encoding="UTF-8"?><projects>
46
+ <project name="project #1" description="some text" id="1">
47
+ <something_else>yo!</something_else>
48
+ </project>
49
+ </projects>
50
+
51
+ For more examples take a look in the example-files.
52
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "rake"
2
+ require "rake/testtask"
3
+ require "rake/rdoctask"
4
+ require "rake/clean"
5
+ require "rake/gempackagetask"
6
+
7
+ task :default => [ :test ]
8
+ task :test => [ :test_units ]
9
+
10
+ desc "run unit tests in test/unit"
11
+ rt = Rake::TestTask.new("test_units") do |t|
12
+ t.libs << "test/unit"
13
+ t.pattern = "test/test_*.rb"
14
+ t.verbose = true
15
+ end
@@ -0,0 +1,21 @@
1
+ require "srxml"
2
+
3
+ @projects = []
4
+
5
+ @projects << {:name => "Project 1", :id => 1, :description => "This is a first test project!"}
6
+ @projects << {:name => "Project 2", :id => 2, :description => "This is a second test project!"}
7
+ @projects << {:name => "Project 3", :id => 3, :description => "This is a third test project!"}
8
+
9
+ xml = SRXML::XML.new
10
+
11
+ xml.projects{
12
+ @projects.each do |project|
13
+ xml.project{
14
+ xml.id project[:id]
15
+ xml.name project[:name]
16
+ xml.description project[:description]
17
+ }
18
+ end
19
+ }
20
+
21
+ puts xml.to_s
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><projects><project><id>1</id><name>Project 1</name><description>This is a first test project!</description></project><project><id>2</id><name>Project 2</name><description>This is a second test project!</description></project><project><id>3</id><name>Project 3</name><description>This is a third test project!</description></project></projects>
@@ -0,0 +1,15 @@
1
+ require "srxml"
2
+
3
+ @projects = []
4
+
5
+ @projects << {:name => "Project 1", :id => 1, :description => "This is a first test project!"}
6
+ @projects << {:name => "Project 2", :id => 2, :description => "This is a second test project!"}
7
+ @projects << {:name => "Project 3", :id => 3, :description => "This is a third test project!"}
8
+
9
+ xml = SRXML::XML.new
10
+
11
+ puts xml.projects{
12
+ @projects.each do |project|
13
+ xml.project "some value in the project-node", :id => project[:id], :name => project[:name], :description => project[:description]
14
+ end
15
+ }.to_s
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><projects><project name="Project 1" description="This is a first test project!" id="1">some value in the project-node</project><project name="Project 2" description="This is a second test project!" id="2">some value in the project-node</project><project name="Project 3" description="This is a third test project!" id="3">some value in the project-node</project></projects>
data/lib/srxml.rb ADDED
@@ -0,0 +1,70 @@
1
+ module SRXML
2
+
3
+ class BlankSlate
4
+ instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_eval|inspect)/ }
5
+ end
6
+
7
+
8
+ class XML < BlankSlate
9
+
10
+ attr_reader :xml_tag, :sep
11
+
12
+ def initialize(options = {})
13
+ @xml_tag = options[:xml_tag].nil? ? true : options[:xml_tag]
14
+ @sep = options[:sep] || "<>"
15
+
16
+ if @xml_tag
17
+ @output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>#{@sep}"
18
+ else
19
+ @output = ""
20
+ end
21
+ end
22
+
23
+
24
+ def method_missing(method_name, *args)
25
+ @output << "<#{method_name}"
26
+
27
+ attributes = []
28
+ value = ""
29
+
30
+ args.each do |a|
31
+ if a.class != Hash
32
+ value = a
33
+ else
34
+ a.keys.sort_by{|k| k.to_s}.each do |key|
35
+ attributes << " #{key}=\"#{a[key]}\""
36
+ end
37
+ end
38
+ end
39
+
40
+ attributes.each do |a|
41
+ @output << a
42
+ end
43
+
44
+ @output << ">#{value}"
45
+
46
+ if block_given?
47
+ @output << @sep
48
+ yield
49
+ end
50
+
51
+ @output << "</#{method_name}>"
52
+ @output << @sep
53
+
54
+ return self
55
+ end
56
+
57
+ def to_s(option = :non_formatted)
58
+ if option == :formatted
59
+ # format here with newline etc.
60
+ @output.gsub(@sep, "\n")
61
+ elsif option == :keep_sep
62
+ @output
63
+ else
64
+ @output.gsub(@sep, "")
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ end
@@ -0,0 +1,23 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <friends>
3
+ <friend>
4
+ <name>Thomas</name>
5
+ <age>20</age>
6
+ </friend>
7
+ <friend>
8
+ <name>Jessie</name>
9
+ <age>21</age>
10
+ </friend>
11
+ <friend>
12
+ <name>Joe</name>
13
+ <age>22</age>
14
+ </friend>
15
+ <friend>
16
+ <name>Sandra</name>
17
+ <age>19</age>
18
+ </friend>
19
+ <friend>
20
+ <name>Chris</name>
21
+ <age>25</age>
22
+ </friend>
23
+ </friends>
@@ -0,0 +1 @@
1
+ <?xml version="1.0" encoding="UTF-8"?><projects><project description="This is a first test project!" id="1" name="Project 1">some value in the project-node</project><project description="This is a second test project!" id="2" name="Project 2">some value in the project-node</project><project description="This is a third test project!" id="3" name="Project 3">some value in the project-node</project></projects>
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <projects>
3
+ <project description="This is a first test project!" id="1" name="Project 1">some value in the project-node</project>
4
+ <project description="This is a second test project!" id="2" name="Project 2">some value in the project-node</project>
5
+ <project description="This is a third test project!" id="3" name="Project 3">some value in the project-node</project>
6
+ </projects>
@@ -0,0 +1,5 @@
1
+ module TestHelper
2
+ def fixture(name)
3
+ File.open("test/fixtures/#{name}.xml", "r").read
4
+ end
5
+ end
@@ -0,0 +1,50 @@
1
+ require "test/unit"
2
+ require "srxml"
3
+
4
+ class TestSRXML < Test::Unit::TestCase
5
+
6
+ include TestHelper
7
+
8
+ def setup
9
+ @projects = []
10
+ @projects << {:name => "Project 1", :id => 1, :description => "This is a first test project!"}
11
+ @projects << {:name => "Project 2", :id => 2, :description => "This is a second test project!"}
12
+ @projects << {:name => "Project 3", :id => 3, :description => "This is a third test project!"}
13
+
14
+ @friends = []
15
+ @friends << {:name => "Thomas", :age => 20}
16
+ @friends << {:name => "Jessie", :age => 21}
17
+ @friends << {:name => "Joe", :age => 22}
18
+ @friends << {:name => "Sandra", :age => 19}
19
+ @friends << {:name => "Chris", :age => 25}
20
+ end
21
+
22
+ def test_xml_output_with_attributes
23
+ xml = SRXML::XML.new
24
+
25
+ xml.projects{
26
+ @projects.each do |project|
27
+ xml.project "some value in the project-node", :id => project[:id], :name => project[:name], :description => project[:description]
28
+ end
29
+ }
30
+
31
+ assert_equal(fixture(:projects), xml.to_s(:non_formatted))
32
+ assert_equal(fixture(:projects_formatted), xml.to_s(:formatted))
33
+ end
34
+
35
+ def test_xml_output_with_inner_tags
36
+ xml = SRXML::XML.new
37
+
38
+ xml.friends{
39
+ @friends.each do |friend|
40
+ xml.friend{
41
+ xml.name friend[:name]
42
+ xml.age friend[:age]
43
+ }
44
+ end
45
+ }
46
+
47
+ assert_equal(fixture(:friends), xml.to_s)
48
+ assert_equal(fixture(:friends_formatted), xml.to_s(:formatted))
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bakkdoor-srxml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Bertels
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-01 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: SRXML is a very (super, if you will!) lightweight xml generator for Ruby. No big magic here, it simply uses method_missing to create the tags. There is some plan to make it more useful. For Example being able to parse existing files etc. but there are probably better libraries to do that.If you simply want to create an xml file without the need for extra fancy formatting, SRXML could just be the deal for you!
17
+ email: bakkdoor@flasht.de
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - README
26
+ - Rakefile
27
+ - lib/srxml.rb
28
+ - examples/example.rb
29
+ - examples/example.xml
30
+ - examples/example2.rb
31
+ - examples/example2.xml
32
+ - test/fixtures/friends_formatted.xml
33
+ - test/fixtures/projects.xml
34
+ - test/fixtures/projects_formatted.xml
35
+ - test/test_helper.rb
36
+ - test/test_srxml.rb
37
+ has_rdoc: false
38
+ homepage: http://github.com/bakkdoor/srxml
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --main
42
+ - README
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements:
58
+ - none
59
+ rubyforge_project:
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Simple Ruby XML Generator
64
+ test_files:
65
+ - test/test_helper.rb
66
+ - test/test_srxml.rb