Module: HappyMapper

Defined in:
lib/happymapper.rb,
lib/happymapper/item.rb,
lib/happymapper/element.rb,
lib/happymapper/version.rb,
lib/happymapper/attribute.rb,
lib/happymapper/text_node.rb,
lib/happymapper/supported_types.rb,
lib/happymapper/anonymous_mapper.rb

Defined Under Namespace

Modules: ClassMethods, SupportedTypes Classes: AnonymousMapper, AnonymousWrapperClassFactory, Attribute, Boolean, Element, Item, TextNode, XmlContent

Constant Summary collapse

VERSION =
'0.8.1'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/happymapper.rb', line 17

def self.included(base)
  if base.superclass <= HappyMapper
    base.instance_eval do
      @attributes =
        superclass.instance_variable_get(:@attributes).dup
      @elements =
        superclass.instance_variable_get(:@elements).dup
      @registered_namespaces =
        superclass.instance_variable_get(:@registered_namespaces).dup
      @wrapper_anonymous_classes =
        superclass.instance_variable_get(:@wrapper_anonymous_classes).dup
    end
  else
    base.instance_eval do
      @attributes = {}
      @elements = {}
      @registered_namespaces = {}
      @wrapper_anonymous_classes = {}
    end
  end

  base.extend ClassMethods
end

.parse(xml_content) ⇒ Object



13
14
15
# File 'lib/happymapper.rb', line 13

def self.parse(xml_content)
  AnonymousMapper.new.parse(xml_content)
end

Instance Method Details

#initializeObject

Set all attributes with a default to their default values



487
488
489
490
491
492
# File 'lib/happymapper.rb', line 487

def initialize
  super
  self.class.attributes.reject { |attr| attr.default.nil? }.each do |attr|
    send("#{attr.method_name}=", attr.default)
  end
end

#parse(xml, options = {}) ⇒ Object

Parse the xml and update this instance. This does not update instances of HappyMappers that are children of this object. New instances will be created for any HappyMapper children of this object.

Params and return are the same as the class parse() method above.



591
592
593
# File 'lib/happymapper.rb', line 591

def parse(xml, options = {})
  self.class.parse(xml, options.merge!(update: self))
end

#to_xml(builder = nil, default_namespace = nil, namespace_override = nil, tag_from_parent = nil) ⇒ String, Nokogiri::XML::Builder

Create an xml representation of the specified class based on defined HappyMapper elements and attributes. The method is defined in a way that it can be called recursively by classes that are also HappyMapper classes, allowg for the composition of classes.

Parameters:

  • builder (Nokogiri::XML::Builder) (defaults to: nil)

    an instance of the XML builder which is being used when called recursively.

  • default_namespace (String) (defaults to: nil)

    The name of the namespace which is the default for the xml being produced; this is the namespace of the parent

  • namespace_override (String) (defaults to: nil)

    The namespace specified with the element declaration in the parent. Overrides the namespace declaration in the element class itself when calling #to_xml recursively.

  • tag_from_parent (String) (defaults to: nil)

    The xml tag to use on the element when being called recursively. This lets the parent doc define its own structure. Otherwise the element uses the tag it has defined for itself. Should only apply when calling a child HappyMapper element.

Returns:

  • (String, Nokogiri::XML::Builder)

    return XML representation of the HappyMapper object; when called recursively this is going to return and Nokogiri::XML::Builder object.



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/happymapper.rb', line 517

def to_xml(builder = nil, default_namespace = nil, namespace_override = nil,
           tag_from_parent = nil)

  #
  # If to_xml has been called without a passed in builder instance that
  # means we are going to return xml output. When it has been called with

  # a builder instance that means we most likely being called recursively
  # and will return the end product as a builder instance.
  #
  unless builder
    write_out_to_xml = true
    builder = Nokogiri::XML::Builder.new
  end

  attributes = collect_writable_attributes

  #
  # If the object we are serializing has a namespace declaration we will want
  # to use that namespace or we will use the default namespace.
  # When neither are specifed we are simply using whatever is default to the
  # builder
  #
  namespace_name = namespace_override || self.class.namespace || default_namespace

  #
  # Create a tag in the builder that matches the class's tag name unless a tag was passed
  # in a recursive call from the parent doc.  Then append
  # any attributes to the element that were defined above.
  #
  builder.send("#{tag_from_parent || self.class.tag_name}_", attributes) do |xml|
    register_namespaces_with_builder(builder)

    xml.parent.namespace =
      builder.doc.root.namespace_definitions.find { |x| x.prefix == namespace_name }

    #
    # When a content has been defined we add the resulting value
    # the output xml
    #
    if (content = self.class.defined_content)

      unless content.options[:read_only]
        value = send(content.name)
        value = apply_on_save_action(content, value)

        builder.text(value)
      end

    end

    #
    # for every define element (i.e. has_one, has_many, element) we are
    # going to persist each one
    #
    self.class.elements.each do |element|
      element_to_xml(element, xml, default_namespace)
    end
  end

  # Write out to XML, this value was set above, based on whether or not an XML
  # builder object was passed to it as a parameter. When there was no parameter
  # we assume we are at the root level of the #to_xml call and want the actual
  # xml generated from the object. If an XML builder instance was specified
  # then we assume that has been called recursively to generate a larger
  # XML document.
  write_out_to_xml ? builder.to_xml.force_encoding('UTF-8') : builder
end