gotascii-happymapper 0.1.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.
data/History.txt ADDED
@@ -0,0 +1,20 @@
1
+ == 0.1.5 2009-01-05
2
+ * 1 major enhancement:
3
+ * Updated to latest version of libxml-ruby (lightningdb)
4
+
5
+ == 0.1.4 2009-01-05
6
+ * 1 major enhancement:
7
+ * Fixed parsing when the object is the root node. (Garret Alfert)
8
+
9
+ == 0.1.3 2008-12-31
10
+ * 1 major enhancement:
11
+ * Added parsing of attributes of elements that are also mapped, see current_weather.rb for example (jeremyf)
12
+
13
+ == 0.1.2 2008-12-12
14
+ * 1 major enhancement:
15
+ * Fixed that :deep only worked for first item (dvrensk)
16
+
17
+ == 0.1.0 2008-11-16
18
+
19
+ * 1 major enhancement:
20
+ * Initial release
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,42 @@
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/current_weather.rb
12
+ examples/post.rb
13
+ examples/twitter.rb
14
+ happymapper.gemspec
15
+ lib/happymapper.rb
16
+ lib/happymapper/attribute.rb
17
+ lib/happymapper/element.rb
18
+ lib/happymapper/item.rb
19
+ lib/happymapper/version.rb
20
+ lib/libxml_ext/libxml_helper.rb
21
+ script/console
22
+ script/destroy
23
+ script/generate
24
+ script/txt2html
25
+ setup.rb
26
+ spec/fixtures/address.xml
27
+ spec/fixtures/current_weather.xml
28
+ spec/fixtures/pita.xml
29
+ spec/fixtures/posts.xml
30
+ spec/fixtures/statuses.xml
31
+ spec/happymapper_attribute_spec.rb
32
+ spec/happymapper_element_spec.rb
33
+ spec/happymapper_item_spec.rb
34
+ spec/happymapper_spec.rb
35
+ spec/spec.opts
36
+ spec/spec_helper.rb
37
+ tasks/deployment.rake
38
+ tasks/environment.rake
39
+ tasks/rspec.rake
40
+ tasks/website.rake
41
+ website/css/common.css
42
+ 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
@@ -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 }
data/TODO.txt ADDED
@@ -0,0 +1,2 @@
1
+ * doesn't do xml namespaces really (does work with default namespace though)
2
+ * switch gem management to echoe as i did with httparty
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.9.7']
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/current_weather.xml')
5
+
6
+ class CurrentWeather
7
+ include HappyMapper
8
+ tag 'aws:ob'
9
+ element :temperature, Integer, :tag => 'aws:temp'
10
+ element :feels_like, Integer, :tag => 'aws:feels-like'
11
+ element :current_condition, String, :tag => 'aws:current-condition', :attributes => {:icon => String}
12
+ end
13
+
14
+ CurrentWeather.parse(file_contents).each do |current_weather|
15
+ puts "temperature: #{current_weather.temperature}"
16
+ puts "feels_like: #{current_weather.feels_like}"
17
+ puts "current_condition: #{current_weather.current_condition}"
18
+ puts "current_condition.icon: #{current_weather.current_condition.icon}"
19
+ 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, '' }
@@ -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.6"
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{2009-01-04}
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/current_weather.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/address.xml", "spec/fixtures/current_weather.xml", "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,133 @@
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.9.7'
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
+ nodes = if namespace
80
+ node = doc.respond_to?(:root) ? doc.root : doc
81
+ node.register_default_namespace(namespace.chop)
82
+ node.find("#{namespace}#{get_tag_name}")
83
+ else
84
+ nested = '.' unless doc.respond_to?(:root)
85
+ path = "#{nested}//#{get_tag_name}"
86
+ doc.find(path)
87
+ end
88
+
89
+ collection = create_collection(nodes, namespace)
90
+
91
+ # per http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Document.html#M000354
92
+ nodes = nil
93
+ GC.start
94
+
95
+ options[:single] ? collection.first : collection
96
+ end
97
+
98
+ private
99
+ def create_collection(nodes, namespace=nil)
100
+ nodes.inject([]) do |acc, el|
101
+ obj = new
102
+ attributes.each { |attr| obj.send("#{attr.name}=", attr.from_xml_node(el)) }
103
+ elements.each { |elem| obj.send("#{elem.name}=", elem.from_xml_node(el, namespace)) }
104
+ acc << obj
105
+ end
106
+ end
107
+
108
+ def create_getter(name)
109
+ class_eval <<-EOS, __FILE__, __LINE__
110
+ def #{name}
111
+ @#{name}
112
+ end
113
+ EOS
114
+ end
115
+
116
+ def create_setter(name)
117
+ class_eval <<-EOS, __FILE__, __LINE__
118
+ def #{name}=(value)
119
+ @#{name} = value
120
+ end
121
+ EOS
122
+ end
123
+
124
+ def create_accessor(name)
125
+ create_getter(name)
126
+ create_setter(name)
127
+ end
128
+ end
129
+ end
130
+
131
+ require 'happymapper/item'
132
+ require 'happymapper/attribute'
133
+ require 'happymapper/element'