Created
July 20, 2015 02:00
-
-
Save xaviershay/9e7c0b6e61aadbd4da1f to your computer and use it in GitHub Desktop.
Standalone script to play around with ROM. http://rom-rb.org/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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