Class: Graffiti::SquishAssertStatement

Inherits:
Object
  • Object
show all
Includes:
Debug
Defined in:
lib/graffiti/squish.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clauses, values) ⇒ SquishAssertStatement

Returns a new instance of SquishAssertStatement.



488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# File 'lib/graffiti/squish.rb', line 488

def initialize(clauses, values)
  @key_node = clauses.first[:subject][:node]
  @table = clauses.first[:map].table.to_sym

  key = values[@key_node]

  @params = {}
  @references = []
  clauses.each do |clause|
    node = clause[:object][:node]
    v = values[node]

    if key.new? or v.updated?
      field = clause[:object][:field]
      @params[field.to_sym] = v.value

      # when subproperty value is updated, update the qualifier as well
      map = clause[:map]
      if map.subproperty_of
        @params[ RdfPropertyMap.qualifier_field(field).to_sym ] = values[map.property].value
      elsif map.superproperty?
        @params[ RdfPropertyMap.qualifier_field(field).to_sym ] = nil
      end

      @references.push(node) if v.new?
    end
  end

  if key.new? and @table != :resource
    # when id is inserted, insert_resource() trigger does nothing
    @action = :insert
    @params[:id] = key.value

  elsif not @params.empty?
    @action = :update
    @filter = {:id => key.value}
  end

  debug { 'SquishAssertStatement ' + self.inspect }
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



529
530
531
# File 'lib/graffiti/squish.rb', line 529

def action
  @action
end

#key_nodeObject (readonly)

Returns the value of attribute key_node.



529
530
531
# File 'lib/graffiti/squish.rb', line 529

def key_node
  @key_node
end

#referencesObject (readonly)

Returns the value of attribute references.



529
530
531
# File 'lib/graffiti/squish.rb', line 529

def references
  @references
end

Class Method Details

.run_ordered_statements(db, statements) ⇒ Object

make sure mutually referencing records are inserted in the right order



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/graffiti/squish.rb', line 542

def SquishAssertStatement.run_ordered_statements(db, statements)
  statements = statements.sort_by {|s| s.references.size }
  inserted = []

  progress = true
  until statements.empty? or not progress
    progress = false

    0.upto(statements.size - 1) do |i|
      s = statements[i]
      if (s.references - inserted).empty?
        s.run(db)
        inserted.push(s.key_node)
        statements.delete_at(i)
        progress = true
        break
      end
    end
  end

  statements.empty? or raise ProgrammingError,
    "Failed to resolve mutual references of inserted resources: " +
    statements.collect {|s| s.key_node + ' -- ' + s.references.join(', ') }.join('; ')
end

Instance Method Details

#run(db) ⇒ Object



531
532
533
534
535
536
537
538
# File 'lib/graffiti/squish.rb', line 531

def run(db)
  if @action
    ds = db[@table]
    ds = ds.filter(@filter) if @filter
    debug { :insert == @action ? ds.insert_sql(@params) : ds.update_sql(@params) }
    ds.send(@action, @params)
  end
end