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/syntax_error.rb,
lib/happymapper/class_methods.rb,
lib/happymapper/supported_types.rb,
lib/happymapper/anonymous_mapper.rb
Defined Under Namespace
Modules: ClassMethods, SupportedTypes Classes: AnonymousMapper, AnonymousWrapperClassFactory, Attribute, Boolean, Element, Item, SyntaxError, TextNode, XmlContent
Constant Summary collapse
- VERSION =
"0.10.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Object
Set all attributes with a default to their default values.
-
#parse(xml, options = {}) ⇒ Object
Parse the xml and update this instance.
-
#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.
Class Method Details
.included(base) ⇒ Object
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/happymapper.rb', line 19 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
15 16 17 |
# File 'lib/happymapper.rb', line 15 def self.parse(xml_content) AnonymousMapper.new.parse(xml_content) end |
Instance Method Details
#initialize ⇒ Object
Set all attributes with a default to their default values
44 45 46 47 48 49 |
# File 'lib/happymapper.rb', line 44 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.
146 147 148 |
# File 'lib/happymapper.rb', line 146 def parse(xml, = {}) self.class.parse(xml, .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.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/happymapper.rb', line 74 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. # tag_name = tag_from_parent || self.class.tag_name builder.send(:"#{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) && !content.[:read_only] value = send(content.name) value = apply_on_save_action(content, value) builder.text(value) 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 |