Broken example code, unexpected auto_struct behavior
pboling opened this issue · 3 comments
pboling commented
Describe the bug
I made a script of the example code, and it seems that the example code for the auto-generated structs no longer reflects reality, and as a result I am very confused about how to setup my custom name-spaced structs, when not even the auto-generated ones work as expected.
The example code I mostly copied is here: https://api.rom-rb.org/rom/ROM/Struct
Note that as written the example code doesn't work at all, as it was missing the conf.relation(:users)
, so I added that. Even with that, the example code still doesn't work at all, and raises the error:
undefined method 'schema' for ROM::OpenStruct:Class (NoMethodError)
To Reproduce
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'rom'
gem 'rom-sql'
gem 'sqlite3'
end
rom = ROM.container(:sql, 'sqlite::memory') do |conf|
conf.default.create_table(:users) do
primary_key :id
column :name, String
end
conf.relation(:users) do
end
end
class UserRepo < ROM::Repository[:users]
end
user_repo = UserRepo.new(rom)
# get auto-generated User struct
model = user_repo.users.mapper.model
puts "model: #{model}"
# EXPECTED
# model: ROM::Struct::User
#
# ACTUAL
# model: ROM::OpenStruct
puts model.schema.key(:id)
# EXPECTED:
# #<Dry::Types[id: Nominal<Integer meta={primary_key: true, source: :users}>]>
#
# ACTUAL:
# undefined method `schema' for ROM::OpenStruct:Class (NoMethodError)
My environment
- Affects my production application: NOT YET (Implementing ROM as part of a new service)
- Ruby version: 2.7.8 through 3.2.2
- OS: macOS Ventura 13.4