module Netconf::RPC::Builder

Public Class Methods

method_missing( method, params = nil, attrs = nil ) click to toggle source

autogenerate an <rpc>, converting underscores (_) to hyphens (-) along the way …

# File lib/net/netconf/rpc.rb, line 22
def Builder.method_missing( method, params = nil, attrs = nil )

  rpc_name = method.to_s.tr('_','-').to_sym

  if params
    # build the XML starting at <rpc>, envelope the <method> toplevel element,
    # and then create name/value elements for each of the additional params.  An element
    # without a value should simply be set to true
    rpc_nx = Nokogiri::XML::Builder.new { |xml|
      xml.rpc { xml.send( rpc_name ) {
        params.each{ |k,v|
          sym = k.to_s.tr('_','-').to_sym
          xml.send(sym, (v==true) ? nil : v )
        }
      }}
    }.doc.root
  else
    # -- no params
    rpc_nx = Nokogiri::XML("<rpc><#{rpc_name}/></rpc>").root
  end

  # if a block is given it is used to set the attributes of the toplevel element
  RPC.add_attributes( rpc_nx.at( rpc_name ), attrs ) if attrs

  # return the rpc command
  rpc_nx
end