bakkdoor-srxml 0.0.2 → 0.0.3

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.
Files changed (5) hide show
  1. data/README +1 -0
  2. data/Rakefile +8 -0
  3. data/lib/srxml.rb +71 -17
  4. data/test/test_srxml.rb +17 -0
  5. metadata +2 -2
data/README CHANGED
@@ -1,5 +1,6 @@
1
1
  =========================
2
2
  SRXML - Simple Ruby XML
3
+ Created by Christopher Bertels (bakkdoor@flasht.de / http://github.com/bakkdoor/)
3
4
  =========================
4
5
 
5
6
  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.
data/Rakefile CHANGED
@@ -12,4 +12,12 @@ rt = Rake::TestTask.new("test_units") do |t|
12
12
  t.libs << "test/unit"
13
13
  t.pattern = "test/test_*.rb"
14
14
  t.verbose = true
15
+ end
16
+
17
+ desc "create rdoc in /doc"
18
+ rd = Rake::RDocTask.new("doc") do |rd|
19
+ rd.main = "README"
20
+ rd.rdoc_files.include("README", "lib/*.rb")
21
+ rd.options << "--all"
22
+ rd.rdoc_dir = "rdoc"
15
23
  end
data/lib/srxml.rb CHANGED
@@ -1,26 +1,72 @@
1
1
  module SRXML
2
-
2
+
3
+ =begin rdoc
4
+ BlankSlate class. Has nearly no methods, except for anything among the regex
5
+ (__*, instance_eval, inspect).
6
+ Is used by the XML class, since we want to use method_missing to create the xml-output.
7
+ Having fewer methods predefined is good, since we want as few nameclashes as possible.
8
+ =end
3
9
  class BlankSlate
4
10
  instance_methods.each { |m| undef_method m unless m =~ /^(__|instance_eval|inspect)/ }
5
11
  end
6
12
 
7
-
13
+
14
+ =begin rdoc
15
+ Main class. Use it to create the xml-output.
16
+ For example:
17
+ xml = SRXML::XML.new :xml => false
18
+ xml.people{
19
+ xml.person{
20
+ xml.name "Todd"
21
+ }
22
+ xml.person{
23
+ xml.name "Mary"
24
+ }
25
+ }
26
+
27
+ puts xml.to_s :formatted
28
+
29
+ Will give you:
30
+ <people>
31
+ <person>
32
+ <name>Todd</name>
33
+ </person>
34
+ <person>
35
+ <name>Mary</name>
36
+ </person
37
+ </people>
38
+ =end
8
39
  class XML < BlankSlate
9
40
 
10
- attr_reader :xml_tag, :sep
41
+ attr_accessor :singles # defines tags, which don't have a closing-tag (e.g. <br/>)
42
+ attr_reader :xml_tag # indicates, if xml-tag is used (should be false for html-mode, for example)
43
+ attr_reader :sep # holds a seperator-string, indicating, where a newline (for formatted-output) should be placed
44
+ # if not specified, its default value is '<>'
11
45
 
46
+ =begin rdoc
47
+ Constructor. Takes a options-hash.
48
+ Valid options are:
49
+ :xml => true/false # indicates if xml-tag should be used at top of xml-output
50
+ :sep => "<>" # custom specified seperator-string; '<>' if not set
51
+ :singles => [] # custom specified list of single-tags (no closing tag), e.g. '<br/>' in html
52
+ =end
12
53
  def initialize(options = {})
13
- @xml_tag = options[:xml_tag].nil? ? true : options[:xml_tag]
54
+ @xml_tag = options[:xml].nil? ? true : options[:xml]
14
55
  @sep = options[:sep] || "<>"
15
56
 
57
+ @singles = options[:singles] || [] # if not specified, there are no single-tags
58
+
16
59
  if @xml_tag
17
- @output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>#{@sep}"
60
+ @output = ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>#{@sep}"]
18
61
  else
19
- @output = ""
62
+ @output = []
20
63
  end
21
64
  end
22
65
 
23
-
66
+
67
+ =begin rdoc
68
+ This method is used to create the xml-ouput based on the called methods on the XML-object.
69
+ =end
24
70
  def method_missing(method_name, *args)
25
71
  @output << "<#{method_name}"
26
72
 
@@ -31,6 +77,7 @@ module SRXML
31
77
  if a.class != Hash
32
78
  value = a
33
79
  else
80
+ # shove all key-value-pairs in as attributes to the xml-tag
34
81
  a.keys.sort_by{|k| k.to_s}.each do |key|
35
82
  attributes << " #{key}=\"#{a[key]}\""
36
83
  end
@@ -40,28 +87,35 @@ module SRXML
40
87
  attributes.each do |a|
41
88
  @output << a
42
89
  end
43
-
44
- @output << ">#{value}"
90
+ @output << ">"
91
+ @output << "#{value}" unless @singles.include?(method_name)
45
92
 
46
93
  if block_given?
47
94
  @output << @sep
48
- yield
95
+ @output << yield
49
96
  end
50
-
51
- @output << "</#{method_name}>"
52
- @output << @sep
53
97
 
54
- return self
98
+ unless @singles.include?(method_name)
99
+ @output << "</#{method_name}>"
100
+ @output << @sep
101
+ end
55
102
  end
56
103
 
104
+
105
+ =begin rdoc
106
+ Returns the xml-output string created by using the xml-object.
107
+ Default-output is non_formatted. Optional output-styles are:
108
+ :formatted # will put newlines in the correct places
109
+ :keep_sep # will leave seperator-string in place (probably non-valid xml then - mainly for debug purposes)
110
+ =end
57
111
  def to_s(option = :non_formatted)
58
112
  if option == :formatted
59
113
  # format here with newline etc.
60
- @output.gsub(@sep, "\n")
114
+ @output.select{|x| x.class == String}.join("").gsub(@sep, "\n")
61
115
  elsif option == :keep_sep
62
- @output
116
+ @output.join("")
63
117
  else
64
- @output.gsub(@sep, "")
118
+ @output.select{|x| x.class == String}.join("").gsub(@sep, "")
65
119
  end
66
120
  end
67
121
 
data/test/test_srxml.rb CHANGED
@@ -47,4 +47,21 @@ class TestSRXML < Test::Unit::TestCase
47
47
  assert_equal(fixture(:friends), xml.to_s)
48
48
  assert_equal(fixture(:friends_formatted), xml.to_s(:formatted))
49
49
  end
50
+
51
+ def test_inner_string_output_and_no_xml_tag
52
+ xml = SRXML::XML.new :xml => false
53
+
54
+ xml.html{
55
+ xml.head{
56
+ xml.title "Title"
57
+ }
58
+ xml.body{
59
+ xml.div{
60
+ "OkiDoki"
61
+ }
62
+ }
63
+ }
64
+
65
+ assert_equal(fixture("inner_string.html"), xml.to_s)
66
+ end
50
67
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bakkdoor-srxml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Bertels
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-01 00:00:00 -07:00
12
+ date: 2008-09-02 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15