Module: Transformator::Dsl
- Included in:
- Transformation
- Defined in:
- lib/transformator/dsl.rb
Instance Method Summary collapse
- #array(name_or_node, &block) ⇒ Object
- #cdata(content, &block) ⇒ Object
- #element(name, options = {}) {|new_element| ... } ⇒ Object
- #element_from_xml(xml, options = {}) ⇒ Object
- #elements_from_hash(hash) ⇒ Object
- #elements_from_xml(xml, options = {}) ⇒ Object
- #find(node, path, &block) ⇒ Object
- #find_all(node, path) {|find_all_result| ... } ⇒ Object
- #xml_from_element(element) ⇒ Object
Instance Method Details
#array(name_or_node, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/transformator/dsl.rb', line 4 def array(name_or_node, &block) name_or_node = name_or_node.to_s if name_or_node.is_a?(Symbol) # eliminate "symbol"-case name = name_or_node.is_a?(String) ? name_or_node : name_or_node.value node = name_or_node.is_a?(Ox::Element) ? name_or_node : element(name, type: "array") if block append_accumulator = Struct.new(:elements) do def <<(element) self.elements.push(element) end end.new([]) yield(append_accumulator) node << element(name) do |array_element| append_accumulator.elements.each do |element| array_element << element end end end node end |
#cdata(content, &block) ⇒ Object
29 30 31 32 |
# File 'lib/transformator/dsl.rb', line 29 def cdata(content, &block) new_cdata = Ox::CData.new(content) block ? yield(new_cdata) : new_cdata end |
#element(name, options = {}) {|new_element| ... } ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/transformator/dsl.rb', line 34 def element(name, = {}, &block) new_element = Ox::Element.new(name) if (attributes = [:attributes]).is_a?(Hash) attributes.each_pair do |key, value| new_element[key.to_s] = value.to_s end end if nodes = [:nodes] (nodes.is_a?(Array) ? nodes : [nodes]).each do |node| new_element << node end end if text = [:text] new_element << text.to_s end if type = [:type] new_element["type"] = type.to_s end yield(new_element) if block new_element end |
#element_from_xml(xml, options = {}) ⇒ Object
65 66 67 |
# File 'lib/transformator/dsl.rb', line 65 def element_from_xml(xml, = {}) elements_from_xml(xml, ).first end |
#elements_from_hash(hash) ⇒ Object
61 62 63 |
# File 'lib/transformator/dsl.rb', line 61 def elements_from_hash(hash) Transformator.document_from_hash(hash).root.nodes end |
#elements_from_xml(xml, options = {}) ⇒ Object
69 70 71 |
# File 'lib/transformator/dsl.rb', line 69 def elements_from_xml(xml, = {}) Transformator.document_from_xml(xml, ).nodes end |
#find(node, path, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/transformator/dsl.rb', line 73 def find(node, path, &block) find_result = find_all(node, path).first if block && find_result yield(find_result) else find_result end end |
#find_all(node, path) {|find_all_result| ... } ⇒ Object
83 84 85 86 87 88 |
# File 'lib/transformator/dsl.rb', line 83 def find_all(node, path, &block) find_all_result = node.locate(Transformator.oxify_path(path)) yield(find_all_result) if block && !find_all_result.empty? find_all_result end |
#xml_from_element(element) ⇒ Object
90 91 92 |
# File 'lib/transformator/dsl.rb', line 90 def xml_from_element(element) Transformator.xml_from_document(element, with_xml: false) end |