class Riddl::Utils::Properties::AddProperties

Public Instance Methods

response() click to toggle source
# File lib/ruby/riddl/utils/properties.rb, line 309
def response
  backend = @a[0]
  handler = @a[1]

  0.upto(@p.length/2-1) do |i|
    property = @p[i*2].value
    ct       = @p[i*2+1]
    value    = ct.name == 'value' ? ct.value : nil
    content  = ct.name == 'content' ? ct.value : nil

    unless backend.modifiable?(property)
      @status = 500
      return # change properties.schema
    end

    path = "/p:properties/*[name()=\"#{property}\"]"
    nodes = backend.data.find(path)
    if nodes.empty?
      @status = 404
      return # this property does not exist
    end

    if backend.is_state?(property)
      unless backend.valid_state?(property,nodes.first.to_s,value)
        @status = 404
        return # not a valid state from here on
      end
    end

    newstuff = value.nil? ? XML::Smart.string(content).root.children : value
    backend.modify do |doc|
      nodes = doc.find(path)
      nods = nodes.map{|ele| ele.children.delete_all!; ele}
      nods.each do |ele|
        if value.nil?
          ele.add newstuff
          ele.attributes['changed'] = Time.now.xmlschema if backend.is_state?(property)
        else
          ele.text = newstuff
          ele.attributes['changed'] = Time.now.xmlschema if backend.is_state?(property)
        end
      end
    end || begin
      @status = 400
      return # bad request
    end

  end
  EM.defer do
    0.upto(@p.length/2-1) do |i|
      property = @p[i*2].value
      handler.property(property).create
    end
  end unless handler.nil?
  return
end