Class: Yinx::SQL::Batch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/yinx_sql/batch.rb

Class Method Summary collapse

Class Method Details

.insert(notes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yinx_sql/batch.rb', line 14

def self.insert notes
  batch = Batch.new

  stacks = Hash.new do |h, name|
    stack = Stack.new name: name
    batch.stacks << stack
    h[name] = stack
  end

  books = Hash.new do |h, stack_book|
    b = Book.new(name: stack_book[1])
    batch.books << b
    stacks[stack_book[0]].books << b
    h[stack_book] = b
  end

  tags = Hash.new do |h, name|
    tag = Tag.new name: name
    batch.tags << tag
    h[name] = tag
  end

  notes.each do |note|
    n = Note.new(title: note.title,
                 content_length: note.contentLength,
                 created_at: note.created_at,
                 updated_at: note.updated_at)

    book = books[[note.stack, note.book]]
    book.notes << n

    note.tags.each do |tag_name|
      tag = tags[tag_name]
      tag.notes << n
    end
  end

  batch.save
end