class Graffiti::SquishAssertStatement

Attributes

action[R]
key_node[R]
references[R]

Public Class Methods

new(clauses, values) click to toggle source
# 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
run_ordered_statements(db, statements) click to toggle source

make sure mutually referencing records are inserted in the right order

# 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

Public Instance Methods

run(db) click to toggle source
# 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