Sha256: 3b4da9f494b5745c0c137ec3340048da3e0818f5437feb88aee34b40cd778f9c

Contents?: true

Size: 1.45 KB

Versions: 15

Compression:

Stored size: 1.45 KB

Contents

require 'representable'
require 'representable/bindings/xml_bindings'
require 'nokogiri'

module Representable
  module XML
    def self.included(base)
      base.class_eval do
        include Representable
        extend ClassMethods
        self.representation_wrap = true # let representable compute it.
      end
    end
    
    
    module ClassMethods
      # Creates a new Ruby object from XML using mapping information declared in the class.
      #
      # Accepts a block yielding the currently iterated Definition. If the block returns false 
      # the property is skipped.
      #
      # Example:
      #   band.from_xml("<band><name>Nofx</name></band>")
      def from_xml(*args, &block)
        create_represented(*args, &block).from_xml(*args)
      end
      
      def from_node(*args, &block)
        create_represented(*args, &block).from_node(*args)
      end
    end
    
    
    def from_xml(doc, *args)
      node = Nokogiri::XML(doc).root
      from_node(node, *args)
    end
    
    def from_node(node, options={})
      update_properties_from(node, options, PropertyBinding)
    end
    
    # Returns a Nokogiri::XML object representing this object.
    def to_node(options={})
      root_tag = options[:wrap] || representation_wrap
      
      create_representation_with(Nokogiri::XML::Node.new(root_tag.to_s, Nokogiri::XML::Document.new), options, PropertyBinding)
    end
    
    def to_xml(*args)
      to_node(*args).to_s
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
representable-1.5.3 lib/representable/xml.rb
representable-1.5.2 lib/representable/xml.rb
representable-1.5.1 lib/representable/xml.rb
representable-1.5.0 lib/representable/xml.rb
representable-1.4.2 lib/representable/xml.rb
representable-1.4.1 lib/representable/xml.rb
representable-1.4.0 lib/representable/xml.rb
representable-1.3.5 lib/representable/xml.rb
representable-1.3.4 lib/representable/xml.rb
representable-1.3.3 lib/representable/xml.rb
representable-1.3.2 lib/representable/xml.rb
representable-1.3.1 lib/representable/xml.rb
representable-1.3.0 lib/representable/xml.rb
representable-1.2.9 lib/representable/xml.rb
representable-1.2.8 lib/representable/xml.rb