mig-xml_magick 0.1

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.textile ADDED
@@ -0,0 +1,40 @@
1
+ h1. XML::Magick
2
+
3
+ h2. About
4
+
5
+ XML::Magick allows you to parse an xml document or string, and access fields/attributes using ruby objects.
6
+
7
+ h2. Install
8
+
9
+ <pre>
10
+ $ gem install <notextile>mig-xml_magick</notextile>
11
+ </pre>
12
+
13
+ h2. Usage
14
+
15
+ <pre>
16
+ <code>
17
+ require 'rubygems'
18
+ require 'xml_magick'
19
+
20
+ xml = %Q{
21
+ <project title="XML Magick">
22
+ <x:content xmlns:x='http://www.w3.org/1999/XSL/Transform'>I am content</x:content>
23
+ <type>Library</type>
24
+ <contact name="Mig">mig@example.com</contact>
25
+ <contact name="Rig">rig@example.com</contact>
26
+ <contact name="Pig">pig@example.com</contact>
27
+ <description>Describe the situation</description>
28
+ </project>
29
+ }
30
+
31
+ @magick = XML::Magick.new(xml)
32
+
33
+ @magick.description #=> Describe the situation
34
+ @magick[:title] #=> XML Magick
35
+ @magick.contact[1].xml_content #=> rig@example.com
36
+ @magick.contact[2][:name] #=> Pig
37
+ @magick.namespace = "x:http://www.w3.org/1999/XSL/Transform"
38
+ @magick.content #=> I am content
39
+ </code>
40
+ </pre>
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "rake"
2
+ require "rake/testtask"
3
+
4
+ desc "Default: run unit tests"
5
+ task :default => :test
6
+
7
+ desc "Test xml_magick"
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << "lib"
10
+ t.pattern = "test/**/*_test.rb"
11
+ t.verbose = true
12
+ end
data/lib/core_ext.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Object
2
+ def number?
3
+ self.is_a?(Fixnum) or self.is_a?(Bignum) or self.is_a?(Integer) or self.is_a?(Range)
4
+ end
5
+ end
data/lib/xml_magick.rb ADDED
@@ -0,0 +1,45 @@
1
+ require "rubygems"
2
+ require "xml/libxml"
3
+ require "core_ext"
4
+
5
+ module XML
6
+ class Node
7
+ alias :xml_content :content
8
+ end
9
+
10
+ class Magick
11
+ attr :element
12
+ attr_accessor :namespace
13
+
14
+ def initialize(xml)
15
+ parser = XML::Parser.new
16
+ parser.string = xml
17
+ @xml = parser.parse
18
+ @element = @xml.root
19
+ end
20
+
21
+ def [](index)
22
+ case index
23
+ when Symbol then @element[index]
24
+ when number? then new(@element[index].to_s) end
25
+ end
26
+
27
+ def namespace_name
28
+ namespace.split(":").first
29
+ end
30
+
31
+ def method_missing(method_sym)
32
+ method = method_sym.to_s
33
+
34
+ if namespace
35
+ then elements = @element.find("#{namespace_name}:#{method}", namespace)
36
+ else elements = @element.find(method)
37
+ end
38
+
39
+ if elements.length > 1
40
+ then elements
41
+ else elements.length == 0 ? nil : elements.first.xml_content
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,12 @@
1
+ require "test/unit"
2
+ require File.dirname(__FILE__) + "/../lib/core_ext"
3
+
4
+ class ObjectTest < Test::Unit::TestCase
5
+ def test_should_return_true_if_object_is_a_type_of_number
6
+ assert 0.number?
7
+ assert 1.to_i.number?
8
+ assert 0x20000000000.number?
9
+ assert -1.number?
10
+ assert (0..10).number?
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ require "test/unit"
2
+ require File.dirname(__FILE__) + "/../lib/xml_magick"
3
+
4
+ class XML::MagickTest < Test::Unit::TestCase
5
+ def setup
6
+ xml = %Q{
7
+ <project title="XML Magick">
8
+ <x:content xmlns:x='http://www.w3.org/1999/XSL/Transform'>I am content</x:content>
9
+ <type>Library</type>
10
+ <contact name="Mig">mig@example.com</contact>
11
+ <contact name="Rig">rig@example.com</contact>
12
+ <contact name="Pig">pig@example.com</contact>
13
+ <description>Describe the situation</description>
14
+ </project>
15
+ }
16
+ @xml = XML::Magick.new(xml)
17
+ end
18
+
19
+ def test_should_parse_elements_without_namespaces
20
+ assert_equal "Describe the situation", @xml.description
21
+ end
22
+
23
+ def test_should_parse_namedspaced_elements
24
+ assert_nil @xml.content
25
+ @xml.namespace = "x:http://www.w3.org/1999/XSL/Transform"
26
+ assert_equal "I am content", @xml.content
27
+ end
28
+
29
+ def test_should_parse_attributes
30
+ assert_equal "XML Magick", @xml[:title]
31
+ end
32
+
33
+ def test_should_parse_enumerable_elements
34
+ assert_equal "Rig", @xml.contact[1][:name]
35
+ assert_equal "rig@example.com", @xml.contact[1].xml_content
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mig-xml_magick
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Mig Swasey
8
+ - Commonthread
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2008-10-22 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: A simple xml parsing library
18
+ email: mig@pggbee.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - Rakefile
27
+ - README.textile
28
+ - lib/core_ext.rb
29
+ - lib/xml_magick.rb
30
+ - test/core_ext_test.rb
31
+ - test/xml_magick_test.rb
32
+ has_rdoc: false
33
+ homepage: http://blog.pggbee.com
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.2.0
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: A library that will turn xml into easily parseable ruby objects
58
+ test_files:
59
+ - test/core_ext_test.rb
60
+ - test/xml_magick_test.rb