xmlbuilder 0.2.6

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/xmlbuilder.rb +78 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 42a7c1b629722216b6d80b67c770a9ddc6b28cd9
4
+ data.tar.gz: 395ad471f2713ed4b11b2c07e1d3860ab8adbdf4
5
+ SHA512:
6
+ metadata.gz: 462b61b94852841639c8892176a1b4ca8cef1f4c8385bfbf27bb3a21a8bbf7fa3b82f265a174b4c6b86946e09d388172ced39509565358f342513b10c2b7fd11
7
+ data.tar.gz: dccf8a82a9405e3d72a87809244868ed6d1886ead40e9e38402f3e717ca999c2c94f7d22dfdd9ad6907e866f6be4c77bc7dccdca3d225dfb6e7aceabc88819ed
@@ -0,0 +1,78 @@
1
+ # XMLBuilder is a class that allows you to easily create XML.
2
+ # Here's an example:
3
+ # xml = XMLBuilder.new
4
+ # xml.document :type => 'xml', :use => 'example' do |document|
5
+ # document.description { |desc| desc.add "This is an example of using XMLBuilder.\n" }
6
+ # document.nextmeeting :date => Time.now+100000 do |meeting|
7
+ # meeting.agenda { |agenda| agenda.add "Nothing of importance will be decided.\n" }
8
+ # meeting.clearance true, :level => :classified # Passing true in as the first parameter will cause it to be a tag with no closing tag.
9
+ # end
10
+ # xml.add "I hope that this has been a good example."
11
+ # end
12
+ # p xml.str
13
+ # # <document type="xml" use="example">
14
+ # # <description>
15
+ # # This is an example of using XMLBuilder.
16
+ # # </description>
17
+ # # <nextmeeting date="2017-02-10 21:56:56 -0800">
18
+ # # <agenda>
19
+ # # Nothing of importance will be decided.
20
+ # # </agenda>
21
+ # # <clearance level="classified" />
22
+ # # </nextmeeting>
23
+ # # I hope that this has been a good example.
24
+ # # </document>
25
+ class XMLBuilder
26
+ attr_reader :str
27
+ # XMLBuilder#initialize simply sets the stored string to "".
28
+ def initialize
29
+ @str = ""
30
+ end
31
+ # XMLBuilder#clear does the same thing as #initialize (by delegating to it).
32
+ def clear
33
+ initialize # That's essentially what it does.
34
+ end
35
+ # XMLBuilder#add adds a string (with no processing) to the object's string.
36
+ def add(str)
37
+ @str << str
38
+ end
39
+ # XMLBuilder#method_missing is the brains of the operation. It takes the name of the tag to add,
40
+ # an optional boolean parameter which signifies whether to make it a single tag or not,
41
+ # any options to put in the tag, and a block to evaluate between the opening and closing tags.
42
+ # There is an alias, XMLBuilder#add_element, which is used for already defined methods such as XMLBuilder#send and
43
+ # XMLBuilder#method_missing.
44
+ def method_missing(name, *args, &block)
45
+ if args.length == 2
46
+ one_tag, hash = *args
47
+ elsif args.length == 1
48
+ if args[0].is_a? Hash
49
+ one_tag, hash = *[false, args[0]]
50
+ else
51
+ one_tag, hash = *[args[0], {}]
52
+ end
53
+ else
54
+ one_tag, hash = false, {}
55
+ end
56
+ @str << "<#{name}"
57
+ if one_tag
58
+ hash.each do |k, v|
59
+ @str << " #{k}=\"#{v}\""
60
+ end
61
+ @str << " />\n"
62
+ else
63
+ hash.each do |k, v|
64
+ @str << " #{k}=\"#{v}\""
65
+ end
66
+ @str << ">\n"
67
+ if block
68
+ block.call(self)
69
+ end
70
+ @str << "</#{name}>\n"
71
+ end
72
+ return @str
73
+ end
74
+ alias :add_element :method_missing
75
+ alias :to_s :str
76
+ alias :to_str :str
77
+ alias :inspect :str
78
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xmlbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.6
5
+ platform: ruby
6
+ authors:
7
+ - Coderz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem allows you to write XML in a very Ruby-ish way. It supports
14
+ single tags, nesting tags, and adding strings.
15
+ email: asdfsocool@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/xmlbuilder.rb
21
+ homepage: https://github.com/Coderzthereal/xmlbuilder/
22
+ licenses:
23
+ - CC-BY-NC-SA-4.0
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.6.7
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: A simple gem that allows you to write XML in a very Ruby-ish way
45
+ test_files: []