class Riddl::Utils::Properties::AddProperty

Modifiable

Public Instance Methods

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

  property = @p[0].value
  ct       = @p[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.any?
    @status = 404
    return # this property does not exist
  end

  if backend.is_state?(property)
    unless backend.init_state?(property,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|
    ele = doc.root.add property
    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 || begin
    @status = 400
    return # bad request
  end

  EM.defer{handler.property(property).create} unless handler.nil?
  return
end