Permalink
Browse files

Added support for after parse callbacks.

  • Loading branch information...
1 parent 2e5bf2e commit 97693dfc5237833cbbe6ca40d750397fd6e2eaf3 @jnunemaker committed Jul 4, 2010
Showing with 366 additions and 329 deletions.
  1. +25 −15 lib/happymapper.rb
  2. +51 −314 spec/happymapper_spec.rb
  3. +290 −0 spec/support/models.rb
View
@@ -13,19 +13,19 @@ def self.included(base)
base.instance_variable_set("@elements", {})
base.extend ClassMethods
end
-
+
module ClassMethods
def attribute(name, type, options={})
attribute = Attribute.new(name, type, options)
@attributes[to_s] ||= []
@attributes[to_s] << attribute
attr_accessor attribute.method_name.intern
end
-
+
def attributes
@attributes[to_s] || []
end
-
+
def element(name, type, options={})
element = Element.new(name, type, options)
@elements[to_s] ||= []
@@ -37,15 +37,23 @@ def content(name)
@content = name
attr_accessor name
end
-
+
+ def after_parse_callbacks
+ @after_parse_callbacks ||= []
+ end
+
+ def after_parse(&block)
+ after_parse_callbacks.push(block)
+ end
+
def elements
@elements[to_s] || []
end
-
+
def has_one(name, type, options={})
element name, type, {:single => true}.merge(options)
end
-
+
def has_many(name, type, options={})
element name, type, {:single => false}.merge(options)
end
@@ -61,11 +69,11 @@ def namespace(namespace = nil)
def tag(new_tag_name)
@tag_name = new_tag_name.to_s
end
-
+
def tag_name
@tag_name ||= to_s.split('::')[-1].downcase
end
-
+
def parse(xml, options = {})
if xml.is_a?(XML::Node)
node = xml
@@ -85,23 +93,25 @@ def parse(xml, options = {})
xpath = root ? '/' : './/'
xpath += "#{DEFAULT_NS}:" if namespace
xpath += tag_name
-
+
nodes = node.find(xpath, Array(namespace))
collection = nodes.collect do |n|
obj = new
-
- attributes.each do |attr|
- obj.send("#{attr.method_name}=",
+
+ attributes.each do |attr|
+ obj.send("#{attr.method_name}=",
attr.from_xml_node(n, namespace))
end
-
+
elements.each do |elem|
- obj.send("#{elem.method_name}=",
+ obj.send("#{elem.method_name}=",
elem.from_xml_node(n, namespace))
end
obj.send("#{@content}=", n.content) if @content
-
+
+ obj.class.after_parse_callbacks.each { |callback| callback.call(obj) }
+
obj
end
Oops, something went wrong. Retry.

0 comments on commit 97693df

Please sign in to comment.