jnunemaker-happymapper 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +8 -0
- data/License.txt +20 -0
- data/Manifest.txt +39 -0
- data/PostInstall.txt +0 -0
- data/README.txt +26 -0
- data/Rakefile +4 -0
- data/TODO.txt +2 -0
- data/config/hoe.rb +73 -0
- data/config/requirements.rb +15 -0
- data/examples/amazon.rb +29 -0
- data/examples/post.rb +19 -0
- data/examples/twitter.rb +37 -0
- data/happymapper.gemspec +35 -0
- data/lib/happymapper/attribute.rb +3 -0
- data/lib/happymapper/element.rb +3 -0
- data/lib/happymapper/item.rb +86 -0
- data/lib/happymapper/version.rb +9 -0
- data/lib/happymapper.rb +123 -0
- data/lib/libxml_ext/libxml_helper.rb +93 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/script/txt2html +82 -0
- data/setup.rb +1585 -0
- data/spec/fixtures/pita.xml +132 -0
- data/spec/fixtures/posts.xml +23 -0
- data/spec/fixtures/statuses.xml +422 -0
- data/spec/happymapper_attribute_spec.rb +17 -0
- data/spec/happymapper_element_spec.rb +17 -0
- data/spec/happymapper_item_spec.rb +73 -0
- data/spec/happymapper_spec.rb +216 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/tasks/deployment.rake +43 -0
- data/tasks/environment.rake +7 -0
- data/tasks/rspec.rake +21 -0
- data/tasks/website.rake +17 -0
- data/website/css/common.css +47 -0
- data/website/index.html +98 -0
- metadata +106 -0
data/History.txt
ADDED
data/License.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 John Nunemaker
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
History.txt
|
2
|
+
License.txt
|
3
|
+
Manifest.txt
|
4
|
+
PostInstall.txt
|
5
|
+
README.txt
|
6
|
+
Rakefile
|
7
|
+
TODO.txt
|
8
|
+
config/hoe.rb
|
9
|
+
config/requirements.rb
|
10
|
+
examples/amazon.rb
|
11
|
+
examples/post.rb
|
12
|
+
examples/twitter.rb
|
13
|
+
happymapper.gemspec
|
14
|
+
lib/happymapper.rb
|
15
|
+
lib/happymapper/attribute.rb
|
16
|
+
lib/happymapper/element.rb
|
17
|
+
lib/happymapper/item.rb
|
18
|
+
lib/happymapper/version.rb
|
19
|
+
lib/libxml_ext/libxml_helper.rb
|
20
|
+
script/console
|
21
|
+
script/destroy
|
22
|
+
script/generate
|
23
|
+
script/txt2html
|
24
|
+
setup.rb
|
25
|
+
spec/fixtures/pita.xml
|
26
|
+
spec/fixtures/posts.xml
|
27
|
+
spec/fixtures/statuses.xml
|
28
|
+
spec/happymapper_attribute_spec.rb
|
29
|
+
spec/happymapper_element_spec.rb
|
30
|
+
spec/happymapper_item_spec.rb
|
31
|
+
spec/happymapper_spec.rb
|
32
|
+
spec/spec.opts
|
33
|
+
spec/spec_helper.rb
|
34
|
+
tasks/deployment.rake
|
35
|
+
tasks/environment.rake
|
36
|
+
tasks/rspec.rake
|
37
|
+
tasks/website.rake
|
38
|
+
website/css/common.css
|
39
|
+
website/index.html
|
data/PostInstall.txt
ADDED
File without changes
|
data/README.txt
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
= happymapper
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
Object to xml mapping library. I have included examples to help get you going. The specs
|
6
|
+
should also point you in the right direction.
|
7
|
+
|
8
|
+
== FEATURES:
|
9
|
+
|
10
|
+
* Easy to define xml attributes and elements for an object
|
11
|
+
* Fast because it uses libxml-ruby under the hood
|
12
|
+
* Automatic conversion of xml to defined objects
|
13
|
+
|
14
|
+
== SYNOPSIS:
|
15
|
+
|
16
|
+
See examples directory in the gem to get a feel for how it works.
|
17
|
+
|
18
|
+
== INSTALL:
|
19
|
+
|
20
|
+
* add github to your sources if you haven't gem sources -a http://gems.github.com
|
21
|
+
* sudo gem install jnunemaker-happymapper
|
22
|
+
* sudo gem install happymapper (when rubyforge approves and i release there)
|
23
|
+
|
24
|
+
== TICKETS:
|
25
|
+
|
26
|
+
http://jnunemaker.lighthouseapp.com/projects/20014-happy-mapper/overview
|
data/Rakefile
ADDED
data/TODO.txt
ADDED
data/config/hoe.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'happymapper/version'
|
2
|
+
|
3
|
+
AUTHOR = 'John Nunemaker' # can also be an array of Authors
|
4
|
+
EMAIL = "nunemaker@gmail.com"
|
5
|
+
DESCRIPTION = "object to xml mapping library"
|
6
|
+
GEM_NAME = 'happymapper' # what ppl will type to install your gem
|
7
|
+
RUBYFORGE_PROJECT = 'happymapper' # The unix name for your project
|
8
|
+
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
|
9
|
+
DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
|
10
|
+
EXTRA_DEPENDENCIES = [
|
11
|
+
['libxml-ruby', '>= 0.8.3']
|
12
|
+
]
|
13
|
+
|
14
|
+
@config_file = "~/.rubyforge/user-config.yml"
|
15
|
+
@config = nil
|
16
|
+
RUBYFORGE_USERNAME = "unknown"
|
17
|
+
def rubyforge_username
|
18
|
+
unless @config
|
19
|
+
begin
|
20
|
+
@config = YAML.load(File.read(File.expand_path(@config_file)))
|
21
|
+
rescue
|
22
|
+
puts <<-EOS
|
23
|
+
ERROR: No rubyforge config file found: #{@config_file}
|
24
|
+
Run 'rubyforge setup' to prepare your env for access to Rubyforge
|
25
|
+
- See http://newgem.rubyforge.org/rubyforge.html for more details
|
26
|
+
EOS
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
RUBYFORGE_USERNAME.replace @config["username"]
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
REV = nil
|
35
|
+
# UNCOMMENT IF REQUIRED:
|
36
|
+
# REV = YAML.load(`svn info`)['Revision']
|
37
|
+
VERS = HappyMapper::VERSION::STRING + (REV ? ".#{REV}" : "")
|
38
|
+
RDOC_OPTS = ['--quiet', '--title', 'happymapper documentation',
|
39
|
+
"--opname", "index.html",
|
40
|
+
"--line-numbers",
|
41
|
+
"--main", "README",
|
42
|
+
"--inline-source"]
|
43
|
+
|
44
|
+
class Hoe
|
45
|
+
def extra_deps
|
46
|
+
@extra_deps.reject! { |x| Array(x).first == 'hoe' }
|
47
|
+
@extra_deps
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Generate all the Rake tasks
|
52
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
53
|
+
$hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
54
|
+
p.developer(AUTHOR, EMAIL)
|
55
|
+
p.description = DESCRIPTION
|
56
|
+
p.summary = DESCRIPTION
|
57
|
+
p.url = HOMEPATH
|
58
|
+
p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
|
59
|
+
p.test_globs = ["test/**/test_*.rb"]
|
60
|
+
p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
|
61
|
+
|
62
|
+
# == Optional
|
63
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
64
|
+
#p.extra_deps = EXTRA_DEPENDENCIES
|
65
|
+
|
66
|
+
#p.spec_extras = {} # A hash of extra values to set in the gemspec.
|
67
|
+
end
|
68
|
+
|
69
|
+
CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
|
70
|
+
PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
|
71
|
+
$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
|
72
|
+
$hoe.rsync_args = '-av --delete --ignore-errors'
|
73
|
+
$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
include FileUtils
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
%w[rake hoe newgem rubigen].each do |req_gem|
|
6
|
+
begin
|
7
|
+
require req_gem
|
8
|
+
rescue LoadError
|
9
|
+
puts "This Rakefile requires the '#{req_gem}' RubyGem."
|
10
|
+
puts "Installation: gem install #{req_gem} -y"
|
11
|
+
exit
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
data/examples/amazon.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'happymapper')
|
3
|
+
|
4
|
+
file_contents = File.read(dir + '/../spec/fixtures/pita.xml')
|
5
|
+
|
6
|
+
module PITA
|
7
|
+
class Item
|
8
|
+
include HappyMapper
|
9
|
+
|
10
|
+
tag 'Item' # if you put class in module you need tag
|
11
|
+
element :asin, String, :tag => 'ASIN'
|
12
|
+
element :detail_page_url, String, :tag => 'DetailPageURL'
|
13
|
+
element :manufacturer, String, :tag => 'Manufacturer', :deep => true
|
14
|
+
end
|
15
|
+
|
16
|
+
class Items
|
17
|
+
include HappyMapper
|
18
|
+
|
19
|
+
tag 'Items' # if you put class in module you need tag
|
20
|
+
element :total_results, Integer, :tag => 'TotalResults'
|
21
|
+
element :total_pages, Integer, :tag => 'TotalPages'
|
22
|
+
has_many :items, Item
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
item = PITA::Items.parse(file_contents, :single => true, :use_default_namespace => true)
|
27
|
+
item.items.each do |i|
|
28
|
+
puts i.asin, i.detail_page_url, i.manufacturer, ''
|
29
|
+
end
|
data/examples/post.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'happymapper')
|
3
|
+
|
4
|
+
file_contents = File.read(dir + '/../spec/fixtures/posts.xml')
|
5
|
+
|
6
|
+
class Post
|
7
|
+
include HappyMapper
|
8
|
+
|
9
|
+
attribute :href, String
|
10
|
+
attribute :hash, String
|
11
|
+
attribute :description, String
|
12
|
+
attribute :tag, String
|
13
|
+
attribute :time, DateTime
|
14
|
+
attribute :others, Integer
|
15
|
+
attribute :extended, String
|
16
|
+
end
|
17
|
+
|
18
|
+
posts = Post.parse(file_contents)
|
19
|
+
posts.each { |post| puts post.description, post.href, post.extended, '' }
|
data/examples/twitter.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
require File.join(dir, 'happymapper')
|
3
|
+
|
4
|
+
file_contents = File.read(dir + '/../spec/fixtures/statuses.xml')
|
5
|
+
|
6
|
+
class User
|
7
|
+
include HappyMapper
|
8
|
+
|
9
|
+
element :id, Integer
|
10
|
+
element :name, String
|
11
|
+
element :screen_name, String
|
12
|
+
element :location, String
|
13
|
+
element :description, String
|
14
|
+
element :profile_image_url, String
|
15
|
+
element :url, String
|
16
|
+
element :protected, Boolean
|
17
|
+
element :followers_count, Integer
|
18
|
+
end
|
19
|
+
|
20
|
+
class Status
|
21
|
+
include HappyMapper
|
22
|
+
|
23
|
+
element :id, Integer
|
24
|
+
element :text, String
|
25
|
+
element :created_at, Time
|
26
|
+
element :source, String
|
27
|
+
element :truncated, Boolean
|
28
|
+
element :in_reply_to_status_id, Integer
|
29
|
+
element :in_reply_to_user_id, Integer
|
30
|
+
element :favorited, Boolean
|
31
|
+
has_one :user, User
|
32
|
+
end
|
33
|
+
|
34
|
+
statuses = Status.parse(file_contents)
|
35
|
+
statuses.each do |status|
|
36
|
+
puts status.user.name, status.user.screen_name, status.text, status.source, ''
|
37
|
+
end
|
data/happymapper.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{happymapper}
|
5
|
+
s.version = "0.1.2"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["John Nunemaker"]
|
9
|
+
s.date = %q{2008-12-12}
|
10
|
+
s.description = %q{object to xml mapping library}
|
11
|
+
s.email = ["nunemaker@gmail.com"]
|
12
|
+
s.extra_rdoc_files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "TODO.txt"]
|
13
|
+
s.files = ["History.txt", "License.txt", "Manifest.txt", "PostInstall.txt", "README.txt", "Rakefile", "TODO.txt", "config/hoe.rb", "config/requirements.rb", "examples/amazon.rb", "examples/post.rb", "examples/twitter.rb", "happymapper.gemspec", "lib/happymapper.rb", "lib/happymapper/attribute.rb", "lib/happymapper/element.rb", "lib/happymapper/item.rb", "lib/happymapper/version.rb", "lib/libxml_ext/libxml_helper.rb", "script/console", "script/destroy", "script/generate", "script/txt2html", "setup.rb", "spec/fixtures/pita.xml", "spec/fixtures/posts.xml", "spec/fixtures/statuses.xml", "spec/happymapper_attribute_spec.rb", "spec/happymapper_element_spec.rb", "spec/happymapper_item_spec.rb", "spec/happymapper_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/environment.rake", "tasks/rspec.rake", "tasks/website.rake", "website/css/common.css", "website/index.html"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://happymapper.rubyforge.org}
|
16
|
+
s.post_install_message = %q{}
|
17
|
+
s.rdoc_options = ["--main", "README.txt"]
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
s.rubyforge_project = %q{happymapper}
|
20
|
+
s.rubygems_version = %q{1.3.1}
|
21
|
+
s.summary = %q{object to xml mapping library}
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
|
29
|
+
else
|
30
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
31
|
+
end
|
32
|
+
else
|
33
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module HappyMapper
|
2
|
+
class Item
|
3
|
+
attr_accessor :type, :tag, :options
|
4
|
+
attr_reader :name
|
5
|
+
|
6
|
+
Types = [String, Float, Time, Date, DateTime, Integer, Boolean]
|
7
|
+
|
8
|
+
# options:
|
9
|
+
# :deep => Boolean False to only parse element's children, True to include
|
10
|
+
# grandchildren and all others down the chain (// in expath)
|
11
|
+
# :single => Boolean False if object should be collection, True for single object
|
12
|
+
def initialize(name, type, o={})
|
13
|
+
self.name, self.type, self.tag = name, type, o.delete(:tag) || name.to_s
|
14
|
+
self.options = {:single => false, :deep => false}.merge(o)
|
15
|
+
@xml_type = self.class.to_s.split('::').last.downcase
|
16
|
+
end
|
17
|
+
|
18
|
+
def name=(new_name)
|
19
|
+
@name = new_name.to_s
|
20
|
+
end
|
21
|
+
|
22
|
+
def from_xml_node(node, namespace=nil)
|
23
|
+
if primitive?
|
24
|
+
typecast(value_from_xml_node(node, namespace))
|
25
|
+
else
|
26
|
+
use_default_namespace = !namespace.nil?
|
27
|
+
type.parse(node, options.merge(:use_default_namespace => use_default_namespace))
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def primitive?
|
32
|
+
Types.include?(type)
|
33
|
+
end
|
34
|
+
|
35
|
+
def element?
|
36
|
+
@xml_type == 'element'
|
37
|
+
end
|
38
|
+
|
39
|
+
def attribute?
|
40
|
+
!element?
|
41
|
+
end
|
42
|
+
|
43
|
+
def typecast(value)
|
44
|
+
return value if value.kind_of?(type) || value.nil?
|
45
|
+
begin
|
46
|
+
if type == String then value.to_s
|
47
|
+
elsif type == Float then value.to_f
|
48
|
+
elsif type == Time then Time.parse(value.to_s)
|
49
|
+
elsif type == Date then Date.parse(value.to_s)
|
50
|
+
elsif type == DateTime then DateTime.parse(value.to_s)
|
51
|
+
elsif type == Boolean then ['true', 't', '1'].include?(value.to_s.downcase)
|
52
|
+
elsif type == Integer
|
53
|
+
# ganked from datamapper
|
54
|
+
value_to_i = value.to_i
|
55
|
+
if value_to_i == 0 && value != '0'
|
56
|
+
value_to_s = value.to_s
|
57
|
+
begin
|
58
|
+
Integer(value_to_s =~ /^(\d+)/ ? $1 : value_to_s)
|
59
|
+
rescue ArgumentError
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
else
|
63
|
+
value_to_i
|
64
|
+
end
|
65
|
+
else
|
66
|
+
value
|
67
|
+
end
|
68
|
+
rescue
|
69
|
+
value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
def value_from_xml_node(node, namespace=nil)
|
75
|
+
node.register_default_namespace(namespace.chop) if namespace
|
76
|
+
|
77
|
+
if element?
|
78
|
+
depth = options[:deep] ? './/' : ''
|
79
|
+
result = node.find_first("#{depth}#{namespace}#{tag}")
|
80
|
+
result ? result.content : nil
|
81
|
+
else
|
82
|
+
node[tag]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/happymapper.rb
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
dir = File.dirname(__FILE__)
|
2
|
+
$:.unshift(dir) unless $:.include?(dir) || $:.include?(File.expand_path(dir))
|
3
|
+
|
4
|
+
require 'date'
|
5
|
+
require 'time'
|
6
|
+
require 'rubygems'
|
7
|
+
|
8
|
+
gem 'libxml-ruby', '>= 0.8.3'
|
9
|
+
require 'xml'
|
10
|
+
require 'libxml_ext/libxml_helper'
|
11
|
+
|
12
|
+
|
13
|
+
class Boolean; end
|
14
|
+
|
15
|
+
module HappyMapper
|
16
|
+
|
17
|
+
def self.included(base)
|
18
|
+
base.instance_variable_set("@attributes", {})
|
19
|
+
base.instance_variable_set("@elements", {})
|
20
|
+
base.extend ClassMethods
|
21
|
+
end
|
22
|
+
|
23
|
+
module ClassMethods
|
24
|
+
def attribute(name, type, options={})
|
25
|
+
attribute = Attribute.new(name, type, options)
|
26
|
+
@attributes[to_s] ||= []
|
27
|
+
@attributes[to_s] << attribute
|
28
|
+
create_accessor(attribute.name)
|
29
|
+
end
|
30
|
+
|
31
|
+
def attributes
|
32
|
+
@attributes[to_s] || []
|
33
|
+
end
|
34
|
+
|
35
|
+
def element(name, type, options={})
|
36
|
+
element = Element.new(name, type, options)
|
37
|
+
@elements[to_s] ||= []
|
38
|
+
@elements[to_s] << element
|
39
|
+
create_accessor(element.name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def elements
|
43
|
+
@elements[to_s] || []
|
44
|
+
end
|
45
|
+
|
46
|
+
def has_one(name, type, options={})
|
47
|
+
element name, type, {:single => true}.merge(options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def has_many(name, type, options={})
|
51
|
+
element name, type, {:single => false}.merge(options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def tag(new_tag_name)
|
55
|
+
@tag_name = new_tag_name.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_tag_name
|
59
|
+
@tag_name ||= to_s.downcase
|
60
|
+
end
|
61
|
+
|
62
|
+
def parse(xml, o={})
|
63
|
+
options = {
|
64
|
+
:single => false,
|
65
|
+
:use_default_namespace => false,
|
66
|
+
}.merge(o)
|
67
|
+
|
68
|
+
namespace = "default_ns:" if options[:use_default_namespace]
|
69
|
+
doc = xml.is_a?(LibXML::XML::Node) ? xml : xml.to_libxml_doc
|
70
|
+
|
71
|
+
nodes = if namespace
|
72
|
+
node = doc.respond_to?(:root) ? doc.root : doc
|
73
|
+
node.register_default_namespace(namespace.chop)
|
74
|
+
node.find("#{namespace}#{get_tag_name}")
|
75
|
+
else
|
76
|
+
doc.find(get_tag_name)
|
77
|
+
end
|
78
|
+
|
79
|
+
collection = create_collection(nodes, namespace)
|
80
|
+
|
81
|
+
# per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354
|
82
|
+
nodes = nil
|
83
|
+
GC.start
|
84
|
+
|
85
|
+
options[:single] ? collection.first : collection
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def create_collection(nodes, namespace=nil)
|
90
|
+
nodes.inject([]) do |acc, el|
|
91
|
+
obj = new
|
92
|
+
attributes.each { |attr| obj.send("#{attr.name}=", attr.from_xml_node(el)) }
|
93
|
+
elements.each { |elem| obj.send("#{elem.name}=", elem.from_xml_node(el, namespace)) }
|
94
|
+
acc << obj
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def create_getter(name)
|
99
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
100
|
+
def #{name}
|
101
|
+
@#{name}
|
102
|
+
end
|
103
|
+
EOS
|
104
|
+
end
|
105
|
+
|
106
|
+
def create_setter(name)
|
107
|
+
class_eval <<-EOS, __FILE__, __LINE__
|
108
|
+
def #{name}=(value)
|
109
|
+
@#{name} = value
|
110
|
+
end
|
111
|
+
EOS
|
112
|
+
end
|
113
|
+
|
114
|
+
def create_accessor(name)
|
115
|
+
create_getter(name)
|
116
|
+
create_setter(name)
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
require 'happymapper/item'
|
122
|
+
require 'happymapper/attribute'
|
123
|
+
require 'happymapper/element'
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "xml/libxml"
|
2
|
+
|
3
|
+
class XML::Node
|
4
|
+
##
|
5
|
+
# Open up XML::Node from libxml and add convenience methods inspired
|
6
|
+
# by hpricot.
|
7
|
+
# (http://code.whytheluckystiff.net/hpricot/wiki/HpricotBasics)
|
8
|
+
# Also:
|
9
|
+
# * provide better handling of default namespaces
|
10
|
+
|
11
|
+
# an array of default namespaces to past into
|
12
|
+
attr_accessor :default_namespaces
|
13
|
+
|
14
|
+
# find the child node with the given xpath
|
15
|
+
def at(xpath)
|
16
|
+
self.find_first(xpath)
|
17
|
+
end
|
18
|
+
|
19
|
+
# find the array of child nodes matching the given xpath
|
20
|
+
def search(xpath)
|
21
|
+
results = self.find(xpath).to_a
|
22
|
+
if block_given?
|
23
|
+
results.each do |result|
|
24
|
+
yield result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
return results
|
28
|
+
end
|
29
|
+
|
30
|
+
# alias for search
|
31
|
+
def /(xpath)
|
32
|
+
search(xpath)
|
33
|
+
end
|
34
|
+
|
35
|
+
# return the inner contents of this node as a string
|
36
|
+
def inner_xml
|
37
|
+
child.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
# alias for inner_xml
|
41
|
+
def inner_html
|
42
|
+
inner_xml
|
43
|
+
end
|
44
|
+
|
45
|
+
# return this node and its contents as an xml string
|
46
|
+
def to_xml
|
47
|
+
self.to_s
|
48
|
+
end
|
49
|
+
|
50
|
+
# alias for path
|
51
|
+
def xpath
|
52
|
+
self.path
|
53
|
+
end
|
54
|
+
|
55
|
+
# provide a name for the default namespace
|
56
|
+
def register_default_namespace(name)
|
57
|
+
default_namespace = namespace.detect { |n| n.to_s == nil }
|
58
|
+
|
59
|
+
if default_namespace
|
60
|
+
register_namespace("#{name}:#{default_namespace.href}")
|
61
|
+
else
|
62
|
+
raise "No default namespace found"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# register a namespace, of the form "foo:http://example.com/ns"
|
67
|
+
def register_namespace(name_and_href)
|
68
|
+
(@default_namespaces ||= []) << name_and_href
|
69
|
+
end
|
70
|
+
|
71
|
+
def find_with_default_ns(xpath_expr, namespace=nil)
|
72
|
+
find_base(xpath_expr, namespace || default_namespaces)
|
73
|
+
end
|
74
|
+
|
75
|
+
def find_first_with_default_ns(xpath_expr, namespace=nil)
|
76
|
+
find_first_base(xpath_expr, namespace || default_namespaces)
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
alias_method :find_base, :find unless method_defined?(:find_base)
|
81
|
+
alias_method :find, :find_with_default_ns
|
82
|
+
|
83
|
+
alias_method :find_first_base, :find_first unless method_defined?(:find_first_base)
|
84
|
+
alias_method :find_first, :find_first_with_default_ns
|
85
|
+
end
|
86
|
+
|
87
|
+
class String
|
88
|
+
def to_libxml_doc
|
89
|
+
xp = XML::Parser.new
|
90
|
+
xp.string = self
|
91
|
+
return xp.parse
|
92
|
+
end
|
93
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/happymapper.rb'}"
|
9
|
+
puts "Loading happymapper gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|