Class: Riddl::Utils::Properties::AddProperties

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

Overview

}}}

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

{{{



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# 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