lib/happymapper.rb in nokogiri-happymapper-0.5.6 vs lib/happymapper.rb in nokogiri-happymapper-0.5.7

- old
+ new

@@ -1,23 +1,36 @@ require 'nokogiri' require 'date' require 'time' +require 'happymapper/anonymous_mapper' -class Boolean; end -class XmlContent; end - module HappyMapper + class Boolean; end + class XmlContent; end - VERSION = "0.5.6" + extend AnonymousMapper DEFAULT_NS = "happymapper" def self.included(base) - base.instance_variable_set("@attributes", {}) - base.instance_variable_set("@elements", {}) - base.instance_variable_set("@registered_namespaces", {}) - base.instance_variable_set("@wrapper_anonymous_classes", {}) + if !(base.superclass <= HappyMapper) + base.instance_eval do + @attributes = [] + @elements = [] + @registered_namespaces = {} + @wrapper_anonymous_classes = {} + end + else + base.instance_eval do + @attributes = superclass.attributes.dup + @elements = superclass.elements.dup + @registered_namespaces = + superclass.instance_variable_get(:@registered_namespaces).dup + @wrapper_anonymous_classes = + superclass.instance_variable_get(:@wrapper_anonymous_classes).dup + end + end base.extend ClassMethods end module ClassMethods @@ -37,23 +50,22 @@ # the object will be converted upon parsing # @param [Hash] options additional parameters to send to the relationship # def attribute(name, type, options={}) attribute = Attribute.new(name, type, options) - @attributes[to_s] ||= [] - @attributes[to_s] << attribute + @attributes << attribute attr_accessor attribute.method_name.intern end # # The elements defined through {#attribute}. # # @return [Array<Attribute>] a list of the attributes defined for this class; # an empty array is returned when there have been no attributes defined. # def attributes - @attributes[to_s] || [] + @attributes end # # Register a namespace that is used to persist the object namespace back to # XML. @@ -93,12 +105,11 @@ # the object will be converted upon parsing # @param [Hash] options additional parameters to send to the relationship # def element(name, type, options={}) element = Element.new(name, type, options) - @elements[to_s] ||= [] - @elements[to_s] << element + @elements << element attr_accessor element.method_name.intern end # # The elements defined through {#element}, {#has_one}, and {#has_many}. @@ -106,11 +117,11 @@ # @return [Array<Element>] a list of the elements contained defined for this # class; an empty array is returned when there have been no elements # defined. # def elements - @elements[to_s] || [] + @elements end # # The value stored in the text node of the current element. # @@ -303,13 +314,11 @@ if options[:namespace] namespace = options[:namespace] elsif namespaces.has_key?("xmlns") namespace ||= DEFAULT_NS - default_namespace = namespaces.delete("xmlns") - namespaces[namespace] ||= default_namespace - namespaces["xmlns:#{namespaces.key(default_namespace)}"] = default_namespace + namespaces[DEFAULT_NS] = namespaces.delete("xmlns") elsif namespaces.has_key?(DEFAULT_NS) namespace ||= DEFAULT_NS end # from the options grab any nodes present and if none are present then @@ -370,11 +379,13 @@ # values from the xml being parsed. Otherwise, create a new object obj = options[:update] ? options[:update] : new attributes.each do |attr| - obj.send("#{attr.method_name}=",attr.from_xml_node(n, namespace, namespaces)) + value = attr.from_xml_node(n, namespace, namespaces) + value = attr.default if value.nil? + obj.send("#{attr.method_name}=", value) end elements.each do |elem| obj.send("#{elem.method_name}=",elem.from_xml_node(n, namespace, namespaces)) end @@ -429,11 +440,18 @@ collection.first else collection end end + end + # Set all attributes with a default to their default values + def initialize + super + self.class.attributes.reject {|attr| attr.default.nil?}.each do |attr| + send("#{attr.method_name}=", attr.default) + end end # # Create an xml representation of the specified class based on defined # HappyMapper elements and attributes. The method is defined in a way @@ -479,10 +497,11 @@ # the below process # unless attribute.options[:read_only] value = send(attribute.method_name) + value = nil if value == attribute.default # # If the attribute defines an on_save lambda/proc or value that maps to # a method that the class has defined, then call it with the value as a # parameter. @@ -699,9 +718,10 @@ end end end -require File.dirname(__FILE__) + '/happymapper/item' -require File.dirname(__FILE__) + '/happymapper/attribute' -require File.dirname(__FILE__) + '/happymapper/element' -require File.dirname(__FILE__) + '/happymapper/text_node' +require 'happymapper/supported_types' +require 'happymapper/item' +require 'happymapper/attribute' +require 'happymapper/element' +require 'happymapper/text_node'