Module: Juniter::HasChildren
- Included in:
- Element
- Defined in:
- lib/juniter/has_children.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
4 5 6 |
# File 'lib/juniter/has_children.rb', line 4 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#assign_children_from_xml(nodes) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/juniter/has_children.rb', line 50 def assign_children_from_xml(nodes) child_map = self.class.child_types.each_with_object({}) do |name, hash| hash[self.class.child_aliases.fetch(name, name).to_s] = name end nodes.each do |node| next if node.is_a?(Ox::Comment) if node.is_a?(String) || node.is_a?(Ox::CData) value = node.is_a?(String) ? node : node.value text_child = self.class.text_child next unless text_child public_send :"#{text_child}=", [ public_send(:"#{text_child}"), value ].join next end name = child_map.fetch(node.value) mapped = self.class.child_processors[name].call(node) if self.class.array_children.include?(name) public_send("#{name}") << mapped else public_send "#{name}=", mapped end end end |
#children_xml ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/juniter/has_children.rb', line 76 def children_xml self.class.child_types.each_with_object([]) do |name, children| value = public_send(name) next if value.nil? element = self.class.child_aliases.fetch(name, name) if self.class.array_children.include?(name) children.concat value.map { |child| child.respond_to?(:to_xml) ? child.to_xml : Ox::Element.new(element).tap { |el| el << child } } elsif self.class.text_child == name children << value else children << (value.respond_to?(:to_xml) ? value.to_xml : Ox::Element.new(element).tap { |el| el << value }) end end end |