Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Standalone script to play around with ROM. http://rom-rb.org/
# Standalone script to try http://rom-rb.org/ with a database
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'rom'
gem 'rom-sql'
gem 'rom-rails'
gem 'activemodel'
gem 'sqlite3'
gem 'activesupport'
end
require 'rom'
require 'rom-rails'
require 'logger'
`rm -Rf /tmp/romtest.sqlite`
ROM.setup(:sql, 'sqlite:///tmp/romtest.sqlite', loggers: [Logger.new($stdout)])
class Users < ROM::Relation[:sql]
end
class User
include Virtus.model
attribute :id, Integer
attribute :name, String
end
class CreateUser < ROM::Commands::Create[:sql]
relation :users
register_as :create
result :one
end
class UserMapper < ROM::Mapper
relation :users
register_as :entity
model User
end
ROM.finalize
rom = ROM.env
gateway = rom.gateways.fetch(:default)
migration = gateway.migration do
change do
drop_table :users rescue nil
create_table :users do
primary_key :id
column :name, String, null: false
end
end
end
migration.apply(gateway.connection, :up)
command = rom
.command(:users)
.as(:entity)
.create
user = command.call(name: 'Don Alias')
p user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.