Skip to content

Instantly share code, notes, and snippets.

@solnic
Created January 12, 2017 17:25
Show Gist options
  • Save solnic/0287833df27a90a77be735ea20b75e7b to your computer and use it in GitHub Desktop.
Save solnic/0287833df27a90a77be735ea20b75e7b to your computer and use it in GitHub Desktop.
require 'byebug'
require 'rom-sql'
rom = ROM.container(:sql, 'sqlite::memory') do |conf|
conf.gateways[:default].create_table(:users) do
primary_key :id
column :name, String
column :created_at, Time
column :updated_at, Time
end
conf.relation(:users) do
schema(infer: true) do
attribute :id, ROM::SQL::Types::Int
primary_key :id
end
end
conf.commands(:users) do
define(:create) do
result :one
register_as :create
def set_timestamps(tuple)
tuple.merge(created_at: Time.now, updated_at: Time.now)
end
end
end
end
puts rom.commands[:users][:create].before(:set_timestamps).call(name: 'Chris').inspect
# {:id=>1, :name=>"Chris", :created_at=>2017-01-12 18:25:22 +0100, :updated_at=>2017-01-12 18:25:22 +0100}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment