Class: Riddl::Utils::Properties::AddProperty

Inherits:
Implementation show all
Defined in:
lib/ruby/riddl/utils/properties.rb

Overview

Modifiable

Instance Method Summary collapse

Methods inherited from Implementation

#headers, #initialize, #status

Constructor Details

This class inherits a constructor from Riddl::Implementation

Instance Method Details

#responseObject

{{{



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# 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