Class: ROM::SQL::Commands::Update

Inherits:
Commands::Update
  • Object
show all
Extended by:
Deprecations
Includes:
ErrorWrapper, Transaction
Defined in:
lib/rom/sql/commands/update.rb

Overview

Update command

Instance Method Summary collapse

Methods included from ErrorWrapper

#call

Methods included from Transaction

#transaction

Instance Method Details

#change(original) ⇒ Command::Update

Update existing tuple only when it changed

Examples:

user = rom.relation(:users).one
new_user = { name: 'Jane Doe' }

rom.command(:users).change(user).call(new_user)

Parameters:

  • original (#to_h, Hash)

    The original tuple

Returns:

  • (Command::Update)


59
60
61
62
# File 'lib/rom/sql/commands/update.rb', line 59

def change(original)
  Deprecations.warn("#{self.class}#change is deprecated. Use repositories with changesets instead")
  self.class.build(relation, options.merge(original: original.to_h))
end

#execute(tuple) ⇒ Array<Hash>, Hash

Updates existing tuple in a relation

Returns:

  • (Array<Hash>, Hash)


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rom/sql/commands/update.rb', line 33

def execute(tuple)
  attributes = input[tuple]
  validator.call(attributes)

  changed = diff(attributes.to_h)

  if changed.size > 0
    update(changed)
  else
    EMPTY_ARRAY
  end
end