Class: ROM::CSV::Commands::Create

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

Instance Method Summary collapse

Instance Method Details

#datasetObject



34
35
36
# File 'lib/rom/csv/commands/create.rb', line 34

def dataset
  relation.dataset
end

#execute(tuples) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/rom/csv/commands/create.rb', line 10

def execute(tuples)
  insert_tuples = [tuples].flatten.map do |tuple|
    attributes = input[tuple]
    validator.call(attributes)
    attributes.to_h
  end

  insert(insert_tuples)
  insert_tuples
end

#insert(tuples) ⇒ Object



21
22
23
24
# File 'lib/rom/csv/commands/create.rb', line 21

def insert(tuples)
  tuples.each { |tuple| dataset << new_row(tuple) }
  dataset.sync!
end

#new_row(tuple) ⇒ Object



26
27
28
# File 'lib/rom/csv/commands/create.rb', line 26

def new_row(tuple)
  ::CSV::Row.new(dataset.data.headers, ordered_data(tuple))
end

#ordered_data(tuple) ⇒ Object



30
31
32
# File 'lib/rom/csv/commands/create.rb', line 30

def ordered_data(tuple)
  dataset.data.headers.map { |header| tuple[header] }
end