Skip to content

Instantly share code, notes, and snippets.

@hieuk09
Created November 28, 2020 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hieuk09/11feb1ef50061ef74ce5c5168e4fc0d9 to your computer and use it in GitHub Desktop.
Save hieuk09/11feb1ef50061ef74ce5c5168e4fc0d9 to your computer and use it in GitHub Desktop.
ROM auto_struct does not respect struct configuration
require 'rom'
require 'rom-repository'
rom = ROM.container(:sql, 'sqlite::memory') do |conf|
conf.default.create_table(:users) do
primary_key :id
column :name, String, null: false
end
class Users < ROM::Relation[:sql]
schema(infer: true)
end
conf.register_relation(Users)
end
module Types
include Dry.Types()
UpcaseString = Types::String.constructor { |input| input.upcase }
end
module Entities
class User < ROM::Struct
attribute :name, Types::UpcaseString
end
end
class UserRepo < ROM::Repository[:users]
commands :create
struct_namespace Entities
end
repo = UserRepo.new(container: rom)
user_1 = repo.create(name: 'abc')
user_2 = repo.users.where(id: 1).map_to(Entities::User).first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment