Module: Serialize
- Defined in:
- lib/simple_mapper/support/bliss_serializer.rb
Constant Summary collapse
- XML_OPTIONS =
{ :include_key => :attribute, # Can be false, :element, or :attribute :report_nil => true, # Sets an attribute nil="true" on elements that are nil, so that the reader doesn't read as an empty string :key_name => 'id', # Default key name }
Class Method Summary collapse
- .hash_from_xml(xml, options = {}) ⇒ Object
-
.object_to_xml(obj, options = {}) ⇒ Object
Default key name.
- .to_xml(root, attributes = {}, options = {}) ⇒ Object
Class Method Details
.hash_from_xml(xml, options = {}) ⇒ Object
43 44 45 |
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 43 def self.hash_from_xml(xml,={}) xml.formatted(:xml).to_hash end |
.object_to_xml(obj, options = {}) ⇒ Object
Default key name
12 13 14 15 16 17 |
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 12 def self.object_to_xml(obj, ={}) # Automatically set the key_name for DataMapper objects .merge!(:key_name => obj.class.table.key.name) if obj.class.respond_to?(:table) && obj.class.respond_to?(:persistent?) && obj.class.persistent? # Should do the above also for ActiveRecord... to_xml(obj.class.name.underscore, obj.to_hash(), ) end |
.to_xml(root, attributes = {}, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/simple_mapper/support/bliss_serializer.rb', line 18 def self.to_xml(root, attributes={}, ={}) = XML_OPTIONS.merge() attributes = attributes.dup.stringify_keys! doc = REXML::Document.new root_element = doc.add_element(root) case [:include_key] when :attribute root_element.add_attribute([:key_name], attributes.delete([:key_name].to_s).to_s).extended do def self.to_string; %Q[#@expanded_name="#{to_s().gsub(/"/, '"')}"] end end when :element root_element.add_element([:key_name]) << REXML::Text.new(attributes.delete([:key_name].to_s).to_s) end attributes.each do |key,value| node = root_element.add_element(key) node << REXML::Text.new(value.to_s) unless value.nil? node.add_attribute('nil', 'true') if value.nil? && [:report_nil] end doc end |