Permalink
Browse files

initial commit, attributes and elements with primitive types work

  • Loading branch information...
0 parents commit 9d8c6310e5be27af026795d900573f5b98ada6ca @jnunemaker committed Aug 14, 2008
@@ -0,0 +1,4 @@
+== 0.1.0 2008-08-13
+
+* 1 major enhancement:
+ * Initial release
@@ -0,0 +1,20 @@
+Copyright (c) 2008 John Nunemaker
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
+History.txt
+License.txt
+Manifest.txt
+PostInstall.txt
+README.txt
+Rakefile
+config/hoe.rb
+config/requirements.rb
+lib/happymapper.rb
+lib/happymapper/version.rb
+script/console
+script/destroy
+script/generate
+script/txt2html
+setup.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/index.html
+website/index.txt
+website/javascripts/rounded_corners_lite.inc.js
+website/stylesheets/screen.css
+website/template.html.erb
@@ -0,0 +1,7 @@
+
+For more information on happymapper, see http://happymapper.rubyforge.org
+
+NOTE: Change this information in PostInstall.txt
+You can also delete it if you don't want it.
+
+
@@ -0,0 +1,48 @@
+= happymapper
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 John Nunemaker
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
+require 'config/requirements'
+require 'config/hoe' # setup Hoe + all gem configuration
+
+Dir['tasks/**/*.rake'].each { |rake| load rake }
@@ -0,0 +1,73 @@
+require 'happymapper/version'
+
+AUTHOR = 'John Nunemaker' # can also be an array of Authors
+EMAIL = "nunemaker@gmail.com"
+DESCRIPTION = "description of gem"
+GEM_NAME = 'happymapper' # what ppl will type to install your gem
+RUBYFORGE_PROJECT = 'happymapper' # The unix name for your project
+HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
+DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
+EXTRA_DEPENDENCIES = [
+# ['activesupport', '>= 1.3.1']
+] # An array of rubygem dependencies [name, version]
+
+@config_file = "~/.rubyforge/user-config.yml"
+@config = nil
+RUBYFORGE_USERNAME = "unknown"
+def rubyforge_username
+ unless @config
+ begin
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
+ rescue
+ puts <<-EOS
+ERROR: No rubyforge config file found: #{@config_file}
+Run 'rubyforge setup' to prepare your env for access to Rubyforge
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
+ EOS
+ exit
+ end
+ end
+ RUBYFORGE_USERNAME.replace @config["username"]
+end
+
+
+REV = nil
+# UNCOMMENT IF REQUIRED:
+# REV = YAML.load(`svn info`)['Revision']
+VERS = HappyMapper::VERSION::STRING + (REV ? ".#{REV}" : "")
+RDOC_OPTS = ['--quiet', '--title', 'happymapper documentation',
+ "--opname", "index.html",
+ "--line-numbers",
+ "--main", "README",
+ "--inline-source"]
+
+class Hoe
+ def extra_deps
+ @extra_deps.reject! { |x| Array(x).first == 'hoe' }
+ @extra_deps
+ end
+end
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.new(GEM_NAME, VERS) do |p|
+ p.developer(AUTHOR, EMAIL)
+ p.description = DESCRIPTION
+ p.summary = DESCRIPTION
+ p.url = HOMEPATH
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
+ p.test_globs = ["test/**/test_*.rb"]
+ p.clean_globs |= ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store'] #An array of file patterns to delete on clean.
+
+ # == Optional
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
+ #p.extra_deps = EXTRA_DEPENDENCIES
+
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
+ end
+
+CHANGES = $hoe.paragraphs_of('History.txt', 0..1).join("\\n\\n")
+PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
+$hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
+$hoe.rsync_args = '-av --delete --ignore-errors'
+$hoe.spec.post_install_message = File.open(File.dirname(__FILE__) + "/../PostInstall.txt").read rescue ""
@@ -0,0 +1,15 @@
+require 'fileutils'
+include FileUtils
+
+require 'rubygems'
+%w[rake hoe newgem rubigen].each do |req_gem|
+ begin
+ require req_gem
+ rescue LoadError
+ puts "This Rakefile requires the '#{req_gem}' RubyGem."
+ puts "Installation: gem install #{req_gem} -y"
+ exit
+ end
+end
+
+$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
@@ -0,0 +1,11 @@
+class Post
+ include HappyMapper
+
+ attribute :href, String
+ attribute :hash, String
+ attribute :description, String
+ attribute :tag, String
+ attribute :time, DateTime
+ attribute :others, Integer
+ attribute :extended, String
+end
@@ -0,0 +1,143 @@
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+require 'date'
+require 'time'
+require 'rubygems'
+gem 'libxml-ruby', '>= 0.8.3'
+require 'xml'
+
+class Boolean; end
+
+module HappyMapper
+
+ def self.included(base)
+ base.instance_variable_set("@attributes", {})
+ base.instance_variable_set("@elements", {})
+ base.extend ClassMethods
+ end
+
+ module ClassMethods
+ def attribute(name, type)
+ attribute = Attribute.new(name, type)
+ @attributes[to_s] ||= []
+ @attributes[to_s] << attribute
+ create_accessor(attribute.name)
+ end
+
+ def attributes
+ @attributes[to_s] || []
+ end
+
+ def element(name, type)
+ element = Element.new(name, type)
+ @elements[to_s] ||= []
+ @elements[to_s] << element
+ create_accessor(element.name)
+ end
+
+ def elements
+ @elements[to_s] || []
+ end
+
+ def tag_name(new_tag_name)
+ @tag_name = new_tag_name.to_s
+ end
+
+ def get_tag_name
+ @tag_name ||= to_s.downcase
+ end
+
+ def create_getter(name)
+ class_eval <<-EOS, __FILE__, __LINE__
+ def #{name}
+ @#{name}
+ end
+ EOS
+ end
+
+ def create_setter(name)
+ class_eval <<-EOS, __FILE__, __LINE__
+ def #{name}=(value)
+ @#{name} = value
+ end
+ EOS
+ end
+
+ def create_accessor(name)
+ create_getter(name)
+ create_setter(name)
+ end
+
+ def parse(xml)
+ parser = XML::Parser.new
+ parser.string = xml
+ doc = parser.parse
+ collection = []
+ doc.find(get_tag_name).each do |el|
+ obj = new
+ # TODO: reading value from xml should move to Item
+ attributes.each { |a| obj.send("#{a.name}=", a.typecast(el.attributes[a.xml_name])) }
+ elements.each { |e| obj.send("#{e.name}=", e.typecast(el.find(e.xml_name).first.content)) }
+ collection << obj
+ end
+ collection
+ end
+ end
+
+ class Item
+ attr_accessor :type, :xml_name
+ attr_reader :name
+
+ def initialize(name, type, o={})
+ self.name, self.type, self.xml_name = name, type, o.delete(:xml_name) || name.to_s
+ @options = {}.merge(o)
+ @xml_type = self.class.to_s.split('::').last.downcase
+ end
+
+ def name=(new_name)
+ @name = new_name.to_s
+ end
+
+ def typecast(value)
+ return value if value.kind_of?(type) || value.nil?
+ begin
+ if type == String then value.to_s
+ elsif type == Float then value.to_f
+ elsif type == Time then Time.parse(value.to_s)
+ elsif type == Date then Date.parse(value.to_s)
+ elsif type == DateTime then DateTime.parse(value.to_s)
+ elsif type == Boolean then ['true', 't', '1'].include?(value.to_s.downcase)
+ elsif type == Integer
+ # ganked from datamapper
+ value_to_i = value.to_i
+ if value_to_i == 0 && value != '0'
+ value_to_s = value.to_s
+ begin
+ Integer(value_to_s =~ /^(\d+)/ ? $1 : value_to_s)
+ rescue ArgumentError
+ nil
+ end
+ else
+ value_to_i
+ end
+ else
+ value
+ end
+ rescue
+ value
+ end
+ end
+
+ def element?
+ @xml_type == 'element'
+ end
+
+ def attribute?
+ @xml_type == 'attribute'
+ end
+ end
+
+ class Element < Item; end
+ class Attribute < Item; end
+end
@@ -0,0 +1,9 @@
+module HappyMapper
+ module VERSION #:nodoc:
+ MAJOR = 0
+ MINOR = 1
+ TINY = 0
+
+ STRING = [MAJOR, MINOR, TINY].join('.')
+ end
+end
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs = " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs << " -r #{File.dirname(__FILE__) + '/../lib/happymapper.rb'}"
+puts "Loading happymapper gem"
+exec "#{irb} #{libs} --simple-prompt"
Oops, something went wrong.

0 comments on commit 9d8c631

Please sign in to comment.