Class: ROM::CSV::Commands::Update

Inherits:
ROM::Commands::Update
  • Object
show all
Defined in:
lib/rom/csv/commands/update.rb

Instance Method Summary collapse

Instance Method Details

#datasetObject



38
39
40
# File 'lib/rom/csv/commands/update.rb', line 38

def dataset
  relation.dataset
end

#execute(tuple) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/rom/csv/commands/update.rb', line 10

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

  update(tuple)
end

#original_datasetObject



42
43
44
# File 'lib/rom/csv/commands/update.rb', line 42

def original_dataset
  source.dataset
end

#update(tuple) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rom/csv/commands/update.rb', line 18

def update(tuple)
  original_data = original_dataset.to_a
  output = []

  dataset.each do |dataset_tuple|
    index = original_data.index(dataset_tuple)
    update_dataset(index, tuple)
    output << original_dataset.data[index].to_hash
  end

  original_dataset.sync!
  output
end

#update_dataset(index, tuple) ⇒ Object



32
33
34
35
36
# File 'lib/rom/csv/commands/update.rb', line 32

def update_dataset(index, tuple)
  tuple.each do |key, value|
    original_dataset.data[index][key] = value
  end
end