Class: Riddl::Utils::Properties::PtcContent

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

{{{



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/ruby/riddl/utils/properties.rb', line 433

def response
  backend = @a[0]
  handler = @a[1]

  property = @r[1]
  value    = @p.detect{|p| p.name == 'value'}; value = value.nil? ? value : value.value
  content  = @p.detect{|p| p.name == 'content'}; content = content.nil? ? content : content.value
  content  = content.read if content.respond_to? :read
  minor    = @r[2]

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

  path = "/p:properties/*[name()=\"#{property}\"]#{minor.nil? ? '' : "/p:#{minor}"}"
  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.root.find(path)
    nodes.each do |ele|
      if value.nil?
        newstuff.each do |child|
          cpath = File.basename(child.path)
          subele = ele.find("p:" + cpath).first
          if subele
            subele.replace_by(child)
          else
            ele.add(child)
          end
        end
        ele.children.first.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

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