Method: Jamf::Utility#item_list_to_rexml_list

Defined in:
lib/jamf/utility.rb

#item_list_to_rexml_list(list_element, item_element, item_list, content = :name) ⇒ REXML::Element

Given an Array of Hashes with :id and/or :name keys, return a single REXML element with a sub-element for each item, each of which contains a :name or :id element.

e.g. :computers

e.g. :computer

Examples:

comps = [{:id=>2,:name=>'kimchi'},{:id=>5,:name=>'mantis'}]
xml = JSS.item_list_to_rexml_list(:computers, :computer , comps, :name)
puts xml
# output manually formatted for clarity. No newlines in the real xml string
<computers>
  <computer>
    <name>kimchi</name>
  </computer>
  <computer>
    <name>mantis</name>
  </computer>
</computers>

# if content is :id, then, eg. <name>kimchi</name> would be <id>2</id>

Parameters:

  • list_element (#to_s)

    the name of the XML element that contains the list.

  • item_element (#to_s)

    the name of each XML element in the list,

  • item_list (Array<Hash>)

    an Array of Hashes each with a :name or :id key.

  • content (Symbol) (defaults to: :name)

    which hash key should be used as the content of if list item? Defaults to :name

Returns:

  • (REXML::Element)

    the item list as REXML



535
536
537
538
539
540
541
# File 'lib/jamf/utility.rb', line 535

def item_list_to_rexml_list(list_element, item_element, item_list, content = :name)
  xml_list = REXML::Element.new  list_element.to_s
  item_list.each do |i|
    xml_list.add_element(item_element.to_s).add_element(content.to_s).text = i[content]
  end
  xml_list
end