class HecksAdapters::SQLDatabase::Commands::Create

Create a resource

Attributes

id[R]

Public Class Methods

new(attributes:, head:) click to toggle source
# File lib/commands/create.rb, line 11
def initialize(attributes:, head:)
  @attributes = attributes.clone
  @reference_ids = {}
  @head = head
  @references = @head.references
  @table = Table.factory([@head]).first
end

Public Instance Methods

call() click to toggle source
# File lib/commands/create.rb, line 19
def call
  DB.transaction do
    find_or_create_references
    create
    add_to_join_tables
  end
  self
end

Private Instance Methods

add_to_join_tables() click to toggle source
# File lib/commands/create.rb, line 51
def add_to_join_tables
  AddToJoinTables.new(
    head: @head,
    id: @id,
    reference_ids: @reference_ids
  ).call
end
create() click to toggle source
# File lib/commands/create.rb, line 40
def create
  graph = @references.map do |reference|
    next if reference.list?
    column = Column.factory(reference)
    [column.to_foreign_key, @reference_ids[reference.name]]
  end.compact.to_h

  @id = @attributes[:id]
  DB[@table.name.to_sym].insert(@attributes.merge(graph))
end
find_or_create_references() click to toggle source
# File lib/commands/create.rb, line 30
def find_or_create_references

  @reference_ids =
  FindOrCreateReferences.new(
    head: @head,
    attributes: @attributes
  ).call.reference_ids

end