Permalink
Browse files

added SAXMachine.configure syntax

  • Loading branch information...
1 parent ba7e422 commit b12caf6df47a48437677e4fa68456f1ba5382aee @archiloque archiloque committed Sep 30, 2011
Showing with 110 additions and 0 deletions.
  1. +4 −0 HISTORY.md
  2. +11 −0 README.textile
  3. +1 −0 lib/sax-machine.rb
  4. +38 −0 lib/sax-machine/sax_configure.rb
  5. +3 −0 sax-machine.gemspec
  6. +53 −0 spec/sax-machine/configure_sax_machine_spec.rb
View
@@ -0,0 +1,4 @@
+# 0.1.0
+
+- rename parent to ancestor
+- added SAXMachine.configure
View
@@ -71,6 +71,17 @@ response.messages.first # => "hi"
response.messages.last # => "world"
</pre>
+# To limit conflicts in the class used for mappping, you can use the alternate SAXMachine.configure syntax
+
+class X < ActiveRecord::Base
+
+ # this way no element, elements or ancestor method will be added to X
+ SAXMachine.configure(X) do |c|
+ c.element :title
+ end
+
+end
+
h2. LICENSE
(The MIT License)
View
@@ -3,6 +3,7 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
require "sax-machine/sax_document"
+require "sax-machine/sax_configure"
require "sax-machine/sax_handler"
require "sax-machine/sax_config"
@@ -0,0 +1,38 @@
+module SAXMachine
+
+ def self.configure(clazz)
+ extended_clazz = Class.new(clazz)
+ extended_clazz.send(:include, SAXMachine)
+
+ # override create_attr to create attributes on the original class
+ def extended_clazz.create_attr real_name
+ superclass.send(:attr_reader, real_name) unless superclass.method_defined?(real_name)
+ superclass.send(:attr_writer, real_name) unless superclass.method_defined?("#{real_name}=")
+ end
+
+ yield(extended_clazz)
+
+ clazz.extend LightWeightSaxMachine
+ clazz.sax_config = extended_clazz.sax_config
+
+ (class << clazz;self;end).send(:define_method, :parse) do |xml_text|
+ extended_clazz.parse(xml_text)
+ end
+
+ end
+
+ module LightWeightSaxMachine
+
+ attr_writer :sax_config
+
+ def sax_config
+ @sax_config ||= SAXConfig.new
+ end
+
+ def inherited(subclass)
+ subclass.sax_config.send(:initialize_copy, self.sax_config)
+ end
+
+ end
+
+end
View
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
"lib/sax-machine/sax_ancestor_config.rb",
"lib/sax-machine/sax_attribute_config.rb",
"lib/sax-machine/sax_config.rb",
+ "lib/sax-machine/sax_configure.rb",
"lib/sax-machine/sax_document.rb",
"lib/sax-machine/sax_collection_config.rb",
"lib/sax-machine/sax_element_config.rb",
@@ -21,6 +22,8 @@ Gem::Specification.new do |s|
"README.textile", "Rakefile",
"Gemfile",
"spec/spec_helper.rb",
+ "spec/sax-machine/configure_sax_machine_spec.rb",
+ "spec/sax-machine/include_sax_machine_spec.rb",
"spec/sax-machine/sax_document_spec.rb"]
s.homepage = %q{http://github.com/pauldix/sax-machine}
s.require_paths = ["lib"]
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+class A
+
+ SAXMachine.configure(A) do |c|
+ c.element :title
+ end
+
+end
+
+class B < A
+
+ SAXMachine.configure(B) do |c|
+ c.element :b
+ end
+
+end
+
+class C < B
+
+ SAXMachine.configure(C) do |c|
+ c.element :c
+ end
+
+end
+
+describe "SAXMachine configure" do
+ before do
+ xml = "<top><title>Test</title><b>Matched!</b><c>And Again</c></top>"
+ @a = A.new
+ @a.parse xml
+ @b = B.new
+ @b.parse xml
+ @c = C.new
+ @c.parse xml
+ end
+ it { @a.should be_a(A) }
+ it { @a.should_not be_a(B) }
+ it { @a.should be_a(SAXMachine) }
+ it { @a.title.should == "Test" }
+ it { @b.should be_a(A) }
+ it { @b.should be_a(B) }
+ it { @b.should be_a(SAXMachine) }
+ it { @b.title.should == "Test" }
+ it { @b.b.should == "Matched!" }
+ it { @c.should be_a(A) }
+ it { @c.should be_a(B) }
+ it { @c.should be_a(C) }
+ it { @c.should be_a(SAXMachine) }
+ it { @c.title.should == "Test" }
+ it { @c.b.should == "Matched!" }
+ it { @c.c.should == "And Again" }
+end

0 comments on commit b12caf6

Please sign in to comment.