happymapper 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.1.0 2008-11-16
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -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.
@@ -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
File without changes
@@ -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
@@ -0,0 +1,4 @@
1
+ require 'config/requirements'
2
+ require 'config/hoe' # setup Hoe + all gem configuration
3
+
4
+ Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1 @@
1
+ * doesn't do xml namespaces really (does work with default namespace though)
@@ -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]))
@@ -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
@@ -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, '' }
@@ -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
@@ -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.0"
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-11-17}
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,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,3 @@
1
+ module HappyMapper
2
+ class Attribute < Item; end
3
+ end